Python Virtual Environments with “venv” — A Step-By-Step Guide

How does the tool venv work?

The venv module is the new default way of creating basic virtual environments for new Python versions >3.3. If you dive into virtual environments, you’ll quickly realize that there are a multitude of tools out there such as “virtualenv”, “pyenv”, and many more.

My recommendation for data scientists and beginners is the tool “conda” that comes with the Anaconda Python distribution. I’ve written an article about the concepts of virtual environments in Python, including a tutorial on how to use conda for your own projects:

Python Virtual Environments with Conda — Why the Buzz?

The “venv” tool is the de-facto standard that is already preinstalled with your Python 3.3+ installation. You should learn this tool first (probably you can write Python code for many years before you are forced to touch another virtual environment tool).

Let’s start slowly: Python is a program such as everything else running on your computer. Programs are compiled into machine-readable binary code that is stored in a file. Hence, Python is nothing but a compiled binary file which you can execute on your computer just like Tetris or Minesweeper (I’m not really a gamer though). If you run the command “python” in your shell, the binary is executed by your operating system.

As a side note: you may have to explicitly specify the location (path) of the Python binary file in your operating systems “environment variables” so that your computer can find the program “python”.

Test whether your Python installation works correctly by opening a shell and typing “python”.

The default way of working on your code project is as follows:

  • write code until you need some library,
  • find the library and import it using the “import” statement,
  • if Python throws an error, install the library using the pip tool “pip install library” etc.

The problem is that all your projects share the same globally installed libraries. But some of them may require different versions or incompatible libraries. Also, you don’t want to clutter your Python installation with hundreds of external libraries.

This is where virtual environments come into play. A virtual environment serves as a “sandbox” for your Python program. You can install any external library or version there without having any global impact. The virtual environments are isolated, independent, and separate.

How to create virtual environments with Python’s “venv”?

The simple answer lies in the following code snippet:

python -m venv ve

The placeholder “ve” is simply the path to the virtual environment you want to create. In practice, it’ll be the path to the folder of your Python project that should be executed under the virtual environment.

The code snippet does multiple things: it creates a folder that contains a copy of the Python program itself. This means that any package you install within the virtual environment is not visible to your global Python installation.

Now, the only thing left is to activate your virtual environment using the command (Bash):

source ve/bin/activate 

Or the command (Win):

ve\Scripts\activate.bat

Now, you can simply execute “python” in your shell and all programs you execute there will be executed within the Python virtual environment.

How to install libraries in your virtual environment?

That’s easy, simply use the pip tool to install packages after you have activated the virtual environment.

pip install package 

It will automatically detect that you are currently in a virtual environment (as you have activated the environment).

You can simply deactivate the virtual environment by typing the command:

deactivate

Summary

Virtual environments help you to isolate the dependencies of your Python projects. Simply create your virtual environment in your project location by using the command “python -m venv your_ve_path”. After activation, you can install new packages using pip. All new packages will be installed only in your virtual environment without global visibility.

Was this tutorial helpful for you? Comment below!

Comments

  1. Hi,
    thank you for this useful guide.
    I have just one question, how can I “transmit” or “deliver” a virtual environment for testing purpose or other to a colleague?
    or, is there another tool for doing this ?
    thanks in advance for your help
    Regards
    Erix

    1. Author

      Hey Erix,

      thanks! This is a very good question.

      For conda, it’s very easy: check out their guide here:
      https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
      “With conda, you can create, export, list, remove, and update environments that have different versions of Python and/or packages installed in them. Switching or moving between environments is called activating the environment. You can also share an environment file.”

      For the “venv” tool, it may work out to copy&paste the folder and use this guide to setup the used interpreter to the one in the virtual environment folder:
      https://www.techcoil.com/blog/how-to-associate-a-virtualenv-environment-with-a-python-project-in-pycharm/

      But I haven’t tried this out — and it certainly isn’t the cleanest solution because there may be some hard-coded paths in your virtual environment.

      I would write a small installation script that recreates the virtual environment for your friend or colleague. It’s simple, lightweight, and very flexible.
      The brute-force approach would be to use a Docker image, etc.

      Hope this helps.
      Chris

Leave a Comment