- Installing packages using pip and virtual environments¶
- Installing pip¶
- Installing virtualenv¶
- Creating a virtual environment¶
- Activating a virtual environment¶
- Installation#
- via pip#
- wheel#
- sdist#
- via zipapp#
- latest unreleased#
- Python and OS Compatibility#
- Linux#
- macOS#
- Windows#
- Packaging variants#
- Installation#
- via pip#
- wheel#
- sdist#
- via zipapp#
- latest unreleased#
- Python and OS Compatibility#
- Linux#
- macOS#
- Windows#
- Packaging variants#
Installing packages using pip and virtual environments¶
This guide discusses how to install packages using pip and a virtual environment manager: either venv for Python 3 or virtualenv for Python 2. These are the lowest-level tools for managing Python packages and are recommended if higher-level tools do not suit your needs.
This doc uses the term package to refer to a Distribution Package which is different from an Import Package that which is used to import modules in your Python source code.
Installing pip¶
pip is the reference Python package manager. It’s used to install and update packages. You’ll need to make sure you have the latest version of pip installed.
Debian and most other distributions include a python-pip package; if you want to use the Linux distribution-provided versions of pip, see Installing pip/setuptools/wheel with Linux Package Managers .
You can also install pip yourself to ensure you have the latest version. It’s recommended to use the system pip to bootstrap a user installation of pip:
python3 -m pip install --user --upgrade pip python3 -m pip --version
Afterwards, you should have the latest version of pip installed in your user site:
pip 21.1.3 from $HOME/.local/lib/python3.9/site-packages (python 3.9)
The Python installers for Windows include pip. You can make sure that pip is up-to-date by running:
py -m pip install --upgrade pip py -m pip --version
Afterwards, you should have the latest version of pip:
pip 21.1.3 from c:\python39\lib\site-packages (Python 3.9.4)
Installing virtualenv¶
If you are using Python 3.3 or newer, the venv module is the preferred way to create and manage virtual environments. venv is included in the Python standard library and requires no additional installation. If you are using venv, you may skip this section.
virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.
python3 -m pip install --user virtualenv
py -m pip install --user virtualenv
Creating a virtual environment¶
venv (for Python 3) and virtualenv (for Python 2) allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtual environment while developing Python applications.
To create a virtual environment, go to your project’s directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands.
The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env .
venv will create a virtual Python installation in the env folder.
You should exclude your virtual environment directory from your version control system using .gitignore or similar.
Activating a virtual environment¶
Before you can start installing or using packages in your virtual environment you’ll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s PATH .
You can confirm you’re in the virtual environment by checking the location of your Python interpreter:
Installation#
virtualenv is a CLI tool that needs a Python interpreter to run. If you already have a Python 3.5+ interpreter the best is to use pipx to install virtualenv into an isolated environment. This has the added benefit that later you’ll be able to upgrade virtualenv without affecting other parts of the system.
pipx install virtualenv virtualenv --help
via pip#
Alternatively you can install it within the global Python interpreter itself (perhaps as a user package via the —user flag). Be cautious if you are using a python install that is managed by your operating system or another package manager. pip might not coordinate with those tools, and may leave your system in an inconsistent state. Note, if you go down this path you need to ensure pip is new enough per the subsections below:
python -m pip install --user virtualenv python -m virtualenv --help
wheel#
Installing virtualenv via a wheel (default with pip) requires an installer that can understand the python-requires tag (see PEP-503), with pip this is version 9.0.0 (released 2016 November). Furthermore, in case you’re not installing it via the PyPi you need to be using a mirror that correctly forwards the python-requires tag (notably the OpenStack mirrors don’t do this, or older devpi versions — added with version 4.7.0 ).
sdist#
When installing via a source distribution you need an installer that handles the PEP-517 specification. In case of pip this is version 18.0.0 or later (released on 2018 July). If you cannot upgrade your pip to support this you need to ensure that the build requirements from pyproject.toml are satisfied before triggering the install.
via zipapp#
You can use virtualenv without installing it too. We publish a Python zipapp, you can just download this from https://bootstrap.pypa.io/virtualenv.pyz and invoke this package with a python interpreter:
python virtualenv.pyz --help
The root level zipapp is always the current latest release. To get the last supported zipapp against a given python minor release use the link https://bootstrap.pypa.io/virtualenv/x.y/virtualenv.pyz , e.g. for the last virtualenv supporting Python 2.7 use https://bootstrap.pypa.io/virtualenv/2.7/virtualenv.pyz. If you are looking for past version of virtualenv.pyz they are available here: https://github.com/pypa/get-virtualenv/blob//public//virtualenv.pyz?raw=true
latest unreleased#
Installing an unreleased version is discouraged and should be only done for testing purposes. If you do so you’ll need a pip version of at least 18.0.0 and use the following command:
pip install git+https://github.com/pypa/virtualenv.git@main
Python and OS Compatibility#
This means virtualenv works on the latest patch version of each of these minor versions. Previous patch versions are supported on a best effort approach.
CPython is shipped in multiple forms, and each OS repackages it, often applying some customization along the way. Therefore we cannot say universally that we support all platforms, but rather specify some we test against. In case of ones not specified here the support is unknown, though likely will work. If you find some cases please open a feature request on our issue tracker. Note, as of 20.18.0 we no longer support running under Python less than 3.7, however we still support creating environments for 2.7 and 3.5.
Linux#
- installations from python.org
- Ubuntu 16.04+ (both upstream and deadsnakes builds)
- Fedora
- RHEL and CentOS
- OpenSuse
- Arch Linux
macOS#
In case of macOS we support:
- installations from python.org
- python versions installed via brew (both older python2.7 and python3)
- Python 3 part of XCode (Python framework — /Library/Frameworks/Python3.framework/ )
- Python 2 part of the OS ( /System/Library/Frameworks/Python.framework/Versions/ )
Windows#
Packaging variants#
- Normal variant (file structure as comes from python.org).
- We support CPython 2 system installations that do not contain the python files for the standard library if the respective compiled files are present (e.g. only os.pyc , not os.py ). This can be used by custom systems may want to maximize available storage or obfuscate source code by removing .py files.
Installation#
virtualenv is a CLI tool that needs a Python interpreter to run. If you already have a Python 3.5+ interpreter the best is to use pipx to install virtualenv into an isolated environment. This has the added benefit that later you’ll be able to upgrade virtualenv without affecting other parts of the system.
pipx install virtualenv virtualenv --help
via pip#
Alternatively you can install it within the global Python interpreter itself (perhaps as a user package via the —user flag). Be cautious if you are using a python install that is managed by your operating system or another package manager. pip might not coordinate with those tools, and may leave your system in an inconsistent state. Note, if you go down this path you need to ensure pip is new enough per the subsections below:
python -m pip install --user virtualenv python -m virtualenv --help
wheel#
Installing virtualenv via a wheel (default with pip) requires an installer that can understand the python-requires tag (see PEP-503), with pip this is version 9.0.0 (released 2016 November). Furthermore, in case you’re not installing it via the PyPi you need to be using a mirror that correctly forwards the python-requires tag (notably the OpenStack mirrors don’t do this, or older devpi versions — added with version 4.7.0 ).
sdist#
When installing via a source distribution you need an installer that handles the PEP-517 specification. In case of pip this is version 18.0.0 or later (released on 2018 July). If you cannot upgrade your pip to support this you need to ensure that the build requirements from pyproject.toml are satisfied before triggering the install.
via zipapp#
You can use virtualenv without installing it too. We publish a Python zipapp, you can just download this from https://bootstrap.pypa.io/virtualenv.pyz and invoke this package with a python interpreter:
python virtualenv.pyz --help
The root level zipapp is always the current latest release. To get the last supported zipapp against a given python minor release use the link https://bootstrap.pypa.io/virtualenv/x.y/virtualenv.pyz , e.g. for the last virtualenv supporting Python 2.7 use https://bootstrap.pypa.io/virtualenv/2.7/virtualenv.pyz. If you are looking for past version of virtualenv.pyz they are available here: https://github.com/pypa/get-virtualenv/blob//public//virtualenv.pyz?raw=true
latest unreleased#
Installing an unreleased version is discouraged and should be only done for testing purposes. If you do so you’ll need a pip version of at least 18.0.0 and use the following command:
pip install git+https://github.com/pypa/virtualenv.git@main
Python and OS Compatibility#
This means virtualenv works on the latest patch version of each of these minor versions. Previous patch versions are supported on a best effort approach.
CPython is shipped in multiple forms, and each OS repackages it, often applying some customization along the way. Therefore we cannot say universally that we support all platforms, but rather specify some we test against. In case of ones not specified here the support is unknown, though likely will work. If you find some cases please open a feature request on our issue tracker. Note, as of 20.18.0 we no longer support running under Python less than 3.7, however we still support creating environments for 2.7 and 3.5.
Linux#
- installations from python.org
- Ubuntu 16.04+ (both upstream and deadsnakes builds)
- Fedora
- RHEL and CentOS
- OpenSuse
- Arch Linux
macOS#
In case of macOS we support:
- installations from python.org
- python versions installed via brew (both older python2.7 and python3)
- Python 3 part of XCode (Python framework — /Library/Frameworks/Python3.framework/ )
- Python 2 part of the OS ( /System/Library/Frameworks/Python.framework/Versions/ )
Windows#
Packaging variants#
- Normal variant (file structure as comes from python.org).
- We support CPython 2 system installations that do not contain the python files for the standard library if the respective compiled files are present (e.g. only os.pyc , not os.py ). This can be used by custom systems may want to maximize available storage or obfuscate source code by removing .py files.