Using a virtual environment with Jupyter notebooks
Good evening!
This is going to be a quick post. I often encounter a situation when I want to test a Python third-party module, which I install into a virtual environment. And I often use Jupyter for quick tests like that. So, how do I make Jupyter use the aforementioned virtual environment in Ubuntu 16.04?
Set up the virtual environment
First, go to the folder where you’re planning to store your environment. Then create it:
cd ~/envs
virtualenv -p /usr/bin/python3.5 geopy_envI have specified the Python interpreter I want to be used, because if I don’t, it’ll set it up for Python 2. I don’t want that.
Next, activate the environment:
source geopy_env/bin/activateNow install the desired modules, geopy in my case:
pip3 install geopyThe environment is ready! Now we need to let Jupyter know about it.
Setting up the virtual environment for Jupyter
First, install the kernel module into the virtual environment:
pip3 install ipykernelAnd run the following command to set it up for Jupyter:
ipython kernel install --user --name="geopy"Opening a Jupyter notebook with the installed environment.
Now you can simply open a new notebook with the required environment:

Or switch the kernel in the existing notebook

In conclusion
That’s it! A very simple way to install environments and subsequently test the modules conveniently.
Thanks for dropping by! I’ll see you eventually.