Yes, I know this is a recurrent question but I still couldn't find a convincing answer. I even read at https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html but could not find out how to solve the problem:
I'm running python 3.6 project that includes jupyter (ipython) notebooks. I want the notebook to import a custom local helpers.py
package that I will probably use also later in other sources.
The project structure is similar to:
my_project/
│
├── my_project/
│ ├── notebooks/
│ └── a_notebook.ipynb
│ ├── __init__.py # suppose to make package `my_project` importable
│ └── helpers.py
│
├── tests/
│ └── helpers_tests.py
│
├── .gitignore
├── LICENSE
├── README.md
├── requirements.txt
└── setup.py
When importing helpers
in the notebook I get the error:
----> 4 import helpers
ModuleNotFoundError: No module named 'helpers'
I also tried from my_project import helpers
and I get the same error ModuleNotFoundError: No module named 'my_project'
I finally (and temporarily) used the usual trick:
import sys
sys.path.append('..')
import helpers
But it looks awful and I'm still looking for a better solution
One can tell python where to look for modules via
sys.path
. I have a project structure like this:I was able to load the module like so:
One should be able load the module from wherever they have it.
This worked for me.
If you move the
notebooks
directory out one level, and then explicitly import your module from the package, that should do it. So your directory would look like this:and then your import statement within the notebook would be:
from my_project import helpers
.I think you need a
__init__.py
module in thenotebooks/
directory. I haven't really used Jupyter notebooks before so I could be wrong. You may also need to try changing your import statement to:to indicate that the import statement is for a local package that is located in the parent directory of the Jupyter notebook.
Here I could find several solutions. Some of them are similar to the ones answered before: https://mg.readthedocs.io/importing-local-python-modules-from-jupyter-notebooks/index.html
Try the following line:
from my_project.helpers import what_you_need
This line should also work:
import my_project.helpers