How to install Python packages offline

Published on February 26, 2023

This article shows how to install a Python package if your computer is not connected to the Internet or access to pypi.org is blocked.

Python pip install package

What is a Python package?

A Python package is an essential building block in Python programming. It is basically a folder or nested folders with modules that perform specific tasks, thereby making it more easy for you to focus on what your code should do. In short, it is reusable code written by someone else for your benefit.

Python has many core packages installed when you install Python.

If you want to install a Python package that does not come with the installed version built-in, you will have to manually install that package.

For example, if you want to install Flask, you would do this:

pip install flask

How to install packages using pip and requirements.txt

If you have a Python application and will install packages contained in requirements.txt, you will use this command:

pip install -r requirements.txt

Suppose this is the content of your requirements.txt file.

Flask
sqlalchemy
flask_sqlalchemy

pip will download each of the packages in requirements.txt line by line. It is possible for a package to have other nested dependencies and they will all get downloaded using pip.

For this to work correctly, you will need to have a cached version of the packages or an active Internet connection.

What if pip cannot download from pypi.org?

Unfortunately, if pypi.org is blocked by the firewall (happens with a few companies) or your computer simply has no Internet connection, you will have to manually download each package from another computer that is connected to the Internet and can connect to pypi.org.

Download packages using another computer

To download the packages, run this command on the second computer that can connect to the Internet.

pip download -r requirements.txt

When you run that, each package will download as a .whl file. The dependencies may also download as .whl files.

Then, scp these whl files to the first machine or copy them to a flash drive and copy them to the first machine, to the /tmp/ directory.

Install packages into Anaconda or Miniconda3 site-packages

Our next step is to stage these .whl files to the site-packages directory in Miniconda or Anaconda. We have Python 3.11 installed under Miniconda3.

Run this command:

cp /tmp/pip/wheel/*.whl /home/arul/miniconda3/lib/python3.11/site-packages

Don't forget to change the username arul to your username.

If you have another version of Python, change python3.11 to that appropriate directory.

Now, all the whl files are in /home/arul/miniconda3/lib/python3.11/site-packages

Set the global.target pip config variable

Set the global.target pip config variable. This makes sure that the package installation happens in the correct directory.

pip config set global.target /home/arul/miniconda3/lib/python3.11/site-packages

Install each package manually

Install each package manually. I was successful only when I specified the path fully in the --find-links option.

pip install --upgrade --no-index --find-links=/home/arul/miniconda3/lib/python3.11/site-packages Flask==2.2.3

Repeat the process for each package that you want to install and whose .wml file you have in site-packages directory.

Conclusion

I am sure there may be an easier way, but for my setup and environment, this was the only successful sequence of steps. I got all the packages to install properly and was able to proceed with my program. YMMV.

TL;DR

I was not able to install Python packages. This blog post contains steps on how I was able to manuallyh download the Python wml package wml files from another computer, copy them over to this computer and manually install each package to the Miniconda3 site-packages directory.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on February 26, 2023