From Python (https://docs.python.org/3/glossary.html#term-virtual-environment):
Virtual environments allow you to create independent collections of installed packages. This is often useful when you need two 3rd party pacakages that conflict with eachother. Instead of installing and unstalling when need, you can create a virtual environment for configurate A with package A and an environment for configuration B with package B. Then you can switch between environments as needed.
There are several packages that exist for creating virtual environments.
venv: A builtin module included in Python 3.3+. Designed for crating lightweight virtual environments.virtualenv: A 3rd party package that is almost identicaly in use tovenv, but available for both Python 2 and 3.pyenv: Allows for switcing between Python versions.pyenv-virtualenv: A plugin forpyenvthat provides features to manage virtual environments created byvirtualenvandconda.conda: An open-source pacakge management and environment management system. This is like combiningpipandvenvinto one, butcondais not associated with either of thos. Thecondamanager is independent ofpipand https://pypi.org, and is used for Anaconda, Minconda, and the Anaconda Repository.- and much more....
This tutorial focuses on using venv to create virtual environments. The follow is covered below...
- Crating a Virtual Environment
- "Modifying"
PYTHONPATHfor Your Virtual Environment - Using Virtual Environments with Jupyter Notebooks
- The base python version used for your virtual enviroment comes from the python version used to create the virtual environment.
In this casse it is Python 3.8
$ python3 --version Python 3.8.6 - You can view the packages currently installed in your Python 3.8 distribution by...
In this case, only
$ python3 -m pip list # equivalent to pip list Package Version ---------- ------- pip 21.1.3 setuptools 49.2.1pipandsetuptoolsare installed in addition to the builtin packages. - Create a directory for your virtual environemnt
$ cd ~/Desktop/2021_Hack_Week $ mkdir venvs $ cd venves $ mkdir py38_hack
- Now, create the environment. Make sure you are sitting one level outsite the directory where the environment will be created. In this example, that is the
venvsdirectory.$ python3 -m venv py38_hack - Now, the environment is created but not yet ativated. By moving into
py38_hackyou can see it is now populated.$ cd py38_hack $ ls bin/ include/ /lib pyvenv.cfg - To activate the virtual environment we have to execute the
activatescript inbin/Now, the virtual environment is activated this can be seen by the$ source bin/activate (py38_hack)$ # on Windows $ .\Scripts\activate(py38_hack)being added to the command prompt. - To deactivate the virtual environment just do...
(py38_hack)$ deactivate $ - Installing packages to a virtual environment is just like normal, but the virtual environment must be activated
$ source bin/activate (py38_hack)$ pip list Package Version --------------- ------- pip 21.1.3 setuptools 49.2.1 (py38_hack)$ pip install numpy (py38_hack)$ python -m pip install matplotlib (py38_hack)$ pip list Package Version --------------- ------- cycler 0.10.0 kiwisolver 1.3.1 matplotlib 3.4.2 numpy 1.21.0 Pillow 8.2.0 pip 21.1.3 pyparsing 2.4.7 python-dateutil 2.8.1 setuptools 49.2.1 six 1.16.0
PYTHONPATH is a list of all the directories python searches to find packages when importing. There are several ways to modify your paths. The "difficult" way is modifying the /bin/activate script (similar to modifying your .bashrc file) so PYTHONPATH is appended when the shell/terminal is launched. In this example I'm going to use the "more elegant" way of appending your PYTHONPATH by adding a <name>.pth file to your lib/pythonX.Y/site-packages directory. Python will recognize this file and know to append the files contents to Python's search paths. Continuing with the above example, I'm going to add my plasmapy local repository to to Python's search paths so I can use it the Python console or Jupyter notebooks without having to install plasmapy.
- We want to navigate to the
site-packagesdirectory for our virtual environment.(py38_hack)$ cd lib/python3.8/site-packages - In this directory we create the
plasmapy.pthfile with the folowing contentswhere this is the real absoute path on your own machine./absolute/path/to/my/plasmapy_repo - So, lauching a python console we can now see that the search path has been updated
Note, for
>>> import sys >>> sys.path [ '', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/.../Hack_Week_2021/venvs/py38_hack/lib/python3.8/site-packages', '/.../PlasmaPy', ] >>> import plasmapy >>> plasampy.__version__ '0.7.dev42+g48972c4f.d20210626'plasmapyto import correctly you first must install all of its dependencies into the virtual environment.
-
First we want to install the necessary Jupyter packages to our virtual environment
(py38_hack)$ pip install jupyter (py38_hack)$ pip install jupyterlab (py38_hack)$ pip install ipykernel -
Add your virtual environment to Jupyter notebooks
python -m ipykernel install --user --name py38_hack --display-name "py38_hack"--userinstall for only the curren tuser--nameThe name for kernelspec (internal naming for Jupyter). If the name already exist, then it will be overwritten.--display-nameThe name seen in notebook menus.
-
Now, you can lauch Jupyter notebook and create a new notebook using your
(py38_hack)$ python -m jupyterlabNote, you do not need to activate your virtual envirnment to be able to use it in your Jupyter notebook. Each notebook has its own kernel associated with it and Jupyter will activate the approriat environment for that notebook.
-
To view the currnt kerenls added to Jupyter do the following
(pyt38_hack)$ python -m jupyter kernelspec list Available kernels: py38_hack /.../Jupyter/kernels/py38_hack -
To remove a kernel, one can unistall it by doing...
(py38_hack)$ python -m jupyter kernelspec uninstall py38_hack

