Do you use jupyter notebooks? And virtual environment too, right? And do you know how to use them together? Ah! I got you. It is very simple: follow this guide to learn how to install a custom kernel.
Using virtual environments is important:
it helps you to maintain your system clean: don’t install system-wide libraries that you are only going to use once for a small project
it allows you to use a certain version of a library for one project and another version for another project: if you install the library system-wide and don’t use venv, then you can only use one version of the library
it helps reproducible research: you work hard on your project, you write a paper on it and then you release the code; one year later, the libraries you used are outdated but your package still works: by listing the particular versions that you used, another researcher can set up a new virtual environment and use those outdated libraries to reproduce your work
Basically, virtual environments simplify your life when you work with Python.
How to do it
I assume that you are a tidy person: you have all your work sorted out in folders; one of these folders contains your very important project and it looks like this:
Inside this folder create a new virtual environment:
python -m venv projectname
Then activate it:
Now, from inside the environment install ipykernel using
pip install ipykernel
And now install a new kernel:
ipython kernel install --user --name=projectname
At this point, you can start jupyter, create a new notebook and select the kernel that lives inside your environment.
$ python -m venv projectname
$ source projectname/bin/activate
(venv) $ pip install ipykernel
(venv) $ ipython kernel install --user --name=projectname
Here is a screencast that shows the whole thing:
Did you like this article?
Leave your email and I’ll send the next one I write directly to your inbox 📫.
Do you want to discuss the content with me? I’d be happy to hear from you: hit me on twitter @angelo_basile or send me an email.