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:
- Download libraries into a folder using pip download command from a system that is connected to Internet
- Copy the folder that contains libraries from the connected system to a folder on the system that is NOT connected to Internet
- Install libraries from that folder using pip install command with appropriate options
pip freeze > requirements.txtThe 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.1boto3==1.24.80botocore==1.27.80certifi==2021.10.8charset-normalizer==2.0.12et-xmlfile==1.1.0idna==3.3jmespath==1.0.1lxml==4.8.0openpyxl==3.1.1python-dateutil==2.8.2requests==2.28.0s3transfer==0.6.0six==1.16.0soupsieve==2.3.2.post1urllib3==1.26.9Now 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.1openpyxl==3.1.1requests==2.28.0Use 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.whlSaved c:\dev\python\libraries\openpyxl-3.1.1-py2.py3-none-any.whlSaved c:\dev\python\libraries\requests-2.28.0-py3-none-any.whlSaved c:\dev\python\libraries\certifi-2022.12.7-py3-none-any.whlSaved c:\dev\python\libraries\charset_normalizer-2.0.12-py3-none-any.whlSaved c:\dev\python\libraries\idna-3.4-py3-none-any.whlSaved c:\dev\python\libraries\soupsieve-2.4-py3-none-any.whlSaved c:\dev\python\libraries\urllib3-1.26.15-py2.py3-none-any.whlSaved c:\dev\python\libraries\et_xmlfile-1.1.0-py3-none-any.whlSuccessfully downloaded beautifulsoup4 openpyxl requests certifi charset-normalizer idna soupsieve urllib3 et-xmlfileAs 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 librariesLooking in links: librariesProcessing c:\python\libraries\beautifulsoup4-4.11.1-py3-none-any.whlProcessing c:\python\libraries\openpyxl-3.1.1-py2.py3-none-any.whlProcessing c:\python\libraries\requests-2.28.0-py3-none-any.whlProcessing c:\python\libraries\soupsieve-2.4-py3-none-any.whlProcessing c:\python\libraries\et_xmlfile-1.1.0-py3-none-any.whlProcessing c:\python\libraries\charset_normalizer-2.0.12-py3-none-any.whlProcessing c:\python\libraries\idna-3.4-py3-none-any.whlProcessing c:\python\libraries\urllib3-1.26.15-py2.py3-none-any.whlProcessing c:\python\libraries\certifi-2022.12.7-py3-none-any.whlInstalling collected packages: urllib3, soupsieve, idna, et-xmlfile, charset-normalizer, certifi, requests, openpyxl, beautifulsoup4Successfully 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.15The 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