Skip to content
Advertisement

Clone a python virtualenv to an offline server

Hi I want to clone a python virtualenv to a server that’s not connected to the internet, I searched different forums but didn’t find a clear answer. Here are the methods I found and the problems I have with each :

Methode 1 : (safest but most time consuming)

Save all the libraries via a pip freeze > requierments.txt then go download each one manually and store them in a directory. Copy this directory to the offline server, then create a new virtualenv in the offline server, and install all requirements from the files downloaded. To avoid downloading each one by hand I used pip download -r requirements.txt -d wheelfiles in the source machine, but I couldn’t find a way to install all the packages in one command. But I could use a script with a loop to go through each one. The problem is when even the source server doesn’t have internet connection to download these packages.

Methode 2 : (less recommended but I didn’t understand why)

Is to simply copy the virtualenv directory with all its files to the offline machine, both machines should have apparently the same Python version, and you’ll have to manually modify some hardcoded paths for example modifying all files containing sourceserveruser1devvirtualenv with targetserveruser4devvirtualenv Usually the files to modify start with activate* or pip*. But this method is said to be not recommended but I don’t understand why.

Also if this method work without problems, can I copy the virtualenv folder from a linux server to a windows server and vice versa ?

Advertisement

Answer

You can install all the requirements using

pip install -r requirements.txt

which means the options are:

  1. pip freeze > requirements.txt
  2. pip download -r requirements.txt -d wheelfiles
  3. pip install -r requirements.txt --no-index --find-links path/to/wheels

or

  1. Ensure target machine is the same architecture, OS, and Python version
  2. Copy virtual environment
  3. Modify various hardcoded paths in files

It should be clear why the former is preferred, especially as it is completely independent of Python version, machine architecture, OS, etc.

Additionally, the former means that the requirements.txt can be committed to source control in order to recreate the environment on demand on any machine, including by other people and when the original machine or copy of the virtual environment is not available. In terms of size, the requirements.txt file is also significantly smaller than an entire virtual environment.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement