0

I have a basic directory strucrure

model_folder
|
|
------- model_modules
|            |
|             ---- __init__.py
|            |
|             ---- foo.py
|            |
|             ---- bar.py
|
|
------- research
|            |
|            ----- training.ipynb
|            |
|            ----- eda.ipynb
|
|
------- main.py

Where model_modules is a folder containing two functions that I use in my model, research is a folder where I put modeling research, etc., and main.py is my main script that tests new data.

My question regards how to import a module, ie model_modules, into a script in a sub-directory. I can import model_modules into main.py just fine by doing the following

from model_modules.foo import Foo
from model_modules.bar import Bar

But when I try the same thing in training.ipynb or eda.ipynb, I get the error

ModuleNotFoundError: No module named 'model_modules'

I want my directory to be "clean" and don't want all my research scripts in the root directory. Is there a fix for this so that I can import model_modules into scripts in research? Or do I need to approach the architecture of this directory differently?

bismo
  • 1,257
  • 1
  • 16
  • 36

1 Answers1

1

I believe your ipynb is not in the same directory as your module.

In this case, you must add the module path as the code below.

Prepare the absolute path of the model_folder.

I suggest this code below.

import sys
sys.path.append('/absolute/path/model_folder')
from model_modules.foo import Foo
from model_modules.bar import Bar

Let's say, your module path which has submodule directories and python files is '/Users/username/custom_module'

import sys
sys.path.append('/Users/username/custom_module')
from model_modules.foo import Foo
from model_modules.bar import Bar

OR you can use the way below. but If you don't want to dive into the Bash and Linux system world, try the first method I suggested.

export PYTHONPATH='/Users/username/custom_module'

more detailed explanation

Constantin Hong
  • 701
  • 1
  • 2
  • 16
  • 1
    Cool. Thanks for the reply. I wonder, is this "pythonic" or a best practice? Or generally considered not a good idea to use sys to prepare an absolute path to import modules? Will be upvoting this, as it does indeed solve my problem. – bismo Nov 22 '22 at 20:00
  • If you are unfamiliar with Linux or a command line, the first method is most convenient for you. There are similar questions like yours ( https://stackoverflow.com/questions/49264194/import-py-file-in-another-directory-in-jupyter-notebook ) – Constantin Hong Nov 22 '22 at 20:07