Python - Download and Install Packages with Dependencies Offline
In this note, I’ve summarized how you can download Python packages and quickly install them offline on other machines.
The note is written for Linux but is fairly easy to apply to Windows.
Make sure that the same Python version is installed on all machines. I tested this with Python version 3.10 and 3.11.
Also pip is the Python package manager. It’s used to install and update packages (into a virtual environment).
The Python installers for macOS and Windows include pip. On Linux, you may have to install an additional package such as python3-pip
. You can make sure that pip is up-to-date by running:
# use the py command instead of python3 for Windows
python3 -m pip install --upgrade pip
python3 -m pip --version
Download Packages
In this example, I’m going to create a requirements file and download the following packages:
- Go to the folder where you want to download the packages and create
requirements.txt
:sudo nano requirements.txt
- In this case add the following packages to
requirements.txt
:Jinja2>=2.5.5 numpy
Jinja2
is used as an example to indicate that it must be above a certain version, an exact version is also possible:Jinja2==2.5.5
- Exit Nano (CTRL-X) and save the changes.
- Run the following command to download the packages and dependencies:
pip download -r requirements.txt # or download to a specific folder with: # pip download <DIR> -r requirements.txt
Now the packages are downloaded, also notice that MarkupSafe
is downloaded as dependency of Ninja2
. The Python version (in my case 3.11) is often mentioned in the filenames like cp311
.
Install Packages Offline
Without Virtual Environment
- Copy the folder with the packages (whl files) and requirements.txt to another machine with the same Python version
- Go to the folder and run the following command to install the packages:
pip install --no-index --find-links . -r requirements.txt # and if needed update already installed packages # pip install --no-index --find-links . -U -r requirements.txt
Now the packages are installed.
With Virtual Environment
In the case of a virtual environment with it’s own independent set of packages, the steps are:
- Create a virtual Python installation in the
my-venv
folder:python3 -m venv my-venv # or create the virtual environment with access to the system site-packages dir with: # python3 -m venv my-venv --system-site-packages
- 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-specificpython
andpip
executables into your shell’sPATH
:source my-venv/bin/activate # activate the virtual environment which python # verify if the virutal enviroment is activated, check the location of your Python interpreter # if you want to switch projects or leave your virtual environment, `deactivate` the environment with: # deactivate
- Install the packages:
pip install --no-index --find-links . -r requirements.txt
Now the packages are installed to the virtual environment.
Pip can export a list of all installed packages and their versions using the freeze
command:
pip freeze
The pip freeze
command is useful for creating requirements files that can re-create the exact versions of all packages installed in an environment.
Read other notes
Tags
Notes mentioning this note
There are no notes linking to this note.
Comments
No comments found for this note.
Join the discussion for this note on this ticket. Comments appear on this page instantly.