mpacer mpacer -4 years ago 257
Python Question

import local function from a module housed in another directory with relative imports in jupyter notebook using python3

I have a directory structure similar to the following


When working in
if I try to use a relative import to access a function

from ..project1.lib.module import function

I get the following error

SystemError Traceback (most recent call last)
<ipython-input-7-6393744d93ab> in <module>()
----> 1 from ..project1.lib.module import function

SystemError: Parent module '' not loaded, cannot perform relative import

Is there any way to get this to work using relative imports?

Note, the notebook server is instantiated at the level of the
directory, so it should have access to the information in those files.

Note, also, that at least as originally intended
wasn't thought of as a module and therefore does not have an
file, it was just meant as a file-system directory. If the solution to the problem requires treating it as a module and including an
file (even a blank one) that is fine, but doing so is not enough to solve the problem.

I share this directory between machines and relative imports allow me to use the same code everywhere, & I often use notebooks for quick prototyping, so suggestions that involve hacking together absolute paths are unlikely to be helpful.

Edit: This is unlike Relative imports in Python 3, which talks about relative imports in Python 3 in general and – in particular – running a script from within a package directory. This has to do with working within a jupyter notebook trying to call a function in a local module in another directory which has both different general and particular aspects.

Answer Source

I had almost the same example as you in this notebook where I wanted to illustrate the usage of an adjacent module's function in a DRY manner.

My solution was to tell Python of that additional module import path by adding a snippet like this one to the notebook:

import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:

This allows you to import the desired function from the module hierarchy:

from project1.lib.module import function
# use the function normally

Note that it is necessary to add empty files to project1/ and lib/ folders if you don't have them already.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download