23

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?

Olov
  • 1,103
  • 11
  • 27

3 Answers3

14

I found that you can set this for Jupyter notebooks in the settings.json file.

{
    "jupyter.notebookFileRoot": "/path/to/your/module/root"
}

Edit: Or, to set it at your workspace root more generically:

{
    "jupyter.notebookFileRoot": "${workspaceFolder}"
}
Alecg_O
  • 892
  • 1
  • 9
  • 19
  • Shouldn't the PYTHONPATH point to the Python executable? This seems to be configuring the dir where the notebook is to be found, not which Python executable Jupyter should use. Isn't that what OP is asking? – user2739472 Aug 08 '23 at 15:23
  • @user2739472 [not exactly](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH). – Alecg_O Aug 08 '23 at 16:56
5

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.

Ian Huff
  • 2,699
  • 1
  • 17
  • 17
  • I can't get this to work. I test it simply by assigning a variable in the startup commands like `hi = 42` and then create a new blank Jupyter notebook via the command palette with the line `print(hi)`. Then, if I try to run the notebook, the variable is not defined. What am I doing wrong? – Olov Nov 14 '19 at 14:05
  • 1
    This worked for me by adding `sys.path.append('./src') where src is the folder I'm putting my Python, but it feels like a really clunky way to "inject" my project's source code into the Jupyter environment. @IanHuff are there any improvements to this being considered? – janh Apr 19 '20 at 12:28
  • @janh Sorry for the slow reply. I'm not on SO that often. If you would like you could create a github issue for this suggestion here. https://github.com/microsoft/vscode-python/issues. That's the spot that we track and triage all our work items. – Ian Huff May 15 '20 at 23:10
  • @IanHuff I added an answer I think may be an evolution of the feature you describe? I couldn't find the `Run Startup Commands` or any other Data Science settings at this time. – Alecg_O Oct 05 '22 at 00:06
1

I had same problem importing a python module developed in C++. I added the relative path to my build directory from my notebook to the .env file in my workspace folder.

PYTHONPATH=build/lib:../../../build/lib

The path from the workspace folder is build/lib, from my notebook folder is ../../../build/lib, and the path separator is :.

(The reason I have the build/lib path, from the workspace folder, is for pytest.)

TooTone
  • 7,129
  • 5
  • 34
  • 60