Skip to content

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.

Dependencies#

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

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.

Using 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.

Operating 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.

Specifying 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 list --format=json | jq -r '.[] | .name' > requirements.txt

Verify the contents of requirements.txt look correct with:

Terminal
$ cat requirements.txt
Example requirements.txt
cat requirements.txt
absl-py
astunparse
brewer2mpl
cachetools
certifi
charset-normalizer
contourpy
cycler
flatbuffers
fonttools
gast
ggplot
google-auth
google-auth-oauthlib
google-pasta
grpcio
h5py
idna
keras
kiwisolver
libclang
Markdown
MarkupSafe
matplotlib
ml-dtypes
numpy
oauthlib
opt-einsum
packaging
pandas
patsy
pillow
pip
protobuf
pyasn1
pyasn1-modules
pyparsing
python-dateutil
pytz
requests
requests-oauthlib
rsa
scipy
setuptools
six
statsmodels
tensorboard
tensorboard-data-server
tensorflow
tensorflow-estimator
tensorflow-io-gcs-filesystem
termcolor
typing_extensions
tzdata
urllib3
Werkzeug
wheel
wrapt