Why Should I Use Python Virtual Environments?

Why would we need to use Python virtual environments? What’s the point?

A virtual environment is really just a folder on your computer housing some programming project that you are working on, and some additional code that makes it a “virtual environment”. Inside this folder is your project’s code and the necessary dependencies (i.e. Python packages) for your project. The purpose of segmenting the project into its own “environment” is so that you can isolate each project’s set of dependencies.

This becomes relevant when your projects require the same Python package but different versions. Let’s say you are interested in making a computer game using Python’s PyGame package. And a year from now, you upgrade to the new version of PyGame for a different project. When you attempt to run your former project’s code without using virtual environments, it may not work (it will only work if the functions/classes you used were not changed in the package’s upgrade). Instead, it’s much easier to use a virtual environment. They ensure that your code will run regardless of which package version is currently your default. In general, it’s best practice to isolate your coding environments as much as possible in order to achieve repeatability and reliability.

 

How Does it Work?

When you create a virtual environment and “activate” that virtual environment, it modifies your Python’s PATH (“sys.path”) variable. What is this Python PATH variable? When you import a Python package for use in your code, it searches folders of your computer in a particular order to find the package you are asking it to run. This order is defined in your Python’s sys.path variable. The first instance of the package name that it finds will be the one it uses. If interested, we explain how you can view your PATH variable and create your own directories here.

A virtual environment essentially hacks that PATH variable by listing a folder of its own to search through first. So, when you import a package in your code, it will use the package in its own directory (if it exists). This allows you to control the version of your dependencies for each project. To use a virtual environment, you “activate” it. By activating it, you are changing the PATH variable to list the virtual environment’s package directory first.

Create a Virtual Environment

There have been several packages made over time for virtual environments because the community was struggling to agree on one (if interested, check out this link for all of the different versions). Now, it seems settled. The package of choice and the one adopted by Python’s Standard Library from Python 3.6+ is called venv.

IF YOU ARE NOT USING PYTHON 3.6 OR LATER:

If you are using Python 3.3/3.4/3.5, you will need to use the built-in pyvenv package, which is a necessary wrapper around the new venv function. If you are running Python 2, you’ll need to use the virtualenv module instead. Since this virtualenv module is not included in the standard library, you will need to pip install it.

Since many of us are likely using Python 3.6 or later, the code we cover below will be for those using venv.

To create a Python virtual environment, make a new directory on your file system. If you are unfamiliar with coding on the command line, search and open Terminal (Mac) or Command Prompt (Windows). Use the “cd” command to change directories, and the “mkdir” command to make a new directory. I’ll navigate to my Desktop and make a new folder called tutorialenv, then change directories into that newly created folder.

cd /Users/nweimer/Desktop 
mkdir tutorialenv
cd tutorialenv

Note: Use “cd ..” to move to the directory one level up. Use “pwd” on Mac (on Windows, working directory is already listed before command prompt cursor) to print your current working directory. Use “ls” on Mac (“dir” on Windows) to list the files/folders in the current directory.

Inside this folder, we want to create a virtual environment. We’ll call it ourenvironment. To do so:

python3 -m venv ourenvironment

Previously, we talked about activating our virtual environment, which changes our PATH variable (lists the “site packages” path of our virtual environment first). To activate our virtual environment on Mac, run the source command followed by the path to your virtual environment’s activate script:

source /Users/nweimer/Desktop/tutorialenv/ourenvironment/bin/activate


Note: On windows, the activate script is in the Scripts directory. You also won’t need the “source” command. So for Windows users, run your path such as:

\Users\nweimer\Desktop\tutorialenv\ourenvironment\Scripts\activate

When your virtual environment is activated, you are able to install packages using pip. As expected, it will install packages only within your virtual environment. These installed packages get placed in your virtual environment’s /lib/pythonx.x/site-packages/ folder as opposed to your computer’s typical Python site packages folder. At this point, you are now set to work on a Python project within your virtual environment. You can start writing a new python script inside the ourenvironment folder.

When you want to deactivate, for both Windows and Mac users, simply type the word:

deactivate

Congratulations! You are now able to create your own virtual environments in Python. Let us know of any questions you may have. Happy coding!