Python: 3 ways to Install Packages Offline (without Internet)

Updated: January 2, 2024 By: Guest Contributor Post a comment

Installing Python packages typically requires an active internet connection to download packages from a repository such as PyPI (Python Package Index). However, in situations without internet access, developers can still install Python packages offline by using various alternative methods.

Solution 1: Using Wheel Files

Wheel is a built-in binary package format used by Python allowing for faster installation without needing to build the source code. Installing packages using wheel files can be performed offline and is straightforward.

The steps:

  • Download the wheel file (.whl) for the desired package from PyPI or another source using a machine with internet access.
  • Transfer the wheel file to the offline target machine using a USB drive or other storage medium.
  • Use the Python package installer pip to install the wheel file.

Example:

pip install /path/to/package_name.whl

Advantages: Quick and simple installation. Does not require building the package.

Limitations: Dependency resolution is manually done. You need to ensure all dependencies are also downloaded (maybe some time in the past).

Solution 2: Using Source Distribution Files

This method involves installing packages from a tarball containing the source code. It is more challenging but useful if wheel files are not available.

Steps:

  • Download the package’s source distribution file (.tar.gz) from PyPI or other sources.
  • Copy the file to the offline environment.
  • Unpack the file and run the installation script.

Example:

tar xvzf package_name.tar.gz
cd package_name
python setup.py install

Advantages: Works when wheel files are not available. More control over build process.

Limitations: More complex. Requires all system dependencies to be present. Might not work for packages that cannot be compiled without internet.

Solution 3: Using Environment Export and Import

The third way is suitable if you need to replicate a complex environment. It leverages the capability to export an environment configuration and reproduce it elsewhere.

Steps:

  • Create and activate a virtual environment on a machine with internet access.
  • Install all required packages within this environment.
  • Export the environment to a ‘requirements.txt’ file.
  • Transfer the file and all downloaded packages to the target machine.
  • Recreate the environment using the requirements file.

Commands to run:

python -m venv myenv
source myenv/bin/activate
pip install necessary_packages
pip freeze > requirements.txt
# Transfer requirements.txt and myenv to offline machine
python -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt --no-index --find-links /path/to/offline_pkg_dir

Advantages: Effective for duplicating complex environments.

Limitations: Can be tedious for large numbers of packages. Requires accurate tracking of package dependencies.

Conclusion

Each of the offline installation methods has its own strengths and limitations, and the choice between them can be informed by the specific situation, requirements, and constraints. While using wheel files tends to be quicker and more manageable for beginners, source distribution offers more control and might be necessary when wheels are not available. Exporting and importing an environment is a robust solution for replicating existing setups but requires careful handling of dependencies. Ultimately, developers must weigh the trade-offs when working in offline environments to decide on the most practical approach to managing Python package installations.