Python Virtual Environments
A Python virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
Using virtual environments is a best practice for Python development. It allows you to:
- Isolate project dependencies: Each project can have its own set of dependencies, which prevents conflicts between projects.
- Avoid polluting the global Python installation: You can install packages without affecting your system-wide Python installation.
- Ensure reproducibility: You can easily recreate the same environment on different machines.
In this tutorial, you’ll learn how to use two popular tools for creating and managing virtual environments: venv and pipenv.
Using venv
venv is a module that’s included in the Python standard library (starting from Python 3.3). It allows you to create lightweight virtual environments.
Creating a Virtual Environment
To create a virtual environment with venv, open a terminal and run the following command:
python -m venv my-envThis will create a new directory called my-env that contains the virtual environment.
Activating a Virtual Environment
To activate the virtual environment, you need to run the activation script.
On Windows:
my-env\Scripts\activateOn macOS and Linux:
source my-env/bin/activateOnce the virtual environment is activated, your shell prompt will be prefixed with the name of the virtual environment.
Installing Packages
You can install packages into the virtual environment using pip:
pip install requestsDeactivating a Virtual Environment
To deactivate the virtual environment, simply run the following command:
deactivateUsing pipenv
pipenv is a tool that aims to bring the best of all packaging worlds to the Python world. It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages.
Installing pipenv
You can install pipenv using pip:
pip install pipenvCreating a Virtual Environment
To create a virtual environment with pipenv, navigate to your project directory and run the following command:
pipenv installThis will create a new virtual environment for your project and a Pipfile to manage your dependencies.
Activating a Virtual Environment
To activate the virtual environment, run the following command:
pipenv shellInstalling Packages
You can install packages with pipenv like this:
pipenv install requestsThis will install the requests package and add it to your Pipfile.
Deactivating a Virtual Environment
To deactivate the virtual environment, simply type exit in your shell.
venv vs. pipenv
| Feature | venv | pipenv |
|---|---|---|
| Included in Python | Yes (3.3+) | No |
| Dependency Management | requirements.txt | Pipfile and Pipfile.lock |
| Automatic Virtualenv Creation | No | Yes |
| Dependency Resolution | Basic | Advanced |
For more information on virtual environments, you can refer to the official Python documentation: https://docs.python.org/3/library/venv.html
You can also learn more about pipenv from its official documentation: https://pipenv.pypa.io/en/latest/