welcome    phd research    awards    publications  & conferences    software    links    blog

 
 

Setting up Python Virtual Environments on the NERSC Hopper Supercomputer

 

Setting up a Python Virtual Environment on the NERSC Hopper supercomputer

 

In this post I will explain how to set up a Python virtual environment on the Hopper supercomputer and prepare it for installing particular modules (e.g., modules needing “pygtk” which takes a little extra work).


In this post I discussed Python virtual environments.


Start in your “$HOME” folder (you will be in this folder when you first connect to Hopper---if you are somewhere else right now simply type “cd” which takes you home).


Now we need to load a bunch of modules:

    module load python/2.7.3

    module load python_base/2.7.3

    module swap numpy numpy/1.8.2

    module load virtualenv/1.8.2

    module load virtualenvwrapper

    module load cray-hdf5


Note: I needed the hdf5 module to install the “tables” Python module, but you might not need that.

Also, I needed the “python_base” module (second in the list above) to get “pygtk” working. More on this later in the post.


We have loaded the virtual environment module so now we create virtual environments through the “virtualenv” function.


Now we create the folder which will hold our virtual environment. Let’s say we want this in our home folder:

    mkdir my_virtual_env


But we could also make a hidden folder, such as “.virtual_envs” and put the environments in there.


Tell virtualenv that we want the “my_virtual_env” folder to be our virtual environment:

    virtualenv --no-site-packages my_virtual_env


Notice the “--no-site-packages”. It makes sure that we don’t inherit any python packages on the system which are global right now (we don’t want those because we want to start “clean” in our own local environment). This means that the “python_base” module we loaded above is not going to be recognized (it contains pygtk among other files: nose, pycairo, pygobject, pygtk and setuptools).


In my case, I needed pygtk. Since we used “no-site-packages” it was not available to me, and installing it via either pip or easy_install did not work. So what do we do? We can first find the path to pygtk. Start Python outside the virtual environment. Notice that you can load the pygtk module (because you are outside the virtual environment, you cannot load it inside the environment).


Then import “sys” and pygtk:

    import sys

    import pygtk


Where is “pygtk” located on the system? Here:

    sys.modules['pygtk']

<module 'pygtk' from '/usr/common/usg/python/2.7.3/lib/python2.7/site-packages/pygtk.pyc'>


So the folder which holds the modules loaded from the “module load python_base/...” call are put in:

/usr/common/usg/python/2.7.3/lib/python2.7/site-packages


What we can do is to go the virtual environment folder and make a symbolic link to the pygtk in the site-packages folder (and only pygtk, no other of the packages in “site-packages”---that was the whole point of using “no-site-packages” earlier). So, go into:

    ~/virtual_env/lib/python2.7/site-packages


And type:

    ln -s /usr/common/usg/python/2.7.3/lib/python2.7/site-packages/pygtk.py

    ln -s /usr/common/usg/python/2.7.3/lib/python2.7/site-packages/pygtk.pth

    ln -s /usr/common/usg/python/2.7.3/lib/python2.7/site-packages/gtk-2.0/

    ln -s /usr/common/usg/python/2.7.3/lib/python2.7/site-packages/cairo


Finally, we activate the virtual environment:

    source my_virtual_env/bin/activate


Notice how your shell session has the name of your virtual environment prepended inside parentheses. This means the virtual environment is activate. You can deactivate it with:

    deactivate


That's it. Now we can use "pip" to install packages (or "easy_install", but with "pip" it's easy to uninstall as well).


In my case I had to install numexpr, cython, tables, py-mcmc, GPy, ase (from a tar-ball), etc., and this all worked out.


Finally, I would recommend automatically loading these modules when connecting to Hopper. You do this by putting the “module load --” statements from above in your “~/.bashrc.ext” file.


Finally, to use pygtk you should log on to Hopper with X11 Forwarding which is done by using the “-Y” flag with ssh:

    ssh -Y user@(...)


I hope this was useful. Comments are welcome.

Sunday, November 2, 2014

 
 

next >

< previous