Python with VirtualEnv
Managing several needed packages within a shared resource environment is difficult. Users may need different package versions, or even packages that conflict with each other. In this example, we will show you how to deal with virtualenv
for python modules.
To setup virtual environment
We start by setting up a virtual environment on your home space
To use the virtual environment
Now, you are in a virtual environment. You may see [env]
at the beginning of SHELL command. The following commands will tell you which python you are using:
To play with the virtual environment, you can try with numpy
which comes with the virtual environment. Here is an example of numpy_example.py
To run
Note that, if your job cannot run outside the virtual environment if numpy is not available.
To install package
Here is an example of how to install ephem
(python package for performing high-precision astronomy computations):
Here is an example to use ephem to calculate the astrometric geocentric right ascension and declination for Mars on specific date.
You then can simply run python ephem_job.py
in the virtual environment. You may see
To leave from the virtual environment
If you try to run again the previous example, but outside the virtual environment, you may see
It is because we do not have ephem
installed in the shared (central) environment.
Using VirtualEnv with Slurm
Here is an example (example4.slurm
) of the Slurm submission script to run python application on worker node using VirtualEnv set in the frontend node (as the example above). To submit, you can simply use the same submission command sbatch example4.slurm
Last updated