Creating virtual environments with venv
A virtual environment is a clone of a Python installation stored in a local folder. The Python executables and packages are not really copied, but symbolic links to the original files are created and environment variables are adjusted to set up a consistent Python filesystem. Once activated, a virtual environment will install packages in local directories without modifying the global Python installation.
A virtual environment is also the recommended setup for development in Python. With a virtual environment, it becomes easy to isolate the packages required to install the software under development.
Note
If you are using Anaconda, do not create virtual environments using venv
. Instead, use conda
, as described in the previous section.
How to do it...
- To create the new environment in the
myenv
directory, use the following command in Linux and macOS:
python3 -m venv myenv
- In Windows, the command to be used is as follows:
python -m venv myenv
This creates an environment in the myenv
subdirectory of the current directory. The -m
switch is used because venv
is a Python module that is being executed.
- To switch to the new environment on Linux and macOS, run the following in the command line:
source myenv/bin/activate
- On Windows, we use the following command:
myenv\bin\activate
- If we need to install a package in the new environment, we use
pip
orpip3
as usual. For example, to install amolecule
package usingpip3
, we execute the following command:
pip3 install molecule
- Optionally, run the following command to create a
requirements
file:
pip3 freeze > requirements.txt
This will generate a report listing the packages required by the current environment and store the information in the requirements.txt
file. This file can be distributed with the package being developed to facilitate installation by users that use pip
.
- When done working in the environment, deactivate it by running the following command:
deactivate
- If we no longer need an environment, we can delete it as follows. First, activate the environment using the following commands: On Linux and macOS, use the following:
source myenv/bin/activate
On Windows, use the following:
myenv\bin\activate
- Create a list of all packages in the environment with the following command:
pip3 freeze > requirements.txt
- Remove all packages installed in the environment with the following command:
pip3 uninstall -y -r requirements.txt
- The
-y
switch preventspip
from asking for confirmation to uninstall each individual package. Next, deactivate the environment by running the following command:
deactivate
- Finally, remove the environment directory with the following commands:
On Linux and macOS use the following:
rm -rf myenv
On Windows use the following:
rmdir /s /q myenv
Note
It is important to uninstall all packages using pip3
because some packages will copy files in directories outside of the environment directory. Typically, these directories are:
On Linux and Windows: The .local
folder located in the user's home folder.
On macOS: /Users/username/Library/Python/x.x/bin
, where username
is the current user name and x.x
denotes the version of Python the environment is based on.