Astronomy  /  Internal Info  /  IT Resources  /  How to create a Python virtual environment

How to create a Python virtual environment

Caltech Astronomy has both Python 2 and Python 3 installed via the Anaconda distributions. On our Linux machines, these are in /usr/local/anaconda2 and /usr/local/anaconda3. Although one can run them directly from there, the best way to use our Python distributions is probably with a Python virtual environment. This process sets up a personalized Python distribution on a local disk, so it is much more configurable and will run faster.

(Note: It's a common mistake to install this in one's home directory. Don't do that - it will take up a lot of space and be quite slow.)

To setup a Python virtual environment, do something like this:

user> export PATH=/usr/local/anaconda3/bin:$PATH
user> mkdir /scr/USER/python
user> python3 -m venv /scr/USER/python/env01

The string "env01" can be anything. You can have many different virtual environments.

NOTE: The PATH setting is for bash; other shells, such as csh/tcsh may require slightly different syntax. You also probably want to put that first line in your .bash_profile, .cshrc, or similar login file. However, note also that a VNC server session may not start properly if anaconda3 is in your PATH. So, if you are using the 'vncserver' command to start a VNC session, then make sure that anaconda3 is not in your PATH setting at that time.

Then to use the virtual environment:

user> bash
user> source /scr/USER/python/env01/activate

After that, all python commands will work out of that directory.
So things like this:

user> pip install tensorflow
user> pip install --upgrade pip
user> pip install keras

will all work in that /scr/USER directory. It's simple and fast, and it doesn't interfere with other python distros on the system.