How to use virtualenv (Python)

In order to have the maximum flexibility while using Python, we decided to really minimize the amount of system python packages, and we encourage the users to always use Python's VirtualEnv:

  1. Create your virtualenv
    imatge@gpie01:~>> mkdir ~/venv
    imatge@gpie01:~>> srun --mem 500M --pty virtualenv --python=python3.6 ~/venv/myenv
    New python executable in /imatge/imatge/venv/myenv/bin/python
    Installing setuptools, pip, wheel...done.
    
    You can also use the --system-site-packages to have the minimal set of packages already installed in the servers Hint: python version is 3.6 on the next example, but you can choose the best it fits with your software
    imatge@gpie01:~>> mkdir ~/venv
    imatge@gpie01:~>> srun --mem 500M --pty virtualenv --system-site-packages --python=python3.6 ~/venv/myenv
    New python executable in /imatge/imatge/venv/myenv/bin/python
    Installing setuptools, pip, wheel...done.
    
  2. Start using our new virtualenv by activating it:
    imatge@gpie01:~>> source ~/venv/myenv/bin/activate
    (myenv) imatge@gpie01:~>>
    
    Note the new initial prefix of the command prompt in parentheses, showing the current virtualenv.
  3. In our new venv we can install any package we want:
    (myenv) imatge@gpie01:~>> srun --mem 500M --pty  pip install requests
    Collecting requests
      Downloading requests-2.13.0-py2.py3-none-any.whl (584kB)
        100% |████████████████████████████████| 593kB 1.0MB/s
    Installing collected packages: requests
    Successfully installed requests-2.13.0
    
  4. Once we are done we can deactivate it:
    (myenv) imatge@gpie01:~>> deactivate
    imatge@gpie01:~>>
    
    Note that the prefix is removed once we deactivate the venv.

Also note that, to be able to replicate / recreate our virtualenv, we can always use:

(myenv) imatge@gpie01:~>> pip freeze > requirements.txt

Then we can activate or create a different venv in clone the installed packages by:

(othervenv) imatge@gpie01:~>> pip install -r requirements.txt

You can see the requirements file to check the packages that you are using:

(myenv) imatge@gpie01:~>> cat requirements.txt
appdirs==1.4.0
packaging==16.8
pyparsing==2.1.10
requests==2.13.0
six==1.10.0

Do not store code or data in your virtualenv folder. Put the code/data into any other place.