2

Possible Duplicate:
Python - It is possible to install another version of Python to Virtualenv?

Within my (virtualenv activated) env folder, it appears as though the python2.7 folder has symlinks to absolute paths for my system python installation.

This is not ideal.

Even when I install from a local path, and use virtualenv to set and environment in a folder env, the paths to Python are absolute, and a dependency on the OS environment is established. I want to rid this dependency and make the python interpreter, as well as all software relying upon it, completely independent.

Let's imagine that I want python2.6 to be included IN the env tree as a STAND-ALONE installation without symlinks to my system folders.

How does one accomplish this feat of extraordinary non-linkage?

$ > pwd 
/Users/foo/development/v1/bar/env
(env)
$ > ls -l lib/python2.7/
total 920
lrwxr-xr-x  1 foo  staff     82 Oct 15 16:48 UserDict.py -> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py
...
lrwxr-xr-x  1 foo  staff     85 Oct 15 16:48 _weakrefset.py -> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.py

Thank you for your help.

EDIT: Moreover, it will be most ideal to have the virtualenv (including the local python install) relocatable.

Community
  • 1
  • 1
4Z4T4R
  • 2,340
  • 2
  • 26
  • 45
  • Installing a Python interpreter from scratch probably simply isn't in the scope of `virtualenv`. Install a Python yourself into the env folder and then tell `virtualenv` to use that installation, making it relocatable as needed. – millimoose Oct 16 '12 at 01:11
  • @Insidi0us - I couldn't find that dupe... thank you. – 4Z4T4R Oct 16 '12 at 01:17
  • Just install another Python, like an OS X Python installer from python.org, and use it instead of the Apple-supplied system Python to install new instances of Distribute and/or pip and virtualenv, if you really need to use it. But it's unlikely that you really need to or should build Python from source. It's a bit tricky to get right, especially on OS X. Another option is to install everything you need from a 3rd-party package manager, like MacPorts. – Ned Deily Oct 16 '12 at 01:23
  • @millimoose - Good idea, however, when I tried this, virtualenv borks with "shutil.Error: `/Users/foo/development/env/bin/python` and `env/bin/python` are the same file" – 4Z4T4R Oct 17 '12 at 08:06
  • @toszter Don't install Python and the virtualenv into the same directory. Put at least one of them (preferrably Python, not the virtualenv) into a separate subdirectory under `env`, say `env/sdk` – millimoose Oct 17 '12 at 13:35
  • @millimoose - Indeed, I installed python to pyenv/ and made all python symlinks within env/ relative. Running `virtualenv --relocatable env` did not fix the relative symlinks. Tested and it works. – 4Z4T4R Oct 17 '12 at 21:06

2 Answers2

3

UPDATE: Please ALSO refer to Is it possible to install another version of Python to Virtualenv?

Big thanks to @millimoose, et al.

Here's what I ended up doing, very specifically. I will update if I encounter problems in the future.

  1. Set up environment folders.

    $ mkdir env
    $ mkdir pyenv
    $ mkdir dep
    
  2. Get Python-2.7.3, and virtualenv without any form of root OS installation.

    $ cd dep
    $ wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
    $ wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
    
  3. Extract and install Python-2.7.3 into the pyenv dir. make clean is optional if you are doing this a 2nd, 3rd, Nth time...

    $ tar -xzvf Python-2.7.3.tgz
    $ cd Python-2.7.3
    $ make clean
    $ ./configure --prefix=/path/to/pyenv
    $ make && make install
    $ cd ../../
    $ ls
    dep    env    pyenv
    
  4. Create your virtualenv

    $ dep/virtualenv.py --python=/path/to/pyenv/bin/python --verbose env
    
  5. Fix the symlink to python2.7 within env/include/

    $ ls -l env/include/
    $ cd !$
    $ rm python2.7
    $ ln -s ../../pyenv/include/python2.7 python2.7
    $ cd ../../
    
  6. Fix the remaining python symlinks in env. You'll have to delete the symbolically linked directories and recreate them, as above. Also, here's the syntax to force in-place symbolic link creation.

    $ ls -l env/lib/python2.7/
    $ cd !$
    $ ln -sf ../../../pyenv/lib/python2.7/UserDict.py UserDict.py
    [...repeat until all symbolic links are relative...]
    $ cd ../../../
    
  7. Test

    $ python --version
    Python 2.7.1
    $ source env/bin/activate
    (env)
    $ python --version
    Python 2.7.3
    

Aloha.

Community
  • 1
  • 1
4Z4T4R
  • 2,340
  • 2
  • 26
  • 45
1
  1. Use pythonbrew to get an isolated Python installation.
  2. Use that Python installation to initiate your virtual environment.
  3. Make the virtual environment relocatable.
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thank you, but in this case, the python install now has a dependency on pythonbrew, and I want to transport the python source without **any** dependencies. – 4Z4T4R Oct 17 '12 at 23:03
  • You don't need pythonbrew to be transported, you just need it to setup your portable environment. – Burhan Khalid Oct 18 '12 at 01:39