1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

CookBook/PyEnv

Virtual environment for testing

Up | Full index


  • How test you packages and programs in clean environment?
  • You test new program and do not want change current python installation?
  • How create several python environments for different task?

The answer is a virtual environment.

For this task may be used:

  • full isolated virtual environment virtualenv which do copy of you Pyton installation.
  • pyenv which initialize virtual environment in specified path but use current installation.
  • combination of above - you may create full isolated environment with virtualenv but initialize and run files with pyenv.
  • also you can search other tools for this task.

pyenv is more easy and faster for use. It not need special installation or copy of you current python. It allow more control under virtual Python initialization or activation then virtualenv. But pyenv not allow fully isolate. In difficult and complex tasks may be good using combination of virtualenv for isolating and pyenv for activating.

The cool feature of pyenv is set up in activate process not only sys.prefix for site-specific directory, but also set up paths in environment variables $PYTHONPAH and $HOME. If you execute with pyenv script or module then pyenv transfer to executed script environment with paths ($PYTHONPATH, $HOME) - and as result if you script will execute other script it also will be have this paths.

Testing in pyenv.

For example will be uased XPyLIB library.

In first we create virtual environment folder in user home directory:

~$ mkdir ENV
~$ mkdir ENV/home
~$ mkdir ENV/usr
~$ mkdir ENV/temp
  • ENV - root virtual environment folder.
  • ENV/home - it will be virtual user home.
  • ENV/usr - it will be virtual prefix for binaries and so on (as sys.prefix in Python).
  • ENV/temp - temporary folder.

Download and unpack XPyLIB package into ENV/temp. For easy using copy pyenv.py script from ENV/temp/XPyLIB/XPyTools/scripts/ to you home binary ~/bin and make it executable.

Now we ready to use virtual environment.

View environments path from ENV:

~$ pyenv.py --env-path ~/ENV/usr --user-home ~/ENV/home --activate-env --add-bin-path --dump

 *** PYENV DUMP ***

SYS.PREFIX = "/home/alex/ENV/usr"
PYTHONPATH = "/home/alex/ENV/usr/bin:/home/alex/ENV/usr/lib/dist-packages:/home/alex/ENV/usr/lib/python2.6/dist-packages:
/home/alex/ENV/usr/lib/site-packages:/home/alex/ENV/usr/lib/python2.6/site-packages"
HOME = "/home/alex/ENV/home"
SYS.PATH =
['/home/alex/ENV/usr/bin',
 '/home/alex/ENV/usr/lib/python2.6/site-packages',
 '/home/alex/ENV/usr/lib/site-packages',
 '/home/alex/ENV/usr/lib/python2.6/dist-packages',
 '/home/alex/ENV/usr/lib/dist-packages',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode',
 '/usr/lib/pymodules/python2.6/gtk-2.0',
 '/usr/lib/python2.6/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/python2.6/dist-packages/gst-0.10',
 '/usr/lib/python2.6/dist-packages/PIL',
 '/usr/lib/python2.6/dist-packages/Numeric',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6',
 '/usr/local/lib/python2.6/dist-packages/virtualenv-1.3.3-py2.6.egg']

 *** PYENV DUMP END *** 

Used options is next:

  • --env-path - this environment path used for adding environment packages to sys.path.
  • --user-home - set user home - set enviromment variable $HOME.
  • --activate-env - activate environment - adjust sys.prefix (set the site-specific directory prefix).
  • --add-bin-path - add path to binaries ("bin" for *nix, and "scripts" for windows). By default in python this path is not added. But if add this bin path then executing scripts from bin will be more easy.
  • --dump - print to standart output environment paths.

Other useful options:

  • --remove-path-contain - remove from sys.path specified paths. If you have installed old version XPyLIB in real Python, then you can use --remove-path-contain XPyLIB for removing old path from sys.path for use only new installation from ENV.
  • --no-site-packages - remove from Python sys.path all old (real installation) site packages paths.
  • --add-path - add additional paths.
  • --add-site - add additional site paths as site.addsitedir().
  • --exec or -c - execute given statement (as Python do).
  • --module, -m - run given module (as Python do).
  • --help - print usage information.

Now change dir to unpacked XPyLIB and setup XPyLIB library into ENV.

~$ cd ENV/temp/XPyLIB
~/ENV/temp/XPyLIB$ pyenv.py --env-path ~/ENV/usr --user-home ~/ENV/home --activate-env --add-bin-path setup.py install

... ... ...

~/ENV/temp/XPyLIB$ cd ~
~$ 

Library was installed in next path (for example):

  • ENV /
    • usr /
      • lib /
        • python2.6 /
          • site-packages /
            • XPyLIB /
            • XPyTools /
            • XPyLIB-0.1.2-py2.6.egg-info

After installation we can execute commands:

~$ pyenv.py --env-path ~/ENV/usr --user-home ~/ENV/home --activate-env --add-bin-path -c "import XPyLIB; print XPyLIB.dir_root"
/home/alex/ENV/usr/lib/python2.6/site-packages/XPyLIB
~$

Output root path where XPyLIB installed: /home/alex/ENV/usr/lib/python2.6/site-packages/XPyLIB.

Run IDLE into virtual environment ENV:

~$ pyenv.py --env-path ~/ENV/usr --user-home ~/ENV/home --activate-env --add-bin-path -m idlelib.idle
... ...

After exit from IDLE try run timetest example:

~$ pyenv.py --env-path ~/ENV/usr --user-home ~/ENV/home --activate-env --add-bin-path -m XPyLIB.timetest --example
... ...

Into virtual home ENV/home/.xpylib/timetest/current.timetest you can see tested system scores (about testing here).