Skip to content

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 Windows
python3 -m pip install --upgrade pip
python3 -m pip --version

How To

Download packages

In this example, I’m going to create a requirements file and download the following packages:

  1. 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 Nano
    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.

  2. Run the following command to download the packages and dependencies:

    # Open your terminal application
    pip download -r requirements.txt
    # Or download to a specific folder with:
    # pip download <folder> -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

  1. Copy the folder with the packages (whl files) and requirements.txt to another machine with the same Python version

  2. Go to the folder and run the following command to install the packages:

    # Open your terminal application
    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:

  1. Create a virtual Python installation in the my-venv folder:

    # Open your terminal application
    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
  2. 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:

    # Open your terminal application
    # Activate the virtual environment
    source my-venv/bin/activate
    # Verify if the virtual enviroment is activated, check the location of your Python interpreter
    which python
    # If you want to switch projects or leave your virtual environment, `deactivate` the environment with:
    # deactivate
  3. Install the packages:

    # Open your terminal application
    pip install --no-index --find-links . -r requirements.txt

    Now, the packages are installed in the virtual environment.

Favorites

Comments

    No comments found for this note.

    Join the discussion for this note on Github. Comments appear on this page instantly.

    Copyright 2021- Fiction Becomes Fact