Importing custom module into jupyter notebook

2020-06-09 03:37发布

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

6条回答
孤傲高冷的网名
2楼-- · 2020-06-09 04:15

One can tell python where to look for modules via sys.path. I have a project structure like this:

project/
    │
    ├── src/
    │    └── my_module/
    │        ├── __init__.py       
    │        └── helpers.py
    ├── notebooks/
    │   └── a_notebook.ipynb
    ...

I was able to load the module like so:

import sys
sys.path.append('../src/')

from my_module import helpers

One should be able load the module from wherever they have it.

查看更多
迷人小祖宗
3楼-- · 2020-06-09 04:28

This worked for me.

import sys

MODULE_FULL_PATH = '/<home>/<username>/my_project/my_project'

sys.path.insert(1, MODULE_FULL_PATH)

from my_module import helpers
查看更多
小情绪 Triste *
4楼-- · 2020-06-09 04:29

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:

my_project/
│
├── my_project/
│   ├── __init__.py       
│   └── helpers.py
├── notebooks/
│   └── a_notebook.ipynb
...

and then your import statement within the notebook would be:

from my_project import helpers.

查看更多
beautiful°
5楼-- · 2020-06-09 04:32

I think you need a __init__.py module in the notebooks/ 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:

import .. helpers

to indicate that the import statement is for a local package that is located in the parent directory of the Jupyter notebook.

查看更多
家丑人穷心不美
6楼-- · 2020-06-09 04:38

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

查看更多
Viruses.
7楼-- · 2020-06-09 04:40

Try the following line:

from my_project.helpers import what_you_need

This line should also work:

import my_project.helpers

查看更多
登录 后发表回答