This guide discusses how to create and activate a virtual environment usingthe standard library’s virtual environment tool venv and install packages.The guide covers how to:
Create and activate a virtual environment
Prepare pip
Install packages into a virtual environment using the
pip
commandUse and create a requirements file
Note
This guide applies to supported versions of Python, currently 3.8and higher.
Note
This guide uses the term package to refer to aDistribution Package, which commonly is installed from an externalhost. This differs from the term Import Package which refers toimport modules in your Python source code.
Important
This guide has the prerequisite that you are using an official Python version obtained from<https://www.python.org/downloads/>. If you are using your operatingsystem’s package manager to install Python, please ensure that Python isinstalled before proceeding with these steps.
Create and Use Virtual Environments#
Create a new virtual environment#
venv (for Python 3) allows you to manage separate package installations fordifferent projects. It creates a “virtual” isolated Python installation. Whenyou switch projects, you can create a new virtual environment which is isolatedfrom other virtual environments. You benefit from the virtual environmentsince packages can be installed confidently and will not interfere withanother project’s environment.
Tip
It is recommended to use a virtual environment when working with thirdparty packages.
To create a virtual environment, go to your project’s directory and run thefollowing command. This will create a new virtual environment in a local foldernamed .venv
:
python3 -m venv .venv
py -m venv .venv
The second argument is the location to create the virtual environment. Generally, youcan just create this in your project and call it .venv
.
venv
will create a virtual Python installation in the .venv
folder.
Note
You should exclude your virtual environment directory from your versioncontrol system using .gitignore
or similar.
Activate a virtual environment#
Before you can start installing or using packages in your virtual environment you’llneed to activate
it. Activating a virtual environment will put thevirtual environment-specific python
and pip
executables into yourshell’s PATH
.
source .venv/bin/activate
.venv\Scripts\activate
To confirm the virtual environment is activated, check the location of yourPython interpreter:
which python
where python
While the virtual environment is active, the above command will output afilepath that includes the .venv
directory, by ending with the following:
.venv\Scripts\python
While a virtual environment is activated, pip will install packages into thatspecific environment. This enables you to import and use packages in yourPython application.
Deactivate a virtual environment#
If you want to switch projects or leave your virtual environment,deactivate
the environment:
deactivate
Note
Closing your shell will deactivate the virtual environment. Ifyou open a new shell window and want to use the virtual environment,reactivate it.
Reactivate a virtual environment#
If you want to reactivate an existing virtual environment, follow the sameinstructions about activating a virtual environment. There’s no need to createa new virtual environment.
Prepare pip#
pip is the reference Python package manager.It’s used to install and update packages into a virtual environment.
The Python installers for macOS include pip. On Linux, you may have to installan additional package such as python3-pip
. You can make sure that pip isup-to-date by running:
python3 -m pip install --upgrade pippython3 -m pip --version
Afterwards, you should have the latest version of pip installed in youruser site:
pip 23.3.1 from .../.venv/lib/python3.9/site-packages (python 3.9)
The Python installers for Windows include pip. You can make sure that pip isup-to-date by running:
py -m pip install --upgrade pippy -m pip --version
Afterwards, you should have the latest version of pip:
pip 23.3.1 from .venv\lib\site-packages (Python 3.9.4)
Install packages using pip#
When your virtual environment is activated, you can install packages. Use thepip install
command to install packages.
Install a package#
For example,let’s install theRequests library from the Python Package Index (PyPI):
python3 -m pip install requests
py -m pip install requests
pip should download requests and all of its dependencies and install them:
Collecting requests Using cached requests-2.18.4-py2.py3-none-any.whlCollecting chardet<3.1.0,>=3.0.2 (from requests) Using cached chardet-3.0.4-py2.py3-none-any.whlCollecting urllib3<1.23,>=1.21.1 (from requests) Using cached urllib3-1.22-py2.py3-none-any.whlCollecting certifi>=2017.4.17 (from requests) Using cached certifi-2017.7.27.1-py2.py3-none-any.whlCollecting idna<2.7,>=2.5 (from requests) Using cached idna-2.6-py2.py3-none-any.whlInstalling collected packages: chardet, urllib3, certifi, idna, requestsSuccessfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
Install a specific package version#
pip allows you to specify which version of a package to install usingversion specifiers. For example, to installa specific version of requests
:
python3 -m pip install 'requests==2.18.4'
py -m pip install "requests==2.18.4"
To install the latest 2.x
release of requests:
python3 -m pip install 'requests>=2.0.0,<3.0.0'
py -m pip install "requests>=2.0.0,<3.0.0"
To install pre-release versions of packages, use the --pre
flag:
python3 -m pip install --pre requests
py -m pip install --pre requests
Install extras#
Some packages have optional extras. You can tell pip to install these byspecifying the extra in brackets:
python3 -m pip install 'requests[security]'
py -m pip install "requests[security]"
Install a package from source#
pip can install a package directly from its source code. For example, to installthe source code in the google-auth
directory:
cd google-authpython3 -m pip install .
cd google-authpy -m pip install .
Additionally, pip can install packages from source indevelopment mode,meaning that changes to the source directory will immediately affect theinstalled package without needing to re-install:
python3 -m pip install --editable .
py -m pip install --editable .
Install from version control systems#
pip can install packages directly from their version control system. Forexample, you can install directly from a git repository:
google-auth @ git+https://github.com/GoogleCloudPlatform/google-auth-library-python.git
For more information on supported version control systems and syntax, see pip’sdocumentation on VCS Support.
Install from local archives#
If you have a local copy of a Distribution Package’s archive (a zip,wheel, or tar file) you can install it directly with pip:
python3 -m pip install requests-2.18.4.tar.gz
py -m pip install requests-2.18.4.tar.gz
If you have a directory containing archives of multiple packages, you can tellpip to look for packages there and not to use thePython Package Index (PyPI) at all:
python3 -m pip install --no-index --find-links=/local/dir/ requests
py -m pip install --no-index --find-links=/local/dir/ requests
This is useful if you are installing packages on a system with limitedconnectivity or if you want to strictly control the origin of distributionpackages.
Install from other package indexes#
If you want to download packages from a different index than thePython Package Index (PyPI), you can use the --index-url
flag:
python3 -m pip install --index-url http://index.example.com/simple/ SomeProject
py -m pip install --index-url http://index.example.com/simple/ SomeProject
If you want to allow packages from both the Python Package Index (PyPI)and a separate index, you can use the --extra-index-url
flag instead:
python3 -m pip install --extra-index-url http://index.example.com/simple/ SomeProject
py -m pip install --extra-index-url http://index.example.com/simple/ SomeProject
Upgrading packages#
pip can upgrade packages in-place using the --upgrade
flag. For example, toinstall the latest version of requests
and all of its dependencies:
python3 -m pip install --upgrade requests
py -m pip install --upgrade requests
Using a requirements file#
Instead of installing packages individually, pip allows you to declare alldependencies in a Requirements File. Forexample you could create a requirements.txt
file containing:
requests==2.18.4google-auth==1.1.0
And tell pip to install all of the packages in this file using the -r
flag:
python3 -m pip install -r requirements.txt
py -m pip install -r requirements.txt
Freezing dependencies#
Pip can export a list of all installed packages and their versions using thefreeze
command:
python3 -m pip freeze
py -m pip freeze
Which will output a list of package specifiers such as:
cachetools==2.0.1certifi==2017.7.27.1chardet==3.0.4google-auth==1.1.1idna==2.6pyasn1==0.3.6pyasn1-modules==0.1.4requests==2.18.4rsa==3.4.2six==1.11.0urllib3==1.22
The pip freeze
command is useful for creating Requirements Filesthat can re-create the exact versions of all packages installed in an environment.