#Anaconda – How to create a custom #Python virtual environment and use it in #Jupyter notebooks (a kernel!)

Hi!

In yesterday post, I created a new virtual environment named [devtf] and in this environment I’ve installed a lot of tools that I need. Then I tried to launch a jupyter notebook from this environment, to use this tools and, of course, it didn’t work.

anaconda start virtual environment and error on launch jupyter notebook

It was time to read and learn how this works. So, when I finally get this I find this amazing article which really explain how this works “Using Virtual Environments in Jupyter Notebook and Python” (see references)

Jupyter Notebook makes sure that the IPython kernel is available, but you have to manually add a kernel with a different version of Python or a virtual environment. First, you need to activate your virtual environment. Next, install ipykernel which provides the IPython kernel for Jupyter. And finally, you can add your virtual environment to Jupyter.

So the commands are

pip install --user ipykernel 
python -m ipykernel install --user --name=devtf

Where “devtf” is the name of the new kernel you want to create. Now, when I launch Jupyter Notebooks, the new kernel is available to be used

jupyter notebook change kernel to one with tensorflow

When I started to use this new kernel (virtual environment) I realized that I didn’t installed TensorFlow. You know, being happy about this, naming the kernel TF but not installing the core component. And, sure, my notebooks didn’t work.

jupyter notebook with kernel without tensorflow

I went to my terminal / command prompt and installed TensorFlow. Then I only need to restart the Kernel, and everything start working. I added a extra couple of lines in my notebook just to check the TensorFlow and keras versions.

jupyter notebook tf ok and test keras version

I find similar errors with another packages, so I pip installed the packages in the terminal and restart the kernel to have the notebook OK. So, my simple reminder for myself about how to do this!

Happy coding!

Greetings @ Mississauga

El Bruno

References

Advertisement

2 comments

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: