
Whether you are working on a project on your own or collaborating with others, there are 3 simple tips to help you keep your space organized.
- Tip #1: Create a virtual environment
- Tip #2: Create a separate directory for tests
- Tip #3: Create different content directories
If you want to go a step further, you should also:
Some sections are related to PyCharm. You can skip it if you use another IDE.
Tip #1: Create a Virtual Environment
In order to keep your project space running, it’s a good idea to create a virtual environment and keep your dependencies isolated.
You can use the venv
module of Python and specify the Python version and the environment name. In the example below, I am using Python3 and calling the environment venv. Once you have created the directory, you need to run the script activate
inside it.

Summary of commands:
python3 -m venv venv
→ creates a virtual environmentsource venv/bin/activate
→ activates the environmentdeactivate
→ deactivates the environment
👁 🗨 PyCharm Insights
If you are using PyCharm you will see a venv
directory popping up in your project directory on the left. A number of things are automatically installed in it. Most likely, you only care about the activate
script – as we saw above.

Importantly, in PyCharm, you should make an extra step so that your programs can run in your virtual environment.
Go to the bottom-right corner. You should see which interpreter PyCharm is using.

Click on "Add Interpreter" and select the option "Existing environment".
Next, navigate to the folder where you have created the environment and select Python from the bin
directory.

If everything works correctly, you should see, at the bottom-right corner, the Python version that exists in your virtual environment. Nice! 🎊

In a nutshell, virtual environments allow you to:
- Maintain dependencies isolated. This avoids situations where you have Projects using different package versions and you globally uninstalling/reinstalling what you need every time you need to run a project.
- Share your dependencies with other people.
Once you have installed all the packages that your project needs, you can run:
pip freeze > requirements.txt
pip freeze
is "freezing" all the packages/versions currently being used. Then you are piping (i.e., saving) the output of pip freeze
via a txt file.
Other people using your program will then run:
pip install -r requirements.txt
and be able to install all the packages and the correct versions in one go. Amazing right? 🚀

Tip #2: Create a separate directory for tests
You’ve heard that creating tests for your code is a good idea. So you’re writing Python code and trying to test it with Pytest.
Example: Let’s say you create a file called greetings.py
and another one to write some tests called test_greetings.py
.
In this example, I will use Pytest as the package for testing. Pytest has a lot of naming conventions. Starting the file name with test_
is one of them. If you follow it, Pytest will automatically look in the current directory and the ones underneath it for those files that start with test_
and run them.
To create the new files, you can use the touch
command in your terminal:

In the top-right part of PyCharm, you should see something like:

Open greetings.py
and write a simple function:
In your terminal, you can run pip install pytest
to install the pytest
module and then simply pytest
. Since you haven’t defined any test yet, pytest
will run but collect 0 items.

Open test_greetings.py
and write a test function:
If you notice, also the function name needs to start with test_
, e.g. test_say_hello
. This is another Pytest naming convention.
How to debug ModuleNotFound Error
Another good idea is to collect all your tests in one directory.
However, if you simply do this, when you now run pytest
, you will see that none of your tests is working. If you look closer, you are probably getting a ModuleNotFoundError
.
Let’s see what happened and how to fix it.

Pytest is trying to import the greetings
module but it is failing.
The easiest way to fix this is to pretend that the test
directory is a package (i.e., a collection of modules). In the directory, create a file named __init__.py

This is a entirely empty file:

However, the simple fact of being there makes your test work again.

Voila’! 🍷
Tip #3: Create different content directories
Now that you got the idea of creating an __init__.py
file, you are good to create as many directories as you want.
For instance:
Basically:
__init__.py
is used to mark directories as Python package directories
If you remove the __init__.py
file, Python will no longer look for submodules inside that directory. Hence, if you try to import the module somewhere else, it will fail.
Tip #4: Document Your Code
Documenting your code is very important for both "future" you and other people reading your project. Some programmers even say:
If it’s hard to document your code, consider changing the design.
There are 3 critical things you should keep in mind:
- Add a docstring with a description at the start of each file

- Add a docstring to each function and class

- Use type-hints whenever you define a function or a class

Type-hints (1) make functions and classes more readable, (2) allow you to bypass specifying the type of parameters in the documentation, and (3) you can use them to automatically check your code with mypy
. 🎖
There are a lot of style guides for writing good documentation. I personally like the Google Style Python Docstring.
Tip #5: Use GitHub for Version Control
If you are working with someone else, Git + GitHub
are essential to do code reviews and to be able to avoid merge conflicts.
If you are working by yourself, they are still helpful to save your work and be able to revert changes and "go back in time".
🐢 If you are new to these concepts, you can find more information here:
😱 If you find yourself panicking when you start using version control:
💪 If you want to learn more:
The first approach with Git is awful, but I promise it will pay off. We’ve all been there 🙇 ♀️
I hope this was useful! ⚡️
References
- New course: Testing your Python programs with pytest by Reuven Lerner
- https://flax.readthedocs.io/en/latest/philosophy.html
- http://web.archive.org/web/20111010053227/
- http://jaynes.colorado.edu/PythonGuidelines.html#module_formatting
- https://realpython.com/lessons/type-checking-mypy/
- What is init.py used for?