How to Use Jupyter Notebooks with Visual Studio Code (and Anaconda)
48K views
May 13, 2022
This tutorial demonstrates how to setup Visual Studio Code to work with Python Jupyter notebooks. We'll use the Anaconda python distribution to create and manage environments. Instructions and code: https://opensourceoptions.com/blog/jupyter-notebooks-in-visual-studio-code/ Sign up for email notifications (https://opensourceoptions.com/subscribe) and get $5 off any course at https://opensourceoptions.com/course-list
View Video Transcript
0:00
Welcome to open source options. This tutorial will demonstrate how to use Python Jupyter
0:07
notebooks with Visual Studio code. Now before we start, I want you to know that I've just
0:13
barely uninstalled Visual Studio code and reinstalled it with a fresh install on my
0:18
machine. So I'm starting fresh just like you will be. That said, there are some
0:22
settings that can get saved locally and so yours may not appear exactly the same as
0:27
mine. Now over here in my editor you can see I have a project open with some code. There's some
0:33
Python files and some Python some Jupyter notebook files over here. Now getting this to run with
0:39
Jupyter notebook in Visual Studio code is actually really, really simple. All we need to do is go over
0:45
to the extensions panel and you can see that I have six extensions installed here. Now these were
0:51
installed when I opened up Visual Studio code right after I installed it. So you may have these
0:55
installed already. I have Jupiter, Jupiter Keymap, Jupiter notebook renderers, live server
1:03
Pylance, and Python. Now, if you don't have these installed, what you can do is you can come up here
1:10
to your extensions panel over here on the left side, go into the extensions and start searching
1:14
for these. So I can start typing Python and all these Python extensions will come up. And so you can
1:25
see just you can just click install over here to install those. Okay and you have the
1:31
extension pack but like I said I gonna get rid of this I gonna go to my installed I only have these six installed and things will work Let go back over to the code
1:43
And so once you have those extensions installed, if I click on, let's say this
1:50
create arrays, I Python notebook, you can see that it's rendered now as a Jupyter notebook
1:58
And I can go in here, after I select the kernel, so you can see up here it says kernel, if I try to run this cell right now, let's see what happens
2:07
It shift editor and it pops up and says you need to select the kernel. So let's go ahead and we'll select the from a conda environment
2:14
This is the base kernel. And now if I run this, it's shift enter to run the cell
2:23
It's running. So you can see the time right there. Now I can run this markdown
2:27
And now I can run this next cell. You can see it gives me the time it took to run and it gives me an output and we can just work through our notebook just like this and you can see that
2:38
Okay. Now, let's say let's let's give something a test. Let's say we have an Anaconda environment, which let's go ahead and just create a new one
2:51
And let's do Konda create and name. call this VSC and Python equals 3.9
3:05
So let's create this environment. And once it created it we activate over here Now notice what I doing this is I haven actually installed Jupiter for this environment yet
3:21
And I'll show you that once it finished installing. So I'm just going to give this a quick pause here while we install
3:33
Okay, we're all ready to go here. So let's do Konda activate VSC
3:40
and then let's do condo list jupiter and this will tell us if it's installed you can see there's no installed packages
3:52
so let's come back over to visual studio code let's change this kernel to vSC okay and now let's see if
4:03
this runs even though jupiter is not actually installed and actually numpy's not installed so let's go
4:09
actually first and do make sure Honda install Numpi. Make sure we have that so this code will run
4:20
This will take just a second here. So I'm going to pause this while this installed
4:28
Okay, so we've got NNP installed. And if you're curious about how to link up Anaconda with Visual Studio code
4:35
It's really easy. I have a tutorial for that on the website
4:39
So you go ahead and check that out. Let's go ahead and run this now. I'm going to hit shift enter to run that code. Requires
4:47
Yeah see so it says that we don have the high kernel package which we can do this
5:00
So let's come over here and let's do Konda install Jupiter. And hopefully this will fix the problem for us
5:09
So let's go ahead and do that. Okay. Let's go ahead and say yes
5:17
And once again, I am going to stop. Okay, so now this is almost an installing
5:22
I just want to point one thing out here, and you'll notice iPi kernel right here is now installed as part of Jupiter
5:30
So now let's come back over here, and let's try to run this again
5:35
Notice we still have our kernel VSC, which is the one I'm working on with the installs with Konda
5:41
Let's hit shift enter. and let's see here let's restart it'll go to running cell there we go. Now we're running. You can see
5:51
the Numpy was now imported. We can run this markdown and we can run this code. So there you go
5:59
That is how you can set up Visual Studio code to use Jupyter notebooks. And then you can develop
6:07
these directly in VSC and not have to interact with the web interface, which I like the
6:14
VSC interface quite a bit better than the default Jupyter web interface. Hopefully this has been
6:19
helpful for you. If you have questions, go ahead and put them in the chat and don't forget
6:24
or in the comments, and don't forget to check out the tutorial on the website. Thanks for watching