
Introduction
Python applications often require packages and modules beyond the standard library. Different applications may need different versions of the same library – one application might require version 1.0 of a module while another needs version 2.0.
This creates a problem: a single Python installation can’t satisfy conflicting requirements. Installing either version 1.0 or 2.0 would prevent one of your applications from running properly.
Virtual environments solve this problem by creating isolated directory trees, each containing a Python installation and its own set of packages. This way, different applications can use different environments. Application A can have a virtual environment with version 1.0 installed, while Application B uses another environment with version 2.0. If Application B later requires an upgrade to version 3.0, Application A’s environment remains unaffected.
Creating Virtual Environments
The venv
module is used to create and manage virtual environments. It will install the Python version from which you run the command (as shown by the --version
option). For example, running the command with python3.12
will install Python 3.12 in the environment.
To create a virtual environment, choose a directory location and run the venv
module:
python -m venv tutorial-env
This creates the tutorial-env
directory (if it doesn’t exist) and populates it with a Python interpreter and supporting files.
A popular directory name for virtual environments is .venv
. This keeps the directory hidden in your shell and explains its purpose. It also avoids conflicts with .env
environment variable definition files supported by some tools.
Activating Virtual Environments
After creating a virtual environment, you need to activate it:
On Windows:
tutorial-env\Scripts\activate
On Unix or MacOS:
source tutorial-env/bin/activate
(The script above is for the bash shell. For csh or fish shells, use activate.csh
or activate.fish
instead.)
Activating the environment changes your shell prompt to show which environment you’re using and modifies the system path so that running python
uses your virtual environment’s Python interpreter:
$ source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May 6 2016, 10:59:36)
...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python35.zip', ...,
'~/envs/tutorial-env/lib/python3.5/site-packages']
>>>
To exit the virtual environment, simply type:
deactivate
Managing Packages with pip
You can install, upgrade, and remove packages using pip
. By default, pip installs packages from the Python Package Index (PyPI), which you can browse at https://pypi.org.
Installing Packages
To install the latest version of a package:
(tutorial-env) $ python -m pip install novas
To install a specific version:
(tutorial-env) $ python -m pip install requests==2.6.0
If you run this command again, pip will notice the requested version is already installed. To upgrade to the latest version:
(tutorial-env) $ python -m pip install --upgrade requests
Removing Packages
To remove packages:
python -m pip uninstall package_name
Package Information
To display information about a package:
(tutorial-env) $ python -m pip show requests
To list all installed packages:
(tutorial-env) $ python -m pip list
Requirements Files
The pip freeze
command outputs installed packages in a format that pip can use for installation:
(tutorial-env) $ python -m pip freeze > requirements.txt
This creates a requirements.txt
file that can be committed to version control and distributed with your application. Others can then install all necessary packages with:
(tutorial-env) $ python -m pip install -r requirements.txt
Further Resources
Pip has many more options. For complete documentation, refer to the Installing Python Modules guide. If you want to make your own package available on the Python Package Index, consult the Python Packaging User Guide.