Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port Python virtualenv to another system

I am using many python packages like numpy, bottleneck, h5py, ... for my daily work on my computer. Since I am root on this machine it is no problem to install these packages. However I would like to use my "environment" of different packages also on a server machine where I only have a normal user account. So I thought about creating a virtual environment (with virtualenv) on my machine by installing all needed packages in there. Then I just copy the whole folder to the server and can run everything from it?

My machine uses Fedora 19 whereas the server uses Ubuntu. Is this a problem? I could not find any information on how to move such a virtual environment to another system. The reason I would like to create the virtual environment on my machine first is that there are a lot of tools missing on the server like python-dev, so I can't compile numpy for instance.

I looked into Anaconda and Enthought Python distributions, but they don't include a couple of packages I need. Also, there should be a completely "open" way for this problem?

Moving the virtual environment to the server failed, since it is complaining about some missing files when I import the packages. This is not surprising probably...

like image 700
HyperCube Avatar asked Jul 11 '13 07:07

HyperCube


People also ask

Can you move a virtualenv?

1 Answer. Kindly be informed that yes, it is possible to move it on the same platform. You can simply use --relocatable on an existing environment. But it will get changed based on the new location in order to call python and pip, etc.

How do I switch to virtualenv?

To install virtualenv, just use pip install virtualenv . To create a virtual environment directory with it, type virtualenv /path/to/directory . Activating and deactivating the virtual environment works the same way as it does for virtual environments in Python 3 (see above).

Are virtualenv portable?

A virtual environment helps keep your project bundled together with a list of its dependencies. This makes it portable and easy for someone else to open your project and get it up and running without dozens of import errors.


2 Answers

You shouldn't move your virtualenv since it is essentially linked to your system python and the binary won't work on other machines.

However... you can export a list of installed packages and install them in another virtualenv through a requirements.txt file.

Basically, what I usually do with most of my projects:

# Generate a requirements file:
pip freeze > requirements.txt

On the new machine:

# This uses virtualenvwrapper, but you can do it without as well
mkproject my_project_name
git clone git://..../ .
pip install -r requirements.txt
like image 141
Wolph Avatar answered Oct 05 '22 05:10

Wolph


Having manually compiled VTK and PySide2 for Python36, I have also found myself bending the virtualenv rules.

Just today, I transferred my virtualenv to another system, and to make things easier, I gave it the exact same path that it had on the previous system. However, I did not have the same path for Python on my new system. Fortunately I was able to change the location that the virtualenv was looking for by altering a 'orig-prefix.txt' file located in [VIRTUALENV]/Lib.

The base Python path a virtualenv requires is stored in: [VIRTUALENV]/Lib/orig-prefix.txt

If I recall correctly, the path of the virtualenv itself is embedded in multiple files. Thus, in a case where I needed to relocate the virtualenv to a different path, I just recreated it and copied over everything except for the [VIRTUALENV]/Scripts directory.

This is probably not the way virtualenv is meant to be used, but it does provide a work-around. Also, note that I am doing this in a Windows environment.

like image 41
Darren Janeczek Avatar answered Oct 05 '22 05:10

Darren Janeczek