Generating requirements.txt for Python packages and dependencies

Use the procedures below if you need to generate a requirements.txt file for air-gapped installs or Curated PyPI sources.

For an air-gapped install, the offline downloader’s get pypi resolves dependencies for you by default, so you can list only the packages you need. See Resolving dependencies.

Dependencies

To generate requirements.txt you will need Python (>=3.10 preferred; if lower than 3.10 see using virtualenv).

Installing a virtual environment

Using a virtual environment ensures that the generated requirements.txt does not interfere with or include any other Python packages that may be installed on the system. Virtual environments can be set up each time you generate a new dependency list.

NoteUsing different versions of Python

A virtual environment uses the same version of Python that is available on your path. If you need to use a different Python version, consider using a tool like pyenv or asdf.

NoteOperating system

It is recommended to use the target operating system of the users downloading these packages. Different operating systems can generate different requirements files.

Using venv

A virtual environment can be installed into a local directory with venv. venv comes installed by default with Python 3.10 and later. This is the recommended way to create a virtual environment:

Terminal
python -m venv .venv
source ./.venv/bin/activate

You can ensure the correct python binary is set on the $PATH by running:

Terminal
which python

Using virtualenv

If you don’t have Python 3.10 or later, you can install and use virtualenv.

To install virtualenv:

Terminal
python -m pip install virtualenv

To create and activate a new virtual environment with virtualenv:

Terminal
python -m virtualenv .venv
export PATH=$(pwd)/.venv/bin:$PATH

You can ensure the correct python binary is set on the $PATH by running:

Terminal
which python

Create the requirements.txt file

Now that your virtual environment is set, the first step is to install the package or packages needed.

For example, ggplot and tensorflow:

Terminal
python -m pip install ggplot tensorflow

This may take a while depending on which packages and how many packages you are installing.

NoteSpecifying versions and extras

Need to download specific versions or recommended extras? See pip documentation for package versions and extras.

Once it has finished installing, we can generate the requirements.txt file with:

Terminal
pip freeze > requirements.txt
ImportantRecord the versions you installed

pip freeze records the exact version of every package. A command that lists package names alone does not, and a package name with no version constraint means every version of that package ever published.

A requirements file is the complete list of what the repository serves, so it must cover every version its users need. One server often serves several teams, and Curated PyPI sources draw on the same air-gapped PyPI data. Combine the files those consumers already use rather than narrowing to a single environment.

Within that list, pin a package when you know the version you need, as pip freeze does. Use a range such as numpy>=1.24,<2.0 when users should be able to take later releases you have allowed. Omit the version only when you intend to carry that package’s full history.

Verify the contents of requirements.txt look correct with:

Terminal
cat requirements.txt
cat requirements.txt
absl-py==2.1.0
astunparse==1.6.3
brewer2mpl==1.4.1
cachetools==5.4.0
certifi==2024.7.4
charset-normalizer==3.3.2
contourpy==1.2.1
cycler==0.12.1
flatbuffers==24.3.25
fonttools==4.53.1
gast==0.6.0
ggplot==0.11.5
google-auth==2.32.0
google-auth-oauthlib==1.2.1
google-pasta==0.2.0
grpcio==1.65.1
h5py==3.11.0
idna==3.7
keras==3.4.1
kiwisolver==1.4.5
libclang==18.1.1
Markdown==3.6
MarkupSafe==2.1.5
matplotlib==3.9.1
ml-dtypes==0.4.0
numpy==1.26.4
oauthlib==3.2.2
opt-einsum==3.3.0
packaging==24.1
pandas==2.2.2
patsy==0.5.6
pillow==10.4.0
protobuf==4.25.4
pyasn1==0.6.0
pyasn1-modules==0.4.0
pyparsing==3.1.2
python-dateutil==2.9.0
pytz==2024.1
requests==2.32.3
requests-oauthlib==2.0.0
rsa==4.9
scipy==1.14.0
six==1.16.0
statsmodels==0.14.2
tensorboard==2.17.0
tensorboard-data-server==0.7.2
tensorflow==2.17.0
tensorflow-io-gcs-filesystem==0.37.1
termcolor==2.4.0
typing_extensions==4.12.2
tzdata==2024.1
urllib3==2.2.2
Werkzeug==3.0.3
wrapt==1.16.0
Back to top