简体   繁体   中英

Importing custom module into jupyter notebook

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:

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.

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

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 .

Try the following line:

from my_project.helpers import what_you_need

This line should also work:

import my_project.helpers

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.

In addition to answer of @Taras-Alenin,

To those who develop own modules and import these modules in their notebooks, I recommend you to restart kernel. Otherwise notebook will not catch updates of modules

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

If you are on a Unix/Linux system another elegant solution may be creating a "soft link" to the module file helpers.py that you would like to use. Change to the notebooks directory and create the link to the module file this way:

cd notebooks; ln -fs ../my_project/helpers.py .

This "soft link" is essentially a pointer (a shortcut) to the original target file. With the link in place you will be import your module file as usual:

import helpers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM