Using Jupyter notebooks inside VS Code
VS Code has native integration with Jupyter notebooks. That means we can run Jupyter notebooks inside of VS Code without running the Jupyter server.
Steps:
- Install Python
- Install the Python and Jupyter VS Code extensions
- Create a venv
- Install any notebook related packages in this venv
- Start your notebook
1. Install Python
brew install python3
There is an existing python3
in /usr/local
installed as part of macOS
Command Line tools, we can ignore that. The shell also ignores it because
/opt/homebrew
appears earlier in the PATH
.
After this, Python will be available to us under the name "python3", not "python". This is fine, we'll fix this below when we create a venv.
2. Install the Python and Jupyter VS Code extensions
(Optional) Create a new profile
We can create a new profile in VS Code to isolate the extensions to the current workspace. Create a new folder somewhere, I called it "Python", and open it in VS Code. This folder will serve as our workspace.
Now run the "Profiles: Create Profile..." command in VS Code. Use the default profile as the basis.
The workspace to profile association is automatic , so anytime we open this folder, this profile will get activated, and any extensions we install when in this profile will be activated too.
(Then) Install VS Code extensions
Install the Python and Jupyter VS Code extensions – both are from Microsoft.
3. Create a venv
A Python venv is a virtual environment that allows us to install packages without installing them globally .
In VS Code we can do that by running the "Python: Create Environment" command.
Under the hood, or if we want to manually create a new venv, this is what it does
# create a new virtual environment
python3 -m venv .venv
# optional step - upgrade pip if needed
# pip is the python package manager
python3 -m pip install --upgrade pip
# activate it
source ./.venv/bin/activate
We could run these commands ourselves, the end result will be the same.
VS Code will also helpfully activate the venv (the last command above) whenever
we open this workspace again, or open a new integrated terminal inside
it . If
we want to activate the venv manually, say if we open an external Terminal to
this folder, we can run source ./.venv/bin/activate
.
Any packages we install now will live in the local .venv directory in our workspace.
Nicely, when we're, in the venv we'll be able to use the python
command instead
of the python3
command since the venv creates a symlink for it. This is also a
good way to check that we're indeed in an active venv (although the shell prompt
should also have updated):
(.venv) $ which python
.../.venv/bin/python
4. Install any notebook related packages in this venv
This step is optional, because when we create a new notebook later on and try to run a code block in it, VS Code will ask us to select a "kernel" (think of them as supported language packs) to run the code, and when we'll select our Python venv, it'll helpfully also ask us if we want to install the Python packages that provide the kernel.
But if we want to, we can install these packages manually beforehand
“To work with Jupyter (formerly IPython Notebook) Notebooks in VS Code, we need to activate a Python environment in which we've installed the Jupyter package.”
by running the following command:
pip install ipython ipykernel
Note in particular that we didn't need to install the Jupyter (Python) package itself .
The VS Code data science tutorial also mention other packages that we might want to pip install in the future:
“if you'd prefer not to use Anaconda or Miniconda, you can create a Python virtual environment and install the packages needed for the tutorial using pip. If you go this route, you will need to install the following packages: pandas, jupyter, seaborn, scikit-learn, keras, and tensorflow.”
5. Create New Jupyter Notebook
Use the "Create: New Jupyter Notebook" command to create a new .ipynb
file.
The first and only cell in the blank notebook should be selected, as indicated
by the highlight in its left gutter. Press Enter to run the
"Notebook: Edit Cell" command.
The cursor will be inside the cell now. Type "hello". Press Ctrl - Enter to run the "Notebook: Execute Cell" command.
VS Code will ask you to select the kernel. Select the venv we created previously.
That's it. We can now interactively type and run Python code in this REPL / file mixture.
Next steps:
Dec 2023