Home Comp Sci Machine Learning Math/Finance Python Notes 3D Printing

Virtual Environments (venv)


If you haven’t heard of venv or the like I highly recommend that you look into them. What venv allows you to do is to create separtate environments usually with different settings and installed software. For example, you can have Python 2.7 installed in your machine and use venv to use Python 3.x. In windows the inner workings of venv may be more complicated, however, in Linux the ‘trick’ is simply that your environment variables are updated to reflect the installations in a particular folder. On my machine I’m running python 3.x inside of /Users/myusername/venvp3 so that if you output my $PYTHONPATH you get:

    > /usr/local/lib/python2.7/site-packages



However, if you activated my venv in ~/venvp3 and then you output my $PYTHONPATH you would get:

    > /Users/username/venvp3/lib/python3.5/site-packages



It is really as simple as that, if you want to create an environment:

And now you can carry on as follows whenver you want to use this environment:

And so you can have many different virtual environments with different settings. For instructions on how to set up virtualenv(what I call venv) and virtualenvwrapper (also very useful) in linux visit the links below:

TIP

I keep a file called ‘requirements.txt’ in the xyz folder and in it I list all of the packages that I have installed via pip. The reason I do this is that I can use that requirements file to install a brand new virtual environment, with all of the packages in the file via this single command:

    > pip install -r requirements.txt

A sample ‘requirements.txt’ file will look like (with the ==version in case that you need that specific version):
pandas==0.10.5
numpy
scipy
scikit-learn
sqlalchemy
etc..