Virtualenv
This is just a quick “how-to” use virtualenv. Full documentation available here.
You can also check out the how-to written by Andrej Krevl.
Create a new virtualenv
#cd to a folder where you want to store the new virtualenv (in this example we are using python2.7) virtualenv -p /usr/bin/python2.7 newenv #or if you want to use python3.7 virtualenv -p /usr/local/bin/python3.7 newenv
Activate virtualenv
#activate virtualenv enviroment that we created before source newenv/bin/activate
Installing packages in the new environment
#just a test that you see which pip executalbe is in use (newenv) <csid>@madmax:/dfs/scratch0/<csid>$ which pip /dfs/scratch0/<csid>/newenv/bin/pip #install a new package (newenv) <csid>@madmax:/dfs/scratch0/<csid>$ pip install numpy
Using python with virtualenv
#check which python is in use while using virtualenv (newenv) <csid>@madmax:/dfs/scratch0/<csid>$ which python /dfs/scratch0/<csid>/newenv/bin/python #check where is the package that we installed before located Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> numpy.__file__ '/dfs/scratch0/<csid>/newenv/lib/python3.5/site-packages/numpy/__init__.py'
Deactivate virtualenv
#deactivate virtualenv enviroment deactivate #check wich python is in use when the virtualenv is deactivated <csid>@madmax:/dfs/scratch0/<csid>$ which python /usr/bin/python
Using Jupyter Notebook with virtualenv
To use Jupyter Notebook with a specific virutalenv you need to do the following:
#active the virtualenv you want to use source newenv/bin/activate # install ipykernel pip install ipykernel # create a new kernel python -m ipykernel install --user --name=newenv # output: Installed kernelspec newenv in /afs/cs.stanford.edu/u/user/.local/share/jupyter/kernels/newenv
Listing all the available kernels:
jupyter kernelspec list #sample output #Available kernels: # newenv. /afs/cs.stanford.edu/u/user/.local/share/jupyter/kernels/newenv # python2 /usr/local/share/jupyter/kernels/python2 # python3 /usr/local/share/jupyter/kernels/python3
If you remove the virtualenv and you don't need the kernel anymore you can remove it with:
jupyter kernelspec uninstall newenv