6

Within the same project directory, I have one file solution.ipynb and other called model.py. In the file solution.ipynb I have to import the module model.py.

Altough both files are in the same directory, when I use the below code:

import import_ipynb
import model

I still get the error: ModuleNotFoundError: No module named 'model'

Please note: I'm using the VSCode to open and write on the .ipynb file. Strangely enough, when I open the .ipynb file with Jupyter lab, the module model.py can be imported without problems.

What I've already tried: (1) install the import-ipynb and reference it above (as seen in the first line of the code) and (2) add a __init__.py file within the directory where the .ipynb is located.

4
  • I'm a developer on this extension. My guess as to what is happening here is that VS Code uses a different default root versus Jupyter. Jupyter defaults to looking next to the location of the ipynb file, but VS Code looks at the root of the currently open workspace. If your .ipynb is not in the same location as your workspace root this would cause the .py file to be to be found. Is that the case for you? If so changing the setting "python.dataScience.notebookFileRoot" in VS Code to "${fileDirName}" might work for you as that sets the working directory to be relative to the ipynb file open. Commented Dec 16, 2019 at 23:17
  • That's right. The problem only arises when I have more than two projects opened at the same time in my VSCode workspace. However, inserting the relative path in the settting.json isn't solving the problem. VSCode is still taking the working directory from the project at the top of my workspace. Commented Dec 17, 2019 at 16:02
  • I actually just realized that I mistyped the variable. It's ${fileDirname} not ${fileDirName} does that work instead? Commented Dec 17, 2019 at 17:30
  • I used both but unfortunately they don't solve the problem. The working directory is still being refered to another project (the project at the top of my VSCode workspace). I tried to change the working directory inside of the file.ipynb with the command "os.chdir('path')" and although it does change the working directoy, when I try to import the file.py, I again get the ModuleNotFoundError. Commented Dec 17, 2019 at 22:54

2 Answers 2

2

As mentioned by Ian Huff in his comment, the problem raised because the PATH variable set by VSCode does not look into the folder where your .ipynb file sits, when you have more than one level of directory. It only look into the top level. To point out the path of your folder explicitly, add below code at the beginning of your .ipynb:

import sys
sys.path.insert(0, ".")

Note:

You can change the path accordingly, e.g. if your .py module is in the parent folder of the .ipynb, use ".."; if it is in another folder parallel to the folder of the .ipynb file, use "../another_dir".

see more here in the comment: https://stackoverflow.com/a/42727538/14237798

Sign up to request clarification or add additional context in comments.

1 Comment

that worst my situation.
0

You can use this

% run "model.py"

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.