- Requirements File Format#
- Example#
- Structure#
- Encoding#
- Line continuations#
- Comments#
- Supported options#
- Global options#
- Per-requirement options#
- Referring to other requirements files#
- Using environment variables#
- Influencing the build system#
- Installing packages using pip and virtual environments¶
- Installing pip¶
- Installing virtualenv¶
- Creating a virtual environment¶
- Activating a virtual environment¶
Requirements File Format#
Requirements files serve as a list of items to be installed by pip, when using pip install . Files that use this format are often called “pip requirements.txt files”, since requirements.txt is usually what these files are named (although, that is not a requirement).
Note The requirements file format is closely tied to a number of internal details of pip (e.g., pip’s command line options). The basic format is relatively stable and portable but the full syntax, as described here, is only intended for consumption by pip, and other tools should take that into account before using it for their own purposes.
Example#
# This is a comment, to show how #-prefixed lines are ignored. # It is possible to specify requirements as plain names. pytest pytest-cov beautifulsoup4 # The syntax supported here is the same as that of requirement specifiers. docopt == 0.6.1 requests [security] >= 2.8.1, == 2.8.* ; python_version "2.7" urllib3 @ https://github.com/urllib3/urllib3/archive/refs/tags/1.26.8.zip # It is possible to refer to other requirement files or constraints files. -r other-requirements.txt -c constraints.txt # It is possible to refer to specific local distribution paths. ./downloads/numpy-1.9.2-cp34-none-win32.whl # It is possible to refer to URLs. http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl
Structure#
For details on requirement specifiers, see Requirement Specifiers . For examples of all these forms, see Examples .
Encoding#
Requirements files are utf-8 encoding by default and also support PEP 263 style comments to change the encoding (i.e. # -*- coding: -*- ).
Line continuations#
A line ending in an unescaped \ is treated as a line continuation and the newline following it is effectively ignored.
Comments#
A line that begins with # is treated as a comment and ignored. Whitespace followed by a # causes the # and the remainder of the line to be treated as a comment.
Comments are stripped after line continuations are processed.
Supported options#
Requirements files only supports certain pip install options, which are listed below.
Global options#
The following options have an effect on the entire pip install run, and must be specified on their individual lines.
- -i, —index-url
- —extra-index-url
- —no-index
- -c, —constraint
- -r, —requirement
- -e, —editable
- -f, —find-links
- —no-binary
- —only-binary
- —prefer-binary
- —require-hashes
- —pre
- —trusted-host
- —use-feature
To specify —pre , —no-index and two —find-links locations:
--pre --no-index --find-links /my/local/archives --find-links http://some.archives.com/archives
Per-requirement options#
The options which can be applied to individual requirements are:
Referring to other requirements files#
If you wish, you can refer to other requirements files, like this:
You can also refer to constraints files , like this:
Using environment variables#
pip supports the use of environment variables inside the requirements file.
You have to use the POSIX format for variable names including brackets around the uppercase name as shown in this example: $ . pip will attempt to find the corresponding environment variable defined on the host system at runtime.
There is no support for other variable expansion syntaxes such as $VARIABLE and %VARIABLE% .
You can now store sensitive data (tokens, keys, etc.) in environment variables and only specify the variable name for your requirements, letting pip lookup the value at runtime. This approach aligns with the commonly used 12-factor configuration pattern.
Influencing the build system#
This disables the use of wheels (cached or otherwise). This could mean that builds will be slower, less deterministic, less reliable and may not behave correctly upon installation.
This mechanism is only preserved for backwards compatibility and should be considered deprecated. A future release of pip may drop these options.
The —global-option option is used to pass options to setup.py .
These options are highly coupled with how pip invokes setuptools using the setup.py (legacy) build system interface. It is not compatible with newer pyproject.toml build system interface.
This is will not work with other build-backends or newer setup.cfg-only projects.
If you have a declaration like:
FooProject >= 1.2 --global-option="--no-user-cfg"
The above translates roughly into running FooProject’s setup.py script as:
python setup.py --no-user-cfg install
Note that the only way of giving more than one option to setup.py is through multiple —global-option options.
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: