What are Python Virtual Environments
and what are they used for? Well, instead of reinventing the wheel, let’s see what the Python Docs have to say about it:Creating your Virtual Environment
If you are using Python 2, you can install Virtualenv using your command line application with (change venv
to whatever name you’d like):
pip install --user virtualenv
virtualenv venv
Python ≥ 3.3 has native support for this operation :
python -m venv venv
After you’ve created your environment, activate it with:
source venv/bin/activate
To deactivate your virtual environment, run deactivate
. To delete, simply remove the folder with the virtual environment, rm -r venv
.
For further information, take a look at the Virtualenv or Venv Documentation.
Using Anaconda?
Anaconda is a Python and R tool designed to simplify package management and, thereby, speed up your development. Start by installing the Anaconda package. Now you can create your environment with:
conda create -n venv
You can specify a different version like so:
conda create -n venv python=3.1
Now you can activate your new environment:
conda activate venv
Similarly to our previous example, deactivate the environment with conda deactivate
and remove with conda env remove -n venv
.
More detailed information available in the Anaconda Documentation.
Add Packages to your Virtual Environment
Now that you have your environment installed, you’ll notice it is a fresh install of Python. Make sure you have activated your environment as shown above (source venv/bin/activate
/ conda activate venv
), and then add any packages you are going to work with using your favorite package installer (e.g. pip install package
)
Connecting to Jupyter
First, activate your environment with source venv/bin/activate
or conda activate venv
. Next, install iPyKernel which provides the IPython kernel for Jupyter:
pip install --user ipykernel
Add your kernel to Jupyter with:
python -m ipykernel install --user --name=venv
You should get the following feedback:
Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/venv
Now you can select your kernel from your Jupyter notebook!
If you’d like to disconnect your kernel in the future, use jupyter kernelspec uninstall venv
Happy coding!
If you’re having trouble getting your Jupyter Notebook up and running, take a look at this article on How to fire up a Jupyter Notebook on Mac.