Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set PYTHONPATH for local Jupyter Notebook in VS Code

I'm using Visual Studio 1.39.2 on Windows 10. I'm very happy that you can run Jupyter Notebook natively through VS Code as of October this year (2019), but one thing I don't get right is how to set my PYTHONPATH prior to booting up a local Jupyter server.

What I want is to be able to import a certain module which is located in another folder (because the module is compiled from C++ code). When I run a normal Python debugging session, I found out that I can set environment variables of the integrated terminal, via the setting terminal.integrated.env.linux. Thus, I set my PYTHNPATH through this option when debugging as normal. But when running a Jupyter Notebook, the local Jupyter server doesn't seem to run in the integrated terminal (at least not from what I can see), so it doesn't have the PYTHONPATH set.

My question is then, how can I automatically have the PYTHONPATH set for my local Jupyter Notebook servers in VS Code?

like image 298
Olov Avatar asked Nov 06 '19 17:11

Olov


People also ask

How do I change the Python path in Visual Studio code?

To do so, open the Command Palette (Ctrl+Shift+P) and enter Preferences: Open User Settings. Then set python.condaPath , which is in the Python extension section of User Settings, with the appropriate path.

How do you create a Jupyter Notebook in Visual Studio code?

You can create a Jupyter Notebook by running the Jupyter: Create New Jupyter Notebook command from the Command Palette (Ctrl+Shift+P) or by creating a new . ipynb file in your workspace. Next, select a kernel using the kernel picker in the top right.

How do I use Python Jupyter notebooks in VS Code?

To work with Python Jupyter Notebooks in VS Code, simply install or update the Python extension. There’s no need to install the Jupyter extension separately. For those who are using the VS Code Insiders build, you may notice that the new preview notebooks experience that was first introduced in July has now been turned on by default.

Does the Python extension for VS Code support Jupyter?

The Python extension for VS Code has shipped with Jupyter Notebook support for over a year with growing popularity. We understand that Jupyter Notebooks are also used with other programming languages beyond Python, such as R, Julia, and Scala.

How to install Jupyter in Visual Studio Code?

The top right of the VS Code UI says "Jupyter server: Not started". This means Jupyter library is currently not installed inside WSL. You will also get two pops, one of which says, "Data science libraries notebook and jupyter not installed". Click on "Install".

How to save and export Jupyter Notebook?

You can use the Ctrl + S keyboard combination or use the save icon on the Notebook Editor toolbar to save Jupyter Notebook. You can export Jupyter Notebook as Python (.py), PDF or HTML files. To export, just click the conversion icon on the main toolbar. Next, you will see the command panel file options.


Video Answer


1 Answers

I'm a developer on this extension. If you have a specific path for module resolution we provide a setting for the Jupyter features called:

Python->Data Science: Run Startup Commands

That setting will run a series of python instructions in any Jupyter session context when starting up. In that setting you could just append that path that you need to sys.path directly and then it will run and add that path every time you start up a notebook or an Interactive Window session.

like image 127
Ian Huff Avatar answered Sep 22 '22 13:09

Ian Huff