Srikanth Technologies

How to install Python libraries in offline mode

Here are the steps you have to take to install Python libraries from pypi.org on a system that is not connected to Internet (offline).

It is easy to use PIP to install libraries when you are connected to Internet. However, when a system is not at all connected to Internet, how do we install required libraries into it.

Here are the steps to install Python libraries to offline system:

Take the following steps on a system that is connected to Internet.

Create requirements.txt file with names and versions of libraries that are to be downloaded.

If you want to create requirements file automatically with all libraries installed on the current system, use the following command.

pip freeze > requirements.txt

The above command creates requirements.txt (or any other name can be used) file with the following contents. Each line specifies the name of the library and its version.

beautifulsoup4==4.11.1
boto3==1.24.80
botocore==1.27.80
certifi==2021.10.8
charset-normalizer==2.0.12
et-xmlfile==1.1.0
idna==3.3
jmespath==1.0.1
lxml==4.8.0
openpyxl==3.1.1
python-dateutil==2.8.2
requests==2.28.0
s3transfer==0.6.0
six==1.16.0
soupsieve==2.3.2.post1
urllib3==1.26.9

Now modify requirements.txt as required.

Remove some entries and add new libraries using the format library==version

Here is modified requirements.txt file as I just want to install only 3 libraries.

beautifulsoup4==4.11.1
openpyxl==3.1.1
requests==2.28.0

Use the following command to download all libraries mentioned in requirements.txt file into a libraries folder:

C:\dev\python>pip download -r requirements.txt -d libraries 
Collecting beautifulsoup4==4.11.1
  Using cached beautifulsoup4-4.11.1-py3-none-any.whl (128 kB)
Collecting openpyxl==3.1.1
  Using cached openpyxl-3.1.1-py2.py3-none-any.whl (249 kB)
Collecting requests==2.28.0
  Using cached requests-2.28.0-py3-none-any.whl (62 kB)
Collecting soupsieve>1.2
  Using cached soupsieve-2.4-py3-none-any.whl (37 kB)
Collecting et-xmlfile
  Using cached et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
Collecting charset-normalizer~=2.0.0
  Using cached charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Collecting idna<4,>=2.5
  Using cached idna-3.4-py3-none-any.whl (61 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached urllib3-1.26.15-py2.py3-none-any.whl (140 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2022.12.7-py3-none-any.whl (155 kB)
Saved c:\dev\python\libraries\beautifulsoup4-4.11.1-py3-none-any.whl
Saved c:\dev\python\libraries\openpyxl-3.1.1-py2.py3-none-any.whl
Saved c:\dev\python\libraries\requests-2.28.0-py3-none-any.whl
Saved c:\dev\python\libraries\certifi-2022.12.7-py3-none-any.whl
Saved c:\dev\python\libraries\charset_normalizer-2.0.12-py3-none-any.whl
Saved c:\dev\python\libraries\idna-3.4-py3-none-any.whl
Saved c:\dev\python\libraries\soupsieve-2.4-py3-none-any.whl
Saved c:\dev\python\libraries\urllib3-1.26.15-py2.py3-none-any.whl
Saved c:\dev\python\libraries\et_xmlfile-1.1.0-py3-none-any.whl
Successfully downloaded beautifulsoup4 openpyxl requests certifi charset-normalizer idna soupsieve urllib3 et-xmlfile
As you can see from the above screenshot, it downloads dependencies also.

Though we mentioned only requests library, it also downloaded all its dependencies like certifi, charset, idna etc.

Now copy libraries folder and requirements.txt file to target system, which is not connected to Internet.

Here are the steps to be taken on a system that is not connected to Internet but that has libraries folder and requirements.txt file.

We must install Python into target system, if not already installed.

Go to command line.

Give the following command to install all libraries mentioned in requirements.txt

C:\python>pip install -r requirements.txt --no-index --find-links libraries
Looking in links: libraries
Processing c:\python\libraries\beautifulsoup4-4.11.1-py3-none-any.whl
Processing c:\python\libraries\openpyxl-3.1.1-py2.py3-none-any.whl
Processing c:\python\libraries\requests-2.28.0-py3-none-any.whl
Processing c:\python\libraries\soupsieve-2.4-py3-none-any.whl
Processing c:\python\libraries\et_xmlfile-1.1.0-py3-none-any.whl
Processing c:\python\libraries\charset_normalizer-2.0.12-py3-none-any.whl
Processing c:\python\libraries\idna-3.4-py3-none-any.whl
Processing c:\python\libraries\urllib3-1.26.15-py2.py3-none-any.whl
Processing c:\python\libraries\certifi-2022.12.7-py3-none-any.whl
Installing collected packages: urllib3, soupsieve, idna, et-xmlfile, charset-normalizer, certifi, requests, openpyxl, beautifulsoup4
Successfully installed beautifulsoup4-4.11.1 certifi-2022.12.7 charset-normalizer-2.0.12 et-xmlfile-1.1.0 idna-3.4 openpyxl-3.1.1 requests-2.28.0 soupsieve-2.4 urllib3-1.26.15

The above command will install all libraries found in requirements.txt by looking for corresponding .whl file in libraries folder.

Option -r specifies requirements filename.

Option --no-index is to ignore package index (only looking at --find-links URLs instead).

Option --find-links specifies where to look for links to archives such as sdist (.tar.gz) or wheel (.whl) files.

Now we are ready to use those libraries on the target system.

In this blog, I showed steps necessary to install Python libraries on a system that is not connected to Internet.

All the best!

Srikanth Pragada