Virtual Environments
Virtual Environments
As you start building more Python projects, both inside and outside a notebook environment, you’ll find yourself installing a growing set of libraries on your system. This will eventually lead to problems such as not knowing which projects use which libraries and where different projects require different versions of the same library.
To solve this problem, Python introduced the concept of virtual environments. These are isolated spaces where you can install and manage project-specific libraries and other dependencies, allowing you to have multiple Python projects on the same machine, each existing in its own “universe.”
Creating a Virtual Environment
To create a virtual environment, go to the command line and select the directory where your project and the virtual environment will go.
Once inside that directory, create a virtual environment using the Python Standard Library’s venv module by running the command below. You may need to type python3:
python -m venv my_env
Here’s what each item in the command above does:
-
pythonlaunches the Python interpreter. - The
-mswitch tells Python to run the contents of the filename that follows it as a program file and load it as if it were a library. -
venvis the virtual environment module, which is part of the Python Standard Library. You’re using the Python interpreter to run this program to create the virtual environment. -
my_envis the name of the virtual environment that you’re creating.
Creating a virtual environment also creates a new support directory in the current working directory. This directory has the same name as the virtual environment you created, and contains:
- A copy of the Python interpreter and
pip. - A subset of Python Standard Library.
- Scripts for activating the virtual environment.
- A configuration file,
pyvenv.cfg, containing information about the virtual environment, including the path to your system’s main Python executable.
Activating the Virtual Environment
To use the virtual environment you created, you must activate it using one of the activation scripts in your virtual environment’s support directory. There are different scripts for Windows and Unix-based systems like macOS and Linux.
On Windows, enter this command in PowerShell to activate your virtual environment. This assumes that your virtual environment and its support directory are named my_env:
my_env\Scripts\activate
On macOS and Linux, use this command instead:
source my_env/bin/activate
While the virtual environment is activated, its name will appear before the command line prompt.
If you’re on Windows, your virtual environment is named my_env, and the current directory is C:\Users\Me\Documents\Python\my-project, your PowerShell prompts will look like this when the virtual environment is active:
(my_env) C:\Users\Me\Documents\Python\my-project
If you’re on macOS or Linux, your virtual environment is named my_env, and the current directory is ~/Documents/Python/my-project, your terminal prompts will look like this when the virtual environment is active:
(my_env) ~/Documents/Python/my-project
Deactivating the Virtual Environment
The command to deactivate a virtual environment is the same, regardless of platform:
deactivate
Time to put this into practice.