5

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.

asa
  • 675
  • 2
  • 7
  • 17
  • 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. – Ian Huff Dec 16 '19 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. – asa Dec 17 '19 at 16:02
  • I actually just realized that I mistyped the variable. It's ${fileDirname} not ${fileDirName} does that work instead? – Ian Huff Dec 17 '19 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. – asa Dec 17 '19 at 22:54

2 Answers2

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

AzimuthLi
  • 29
  • 2
0

You can use this

% run "model.py"
Heng Sengthai
  • 483
  • 1
  • 4
  • 10