32

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?

4 Answers 4

28

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}"
}
Sign up to request clarification or add additional context in comments.

5 Comments

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?
We have to remember that Python looks at physical folders to import modules. The environment variable PYTHONPATH is a semicolon delimited list of folders where Python will search for modules to be imported. Python will interpret this environment variable and set the value of sys.path accordingly.
@user2739472 not exactly.
If you're trying to get an existing notebook to work, make sure to close and reopen it after adding this setting! :)
This saved me from a massive headache where launching my script from the CLI python myscript.y was working perfectly but executing it from inside VSCode Ipython Interactive window was not, raising: "ModuleNotFoundError: No module named X"
7

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.

4 Comments

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?
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 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. github.com/microsoft/vscode-python/issues. That's the spot that we track and triage all our work items.
@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.
3

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.)

1 Comment

Tested on linux, not on windows. Path separator might be different on windows?
1

Here's a testcase .ipynb:

https://gist.github.com/p-i-/6fa0171819b2647fa7ba0a6f0dc0efb4

TLDR:

# .vscode/settings.json
{
    "jupyter.notebookFileRoot": "${workspaceFolder}/tools",
    "python.analysis.extraPaths": [
        "./tools"
    ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.