If you’re like me, you love the convenience of installing modules for your Python interpreter from within the IDE. My personal preference is the PyCharm IDE, and easily adding modules for the interpreter is one of the features I love. Recently, however, I found myself needing to install Python modules on a computer without internet access. Without access to the internet, the IDE cannot simply download and install the modules automatically for you.
So, there I was – I had the IDE, I copied over the code, and I was working on getting everything set up. When I tried to run my code, I found that my next task was to manually install the necessary Python modules without internet!
To accomplish the task at hand, I performed the following steps:
- Determine which module I would need next
- Download the module using a computer with internet access
- Copy the module to the destination computer using a thumb drive
- Use Command Prompt to navigate to the module and install it
- Repeat steps 1-4 until all necessary modules were installed
Determine Which Module You Need
I was trying to get Python code to run on a computer with no access to the internet. Every time I would try to run the program, I would get an error when the interpreter went to look for a module I didn’t have. If you’re trying to do what I was trying to do, Python will tell you what module you need. For this guide, I’ll use
matplotlib as the example case.
Use A Computer With Internet Access To Download The Module
Using a computer that is able to access the internet, we now need to download the module from “PyPI”, the Python Package Index. To find modules, I typically just Google “pypi [name of module]”. So, in this case, I Googled “pypi matplotlib”:
Nice. I click the link and check to make sure I am satisfied that this appears to be the correct module. Under the name of the library, under the Navigation menu, I click the “Download files” link.
Selecting The Correct Build Distribution
Next, we need to select the correct distribution. (Here’s some info on Python distribution naming). When manually installing Python modules on a computer that doesn’t have access to the internet, I found that it was easiest to get the job done by using individual .whl files available under the Built Distributions heading.
For many packages, you’ll find a large number of distributions available, and you’ll need to choose the right one. While this can seem a bit daunting at first, primarily you’re looking for three things:
- The correct Python version
- The correct Operating System
- The correct type of CPU
So, for instance, in my case, the destination computer was a 64-bit Windows-based PC with an Intel i7 processor running Python 3.11.
As such, I chose the build with “cp311” (for Python 3.11), “win”, and “amd64” (for a CPU with x86-64 architecture).
Typically, I’ve found it straightforward to find the correct distribution. Worst comes to worst, you get it wrong, you get an error, and you try another build.
Once I click on the link to the build’s wheel file, I’m given my browser’s prompt to Open, Save as, etc.
Save the file to a thumb drive or to a shared network drive, etc., that you will be able to use to transfer the distribution to the computer that doesn’t have access to the internet.
Copy The Module To The Destination Computer
The next step is to take your thumb drive or other media and transfer the module to the destination computer. I made a folder off of the C drive root,
C:\Python Packages, that would be easy to navigate to from the command prompt.
Use Command Prompt To Install Python Modules Without Internet Access
You’ve now downloaded your module and copied it to the computer that does not have access to the internet. Next, you’ll use the command prompt to install the module and get you on the road towards having a working Python interpreter!
Open the command prompt, and navigate to the folder you copied your .whl file to in the previous step.
Next, you’ll enter the following command, with the full filename for the .whl file at the end of the command:
pip install --no-index --no-dependencies --find-links . nameofwheel.whl
So, in my case, the command I entered at the prompt looked like the following:
Download & Install Additional Python Modules Without Internet Access
Nice! Now, you’ve downloaded, transferred, and seen how to use the command prompt to install Python modules on a computer without internet access. Time to see if that solved your problem.
In my case, I return to PyCharm to try to run my code. I no longer receive the error for the
matplotlib module! Of course, I do now have an error for a different module. 😂
You see, modules often have “dependencies”; they require other modules for internal use. Typically, when you install a module on a computer that does have access to the internet, the module’s dependencies are pulled and installed automatically for you.
Because we are installing modules offline, however, the installer is unable to pull the next required module from PyPI. Therefore, we have to choose between two courses of action:
- Find out the dependencies and download each new module as we go, or
- Determine all dependencies in advance, download and install all at once
For me, I find it easier to simply check after each installation if I’m missing any dependencies and to then download the next one. If you wanted, though, you could research your module’s dependencies and download all required modules in advance.
Whichever course of action you choose, at this point, you’ll simply repeat the steps above as necessary until you get past the ModuleNotFoundErrors. At that point, you’ll have installed any Python modules that were missing from your offline computer, and you’ll be ready to work on your code!