Enchanted Code

Using Python Virtual Environments (venv)

3 minutes read

Intro

This tutorial is aimed to provide knowledge of Python’s venv interface. It will teach you how to create and use Python virtual environments.

Requirements

  • Have Python 3.3 or greater installed, with pip & venv
  • Know how to use the terminal/command-prompt

What Is It?

Creating Python virtual environments allows you to create isolated Python instances, these can have their Python executable and set of installed packages.

Having a isolated environment is useful, as when working on several projects you may have conflicting dependencies due to different versions or having multiple packages with the same name.

Also having your installed packages split from the “system” Python installation allows you to keep the original clean, also when you have finished with a Python virtual environment you can also simply delete the directory and don’t have to worry about uninstalling specific packages.

Creating An Environment

Creating a virtual environment is easy, we can do this with the command listed below.

1
python -m venv <path to environment>

Generally we separate our projects into different directories, so we can make a virtual environment in that directory called something like “.venv”. In most IDE’s it will autodetect the newly created environment and ask you whether you want to use it for that specific project, this is very useful.

Using Environment

If we are using an environment from the terminal and not an IDE, we need “activate” the environment. Depending on your system we will need to use different commands.

If on Windows we will need to execute a bat file in the terminal.

1
<path to environment>/Scripts/activate.bat

On a POSIX based system (Linux, Unix, MacOS) we will need to execute a shell script inside the environments bin directory, however instead of running the script directly we instead use the “source” command.

1
source <path to environment>/bin/activate

Depending on your system you may need a different script for example: activate.fish, these are listed at the official Python docs.

Once we have “activated” our environment we should see the name of our environment before the prompt in our terminal. This should look something similar to what is shown below:

1
(venv) user@computer:~$

Now we have an environment we can use the normal Python commands, for example pip to add packages to our environment. For example we could add the flask package:

1
(venv) user@computer:~$ python -m pip install flask

When we are finished with our environment we use the “deactivate” command. This should get back to a normal terminal prompt without the environment name.

This won’t delete the environment we have to do this manually

1
(venv) user@computer:~$ deactivate

Conclusion

As you can see Python virtual environments are very useful for managing installed packages in different projects.

There are a few features of venv that I did not mention in this tutorial however they can be found in the official Python docs.

Sources

See Also

What Is Pip?

A Python 3 tutorial to show how to use Python's pip command...

Buy Me A Coffee