An Intro to Python Virtual Environments

Python has the concept of the virtual environments built-in to the language. A Python virtual environment is an environment where you can install 3rd party packages for testing without affecting the system Python installation. Each virtual environment has its own set of installed packages and, depending on the virtual environment and how it's set up, may have its own copy of the Python binary and standard library as well.

There are several different ways to create Python virtual environments. You will focus on the following two methods:

  • The built-in venv module
  • The virtualenv package

There are other tools that you can use to create virtual Python environments. You will learn a little about them in the last section of this chapter.

For now, let's get started by looking at the venv library!

Python's venv Library

Python added the venv module in version 3.3. You can read all about it here:

To use venv, you can run Python using the -m flag. The -m flag tells Python to run the specified module that follows -m.

Let's try it out. Open up a cmd.exe on Windows or a terminal in Mac or Linux. Then type the following:

python -m venv test

This will create a folder named test in whatever directory that you are open to in your terminal session.

To activate the virtual environment, you will need change directories into the test folder and run this on Linux/Mac:

source bin/activate

If you are a Windows user, you can activate it by running the bat file inside of the Scripts sub-folder that you will find in your test folder.

Now you should see something like this:

Python Virtual Environment

Note that the name of the prompt is now "test". That indicates that the virtual environment has been activated and is ready to use.

You can now install new packages and they will install to your virtual environment instead of your system Python.

When you are finished, you can deactivate the virtual environment by running deactivate in the terminal or command prompt. The exact nature of deactivate is implementation-dependent: it may be a script or batch file or something else.

PyCharm, WingIDE and VS Code all support using Python virtual environments. In fact, you can usually create and activate them from within the IDE rather than doing it on the command line.

The virtualenv Package

The virtualenv package was the original method for creating Python virtual environments. You can read the documentation for the virtualenv package here:

A subset of virtualenv was eventually integrated into Python's own venv module. The actual virtualenv package is better than venv in the following ways:

  • It's faster
  • Easier to extend
  • Can create virtual environments for multiple Python versions
  • Can be upgraded via pip
  • Has a rich programmatic API

You can install virtualenv by using pip:

pip install virtualenv

Once installed, you can create a virtual environment using your terminal or cmd.exe like this:

virtualenv FOLDER_NAME

Activating and deactivating the virtual environment works exactly as it did when you created a virtual environment using Python's venv module.

There are quite a few command line parameters you can use with virtualenv. You can read the full listing here:

https://virtualenv.pypa.io/en/latest/cli_interface.html

Most of the time, you can use the defaults. But there are times when it is nice to configure your virtual environment to use other pip versions, or give it access to your system's site-packages folder. Check out the link above to see what all you can do with virtualenv.

Other Tools

There are other tools you can use to work with Python virtual environments. Here are just a few:

Anaconda has its own tooling for creating virtual environments.

The other two are popular packages for creating and managing virtual environments. Both pipx and pipenv are quite popular. You should read up on them and determine if they might be useful for your own projects.

Wrapping Up

Python virtual environments are a great way to isolate your system Python while allowing you to test out new packages. You can test out multiple versions of a package by using multiple virtual environments. Then when you are done, you can simply delete the virtual environment's folder.

This allows for quick iterations to verify that nothing in your package stack is causing breakage. Standard practice is to always use a virtual Python environment whenever you are testing out a new package.

Go ahead and give it a try. You'll soon find that it becomes second nature and it's super useful to boot!

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary