Python 3 Virtual Environment Tips

Tips for creating and using virtual environments in Python 3.

Create a virtual environment

Create a virtual environment with the current Python3 version.

python3 -m venv <directory>

This will create a virtual environment for the given directory.

Example:

python3 -m venv venv
Activate a virtual environment

If you created a virtual environment from the Create a virtual environment snippet, you can activate a virtual environment for the created virtual environment directory.

source <virtual_environment_directory>/bin/activate

Example:

source venv/bin/activate
Deactivate a virtual environment

Use the deactivate command to exit the virtual environment.

deactivate
References

Author: rfmeier

Product developer at WPEngine and runner from Schaumburg, Illinois. I work with php and Python; Spending most of my time digging into source code to see how it all works.