Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relocatable (self-contained) Python built from source tarball inside virtualenv environment?

I have looked at the virtualenv documentation and also found this question here at StackOverflow. However, neither answer my question or I may be missing something, so I am asking.

How can I create a virtualenv environment which contains a python that doesn't depend on the (system-wide) python of the "host"? I.e. I want a fully self-contained virtualenv with its own Python 2.7 compiled from source. Another property would be important: since virtualenv isn't the same as chroot the python inside the virtualenv has to cope with different absolute paths.

I reckon the closest to what I want is a virtualenv --relocatable with its own Python installation inside.

How can I achieve that?

Rationale

I have some older Linux boxes with different versions of Python offered through the package manager. The admin won't allow me to build a more recent Python on the boxes, so I need to be able to install a Python from source into the constrained environment I have.

Properties I need:

  • virtualenv environment should be fully self-contained (complete Python with all libraries etc)
  • the python inside should work regardless of its absolute path when deployed
    • I'm aware the $ENV/bin/python seems to do that, but it also seems to rely on the python installed system-wide. I want to cut this dependency out completely, rolling my own Python.
  • Should behave like with virtualenv --relocatable but needs to contain a full Python installation as well.

Issues that can be ignored for the scope of this question:

  • Architecture/platform. This will run only on a single architecture and the virtualenv environment will be built for each platform (currently only various Linux distros).
like image 813
0xC0000022L Avatar asked Apr 15 '13 16:04

0xC0000022L


1 Answers

  1. get the python source
  2. ./configure --prefix=/dest
  3. make && make install

congratulations, you now have a python installation that's completely independent of the system python in /dest. Moving this directory shouldn't be a problem, if that's what you mean with 'relocatable'.

To use this installation instead of the system python, just make sure to put /dest/bin on the PATH before the standard locations.

If that's not enough for you, you can setup a virtualenv using this python installation...

like image 79
mata Avatar answered Oct 15 '22 03:10

mata