Using python libraries is easy and fun, but when you can come across a point that you need them offline, that’s a whole new case.
On my work, I work on a isolated network from the internet so I needed to find a way I could do this easily and FAST!
Well, I remembered the virtualenv tool that I can use in order to bundle up all my libraries which I would install using pip and than copy paste that environment.
So let’s jump right into it! All we need is to create a virtual environment
Short brief about virtualenv
Virtualenv – A tool that enables us to create another folder that has the scripts from our “Parent Python” folder and add any other library we wish to that virtual environment. Simply it’s a replica that is a standalone environment of python.
When I say scripts I mean about the python builtin scripts that comes with python — pip, wheel, setup-tools.
So for example if we install Django/numpy or any other python library and we create a virtualenv from that python environment, the virtual environment would not have Django/numpy installed.Virtualenv is needed to be installed using the command(CMD/Terminal):
pip install virtualenvHow to create a new virtual environment(CMD/Terminal):
virtualenv my-environment-nameThe installation to that environment is being used by activating that environment using(Run the commands on cmd/terminal):
— Windows: /virtual-environment-folder-name/Scripts/activate
—Linux/Mac: source /bin/activate
Once the environment is activated the scripts and the python virtual environment itself is considered the entire machine python interpreter.
When you actually create a new virtual environment it’s like a really normal python environment that you can add new libraries or make any other configurations you desire.
So the benefit is that you don’t tamper with the original python enviornment, What you can possibly already understand, it’s not really necessary to use virtualenv to download libraries to an offline station, you can use your original python environment.
We will see that how the virtual environment benefits us later in this article, in terms of a cleaner way to download packages.
Before we dig in more on downloading our files we need to understand — how does the installation of python libraries are made using pip?
Well… python has packages repository called PyPi that works like NPM/Nuget for our Node.js/C# developers 🙂
When we are calling the command
pip install 'library-name'
We actually requesting from PyPi sever the ‘library-name’ package .whl/.tar file. Once pip done downloading that package it unpacks it and install it in our python environment.
You probably see where I’m going with this 🙂
In order to only download the library itself and not install it we can simply issue the following command
pip download 'library-name'
What it would that, as probably you already guessed it, it downloads the .whl/.tar file and put it and the path you are currently inside the cmd/terminal.
I haven’t mentioned this but .whl stands for Windows packages files while .tar stands for Linux packages files.
Let me introduce you another pip command on the way
pip freeze
The use of this functionality is to print the current installed libraries inside our python enironment.
The reason I’m talking about it is because we can simply do the following set of commands(In case we want Django and numpy libraries to our offline station, just for the example)
virtualenv my-new-virtual-env
cd my-new-virtual-env
#Activate the environment using the commands shown above
#For our convenience lets create in the root folder of the env a Wheelhouse/Tarhouse folder which will we install all our packages.
(Windows) mkdir Wheelhouse and afterwards cd Wheelhouse
(Linux) mkdir Tarhouse and afterwards cd Tarhouse
pip download virtualenv django numpy
pip freeze > requirements.txt
#Take the downloaded .whl/.tar files to an offline station
Now we have a folder on our online station with virtual environment that holds a Wheelhouse/Tarhouse folder.
What we can do now is to compress it or move the folder as it is to the offline station.
Now, ensure you have same python version installed on the isolated network station and off course transfer the virtual environment to it.
Firstly, we need to call the activate script just as we did before.
Make sure you are in the root folder of the virtual envrionment and issue the following command in your command line:
pip install -r requirements.txt --find-links=(Wheelhouse or Tarhouse)
What we just did were telling pip to look inside the requirements.txt file for libraries we listed using pip freeze, and also told him the links to locate those libraries , which is the Wheelhouse(Windows) or Tarhouse(Linux) we made.
Well, there you have it!
You accomplished and learned how to use pip and work with python packages files, in order to download and install them to a isolated network station.
I just wanted to add up that you can issue the command(if you want install single package at a time):
pip install Wheelhouse/some-package-file.whl (or .tar on linux)
For my taste I think it’s more cleaner and simpler using the virtual environments and Wheel/Tar folder, which gives cleaner way of downloading multiple packages and not confuse between which dependencies were added or not with our packages.
I hope you had great time reading this article and off course that it helped you out in any way possible.
P.S — If there’s anything that is a mistake or not correct even by date and etc… I would like to know about it in order to update the article, so it will not mislead anyone about the subject.
If you liked it, don’t forget to clap 🙂
Thank you and have a great day!