▄▄·       ·▄▄▄▄  ▄▄▄ .• ▌ ▄ ·. ▄• ▄▌ ▄▄·  ▄ .▄
▐█ ▌▪▪     ██▪ ██ ▀▄.▀··██ ▐███▪█▪██▌▐█ ▌▪██▪▐█
██ ▄▄ ▄█▀▄ ▐█· ▐█▌▐▀▀▪▄▐█ ▌▐▌▐█·█▌▐█▌██ ▄▄██▀▐█
▐███▌▐█▌.▐▌██. ██ ▐█▄▄▌██ ██▌▐█▌▐█▄█▌▐███▌██▌▐▀
·▀▀▀  ▀█▄▀▪▀▀▀▀▀•  ▀▀▀ ▀▀  █▪▀▀▀ ▀▀▀ ·▀▀▀ ▀▀▀ ·
android, iOS, web :: hacking + reverse engineering

Using VSCode with Jupyter Notebooks and Python Virtual Environments

python programming jupyter vscode coding

Set up a Jupyter Notebook in Visual Studio Code using Python virtual environments.

If you attempt to set up a virtual environment, and import a third-party Python module like Imbalanced Learn in Visual Studio Code using the official Jupyter Notebook extension, you may run into the following error:

ModuleNotFoundError: No module named 'imblearn'

This error is due to the fact that your VS Code isn't configured to use the correct Python interpreter for your virtual environment. If you attempt to troubleshoot, you may run into additional questions regarding Jupyter “kernels” and IPython. In the context of Jupyter, a “kernel” is an execution environment that allows your code to be run in a particular notebook. Jupyter supports languages other than Python, and IPython is what powers Jupyter's Python kernel. I found that it was easiest to fix this issue by starting from scratch with a clean virtual environment, which includes installing a new Jupyter kernel, and associating that new kernel with your Jupyter notebook in VS Code.

By the end of this short tutorial, you should have a new Jupyter Notebook inside of Visual Studio Code (Version: 1.65.2 as of this writing), as well as the ability to import third party modules and run them from inside of that notebook. The benefit of using virtual environments is to make it easy to install third party modules your project needs, while avoiding the clutter you'd end up with if you were to install the modules globally. It's generally a good practice to use a separate virtual environment for each project you create.

This guide assumes you already have Python 3, and Visual Studio Code with the official Jupyter Notebook extension installed. I wrote this with MacOS in mind, but you should be able to follow the same steps on Linux or Windows with some modifications.

  1. Create a new directory to host your project, set up a new virtual environment called venv, and activate that virtual environment. I created mine in ~/dev/jupyter-learn, but you can use whatever you like.

    mkdir ~/dev/jupyter-learn; cd $_
    python3 -m venv env
    source ./env/bin/activate

Note: Remember that the rest of these Terminal commands will need to be run from your newly-created virtual environment. If you type them into another tab or Terminal window without first activating this environment, you'll likely run into issues.

  1. Use your newly-created virtual environment to install the required modules (ipykernel and jupyter):

    pip install ipykernel jupyter
  2. To demonstrate the ability to install third-party modules, I'm also going to install the Imbalanced Learn module as an example:

    pip install imbalanced-learn
  3. Now, create a new ipykernel to use for this project. I'm calling it jupyter-learn-kernel, but you're free to select a name that works for your project:

    python -m ipykernel install --name=jupyter-learn-kernel
  4. Create a new notebook, and open it in Visual Studio Code:

    touch demo.ipynb
    open demo.ipynb

Now, when the notebook opens up in Visual Studio Code, click on the Select Kernel button on the upper-right and select jupyter-learn-kernel (or whatever you named your kernel). If you don't see your kernel in the list, close Visual Studio Code completely, and re-open it.


If everything worked, you should be able to import your library inside of Jupyter Notebook in VSCode. To test, click Run All and make sure there are no errors: