3

I am writing my modules in jupyter notebooks and store them in a folder, say, /Users/me/myModules (Note: the actual path contain spaces, does it matter?)

I would like to import them from other jupyter notebooks which are in different locations. Since the modules have .ipynb extension, I start my code with import import_ipynb.

I followed this answer and added

export JUPYTER_PATH="${JUPYTER_PATH}:/Users/me/myModules"

to the top line of my bash_profile file, and saved. Then I run source ~/.bash_profile in terminal.

Now, when I run jupyter --paths in terminal, I can see the path to myModules folder under the data section (not the config or runtime sections).

However, in my jupyter notebook

import import_ipynb
import myFirstModule

gives a ModuleNotFoundError: no module named 'myFirstModule'. I am using Python 3.6.10 (Anaconda).

How can I import the module from the jupyter path?

(Actually I found a solution by simply typing %cd '/Users/me/myModules' at the beginning of each notebook, but then every saved file (for example exported image) goes in the myModules folder, which is something I don't want)

ThePortakal
  • 229
  • 2
  • 10
  • 1
    Have you tried adding your path to `PYTHONPATH` as well? I.e. ```export PYTHONPATH="$PYTHONPATH:/Users/me/myModules"``` – Tomasz Bartkowiak Feb 06 '20 at 23:45
  • @TomaszBartkowiak Yes I tried that, didn't work. I don't know if it will help, but the first line of my bash profile is export PATH="/Users/me/anaconda3/bin:$PATH" (I didn't write that line) – ThePortakal Feb 07 '20 at 00:05
  • Do you have an `__init__` file inside `/Users/me/myModules`? Does `which python` point to the appropriate anaconda environment? Are you using the same environment in your notebook? (What's the output of `!which python` inside the notebook)? Is your module in the output of `import sys; sys.path`? – Tomasz Bartkowiak Feb 08 '20 at 11:29
  • @TomaszBartkowiak I have an empty file titled __init__.py (two underscores before and after init) file inside myModules folder. which python in terminal and !which python in notebook gives the same folder: /Users/me/anaconda3/bin/python . import sys; sys.path includes myModules folder – ThePortakal Feb 09 '20 at 11:05

2 Answers2

1

I cannot reproduce your issue without additional details but I would like to provide you with some work-around that should let you load your modules in /Users/me/myModules without changing the directory in Jupyter:

import os
from importlib.util import spec_from_file_location, module_from_spec

path = 'full/path/to/Users/me/myModules'

for fn in os.listdir(path):
    fp = os.path.join(path, fn)
    spec = spec_from_file_location(fn, fp)
    mod = module_from_spec(spec)
    spec.loader.exec_module(mod)

After running this piece of code, your modules will be already imported and free to use.

Arn
  • 1,898
  • 12
  • 26
0

I think the best way to perform "inter-notebook" imports is using %run magics:

%run `full/path/to/the/notebook.ipynb`

... and there is no %cd.

Here is an example: 2 notebooks are located in different locations, mySecondModule performs import of myFirstModule.

m1 m2

trsvchn
  • 8,033
  • 3
  • 23
  • 30
  • Note that this may overwrite some of the names in your second module. Your solution is an equivalent to `from module import *`, which in some cases might not be the best approach. – Arn Feb 15 '20 at 13:51