1.4. Virtual environments

It has been said already more than once: Using virtual environments for your Python projects is not a nice option, but a necessary prerequisite. The reasons are manifold, and perhaps one of the most important: many Linux distributions use Python for mission-critical tasks. Hence updating your Python version globally or installing/updating packages is usually not a good idea of you don’t want to end up having to reinstall your system from scratch. Therefore:

Warning

Always use (separate) virtual environments for each of your Python projects. Python provides you with easy-to-use tools for this.

Some more general introduction to Python virtual environments as well as high-level and lower-level ways to deal with virtual environments can be found in the corresponding section in the Hitchhiker’s Guide to Python. Don’t miss out the official documentation of virtual environments.

1.4.1. Creating virtual environments

Probably the most robust way of creating a virtual environment is by using python3 directly:

python3 -m venv <name_of_environment>

with “<name_of_environment>” being the name (and directory) of the virtual environment to be created. This will create the respective folder and install a Python virtual environment within this folder. The virtual environment will contain copies of the standard Python interpreter (in the same version as installed on your operating system) as well as a minimum list of packages, such as the package manager pip.

Note

Either create your virtual environment(s) outside of your project folder, or make sure to exclude it from the version control (using .gitignore and adding the path to the virtual environment to this file).

1.4.2. Activating and deactivating virtual environments

Having created a virtual environment doesn’t help if we do not activate it. Activating a virtual environment is as simple as issuing the following command in a terminal:

source <name_of_environment>/bin/activate

Let’s look at a more specific example. Suppose you have created a virtual environment with the name “python-course”, by issuing:

foo@foobar:~$ python3 -m venv python-course

Note that in the above example, foo@foobar:~$ is my terminal prompt.

After activating this environment by calling

foo@foobar:~$ source python-course/bin/activate

your terminal prompt will change to something like:

(python-course) foo@foobar:~$

As you can see, your terminal prompt will be prefixed by the name of the currently active virtual environment. This is very convenient not only to know that you have activated a virtual environment, but having multiple of them, knowing immediately as well with has been activated.

Once activated, you can install packages and run your programs.

So, how now to clean up afterwards and deactivate the currently active virtual environment? Besides simply closing the terminal, there is an explicit command for this, usefully named deactivate and only accessible from within an activated virtual environment:

(python-course) foo@foobar:~$ deactivate

This will leave the virtual environment, and your terminal prompt will change back to what it looked like before activating your virtual environment:

foo@foobar:~$

1.4.3. Virtual environments and PyCharm

If you use an IDE like PyCharm (as suggested earlier), you will need to make your virtual environment known to the IDE as well. PyCharm offers both, creating a new virtual environment, as well as adding (and using) an existing virtual environment for your project. Note that PyCharm (as many IDEs) “thinks” in terms of projects.

You will find the necessary settings in PyCharm in “Settings” > “Project” > “Python Interpreter”.