How to install Python packages and dependencies offline
Introduction
In this note, I’ve summarized how you can download Python packages and quickly install them offline on other machines. Although the instructions are written for Linux, they’re fairly easy to apply to Windows as well. Ensure that the same Python version is installed on all machines—I tested this with Python versions 3.10 and 3.11.
Requirements
The Python package manager, pip
, is used for installing and updating packages within a virtual environment.
The Python installers for macOS and Windows include pip. On Linux, you may need to install an additional package, such as python3-pip
. To ensure pip is up-to-date, you can run:
# Open your terminal application# Use the `py` command instead of `python3` for Windowspython3 -m pip install --upgrade pippython3 -m pip --version
How To
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, create
requirements.txt
and add the following packages:./requirements.txt # To edit use your text editor application, for example NanoJinja2>=2.5.5numpyJinja2
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
. -
Run the following command to download the packages and dependencies:
# Open your terminal applicationpip download -r requirements.txt# Or download to a specific folder with:# pip download <folder> -r requirements.txtNow the packages are downloaded, also notice that
MarkupSafe
is downloaded as dependency ofNinja2
. The Python version (in my case 3.11) is often mentioned in the filenames likecp311
.
Install packages offline
Without virtual environment
-
Copy the folder with the packages (
whl
files) andrequirements.txt
to another machine with the same Python version -
Go to the folder and run the following command to install the packages:
# Open your terminal applicationpip install --no-index --find-links . -r requirements.txt# And if needed update already installed packages:# pip install --no-index --find-links . -U -r requirements.txtNow 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:# Open your terminal applicationpython3 -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-specific
python
andpip
executables into your shell’s PATH:# Open your terminal application# Activate the virtual environmentsource my-venv/bin/activate# Verify if the virtual enviroment is activated, check the location of your Python interpreterwhich python# If you want to switch projects or leave your virtual environment, `deactivate` the environment with:# deactivate -
Install the packages:
# Open your terminal applicationpip install --no-index --find-links . -r requirements.txtNow, the packages are installed in the virtual environment.
No comments found for this note.
Join the discussion for this note on Github. Comments appear on this page instantly.