Python virtualenv and Sun Grid Engines module system
Yesterday I was trying to use a Python virtualenv for a python script and all the subprocesses generated by this script. You may ask, where is the problem? All subprocesses are started with the same environment variables as its mother/father process. The problem occured because I am loading the python module (using the Sun Grid Engine) prior to executing a python script. BUT loading the module prepends the PATH variable with the path to the global python installation. Thereby path found by #!/usr/bin/env python
is the global installation, but I do want to use a python virtualenv (relative path in she-bang fails if script called from another directory).
So the solution came from this post on SO. I added these lines to each python script:
import os
script_path = os.path.dirname(os.path.realpath(__file__))
activate_this_file = '%s/python_env/bin/activate_this.py' % script_path
execfile(activate_this_file, dict(__file__=activate_this_file))
At first I need to get the absolute path to the current file. Then add the relative path from there to activate_this.py
file in the python virtualenv. At last execfile()
as requested inside activate_this.py
.