ModuleNotFoundError: No module named… Jupyter Notebooks

Question:

dev is my root directory. I have the below files.

devsitehealthchecktest.py
devsitehealthcheckdraw.py

In test.py, the first line reads:

from sitehealthcheck.draw import *

If I run test.py in the VSCode terminal, everything works as expected. However, when I try to execute test.py in the interactive window, Jupyter returns:

ModuleNotFoundError: No module named ‘sitehealthcheck’

What can I do so VSCode automatically searches for modules in the same directory as the file I’m executing?

I would prefer just to type the below line.. and, have the VSCode editor/Intellisense and Jupyter to automatically search for modules in the same directory as the file I’m executing.

from draw import *
Asked By: MKANET

||

Answers:

For the interactive window the current working directory is not set to the path of the file being executed, instead it’s set to the path of the workspace folder that you have open in VSCode. If I have the following:

WorkspaceFolder:
    SubFolder:
      MyScript.py
      ImportMe.py

If you run MyScript.py in the terminal you would want this:

from importme import * since the file location is added to the path

But if you are using the interactive window you can either add to your PythonPath as suggested above. Or you can use this:

from SubFolder.importme import *

Since the CWD is set to the workspace root for the Interactive Window session.

Answered By: Ian Huff

If you add .../dev/sitehealthcheck to your PYTHONPATH, then you can import draw.

Answered By: New Alexandria

To solve this problem, you can search for a setting in the Python extension which is called “Python › Data Science: Notebook File Root”. The line below the title of this setting says: “Set the root directory for loading files for the Python Interactive window.”. Then you change ${workspaceFolder} to ${fileDirname}, close the interactive Python terminal and restart it, and it should work.

Answered By: xuanphong

In support of the existing answers, you need to add the jupyter.notebookFileRoot line to your settings.json file in your workspace when using a notebook in a subfolder and modules in a sibling folder:

.vscode/settings.json:

{
    "python.pythonPath": "env/bin/python",
    "jupyter.notebookFileRoot": "${workspaceFolder}"
}
Answered By: Felipe

Easy way to set proper kenral and venv. Try setting proper venv like below.

enter image description here

Answered By: Aamer

if error in jupyter notebook

  1. open -: anaconda prompt
pip install import-ipynb
  1. open main file

import import_ipynb

import first

Answered By: GOPAL19