After having worked on several Python projects, I have found that the easiest way to manage your development environment is to use venv and pip to manage your dependencies.

As a way of example, we will create a project called my_project and install requests (a popular Python package) in it. This will be the structure of our project:

my_project
├── my_project.py
├── requirements.txt
└── venv
    ├── bin
    │   └── ...
    ├── include
    ├── lib
    │   └── ...
    ...

Installation

We need the python3.5-venv package:

_$: sudo apt install python3.5-venv

Create the project directory

This is a simple step, just create a directory:

_$: mkdir my_project

Create the virtual environment

Inside our project we will create a directory called venv (virtual environment) to hold our dependencies. Alternatively, you can call it my_project_env or something similar if you would rather have the name of your project visible at all times. Since I am not usually changing a lot of times throughout the day from one project to another, I prefer to have a simple name.

_$: cd my_project
_$: python3.5 -m venv ./venv

Activate the virtual environment

You will have to do this any time you start working on this project to have all your dependencies available. Note that when you activate your virtual environment, the shell prompt changes its name.

_$: source ./venv/bin/activate
(venv)_$: ls
venv

Create your project

Time to code! Let’s create a simple Python program:

# my_project.py
# -------------

def main():
    print("Hello, world!")


if __name__ == '__main__':
    main()

Install packages

When your project gets bigger you will surely need some Python packages to ease your development. Let’s install requests to show how it is done:

Note: We don’t need requests at all for our simple project. At this time it is only used as an example of a package installation using pip in a virtual environment.

(venv)_$: pip install requests
Collecting requests
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 2.0MB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 2.6MB/s
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 133kB 2.5MB/s
Collecting certifi>=2017.4.17 (from requests)
  Downloading certifi-2017.7.27.1-py2.py3-none-any.whl (349kB)
    100% |████████████████████████████████| 358kB 2.0MB/s
Collecting idna<2.7,>=2.5 (from requests)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 5.7MB/s
Installing collected packages: chardet, urllib3, certifi, idna, requests
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

Apart from installing requests it has also installed the dependencies needed for requests to run.

Create the requirements file

It is a kind of standard practice to have a requirements file in which you specify all the packages your project depends on. In this case, we should specify only requests since the rest of the packages installed are requests’ dependencies, not ours.

requirements.txt:
-----------------
requests==2.18.4

Once you have a requirements file, you can install all packages inside that file with the following command:

(venv)_$: pip install -r requirements.txt

Find your dependencies

After you install lots of packages you can loose track of what packages you have installed in your virtual environment. To see all your currently installed packages you can do the following:

(venv)_$: pip freeze

Note: pip freeze shows your dependencies and your dependencies’ dependencies all mixed, so while you could create your requirements file using pip freeze > requirements.txt it would list, and therefore save, all the packages currently installed as opposed to only your dependencies.

Deactivate your virtual environment

In case you need it, here is how to deactivate your virtual environment:

(venv)_$: deactivate
_$: ls
my_project.py requirements.txt venv

But you don’t have to do it, merely closing the shell works just as well.