69

In Jupyter my own little module is not loaded but in python/bpython is everything is fine. When typing

import sys
print(sys.path)

the path to my module will not in show in Jupyter but in python/bpython it is still there.

I am using:

  1. PYTHONPATH in .bashrc to include my module,
  2. Jupyter and bpython inside a virtualenv.

The most similar questions is this Cannot import modules in jupyter notebook; wrong sys.path

How to configure Jupyter to load my modules automagically?

Community
  • 1
  • 1
ulf
  • 889
  • 1
  • 7
  • 5
  • 5
    have you tried running `sys.path.append('/path/to/your/code')` within the Jupyter ipython shell or notebook? If that works, you can add this command to the ipython profile. – miraculixx Jan 24 '16 at 14:46
  • 2
    thank you for your hint with the ipython profile. I was not aware that ipython/jupyter does it own thing. A good instruction what to do is found here http://www.lucypark.kr/blog/2013/02/10/when-python-imports-and-ipython-does-not/ – ulf Jan 25 '16 at 14:16
  • One general comment is to run both through an anaconda environment. That way you a) do not mess with system files, b) have both running the same versions of packages and avoid confusions. Also for your module, it's quite easy to write a setup.py and install an ediatble version of it in the environment. That way you can do ``` import my module``` straight away. – GrigorisG Mar 26 '17 at 12:04

6 Answers6

55

Here is what I do on my projects in jupyter notebook,

import sys
sys.path.append("../") # go to parent dir
from customFunctions import *

Then, to affect changes in customFunctions.py,

%load_ext autoreload
%autoreload 2
Dogan Askan
  • 920
  • 8
  • 20
  • 7
    Do you really want to use `append`? I would suggest that it is better to prepend; e.g. `sys.path.insert(0, "..")`. If you were to prepend ".." to your path, then the parent directory would be searched first, which is arguably what you should want to happen. Also, this is consistent with how Jupyter notebook modifies the path -- it puts the path of the current notebook directory as the first item on the path. – David J. Aug 14 '19 at 04:50
  • What does "affect changes in customFunctions.py" mean? Do you mean you modified customFunctions.py after starting the notebook? – flow2k Oct 08 '19 at 06:50
  • 1
    @flow2k it means; when/if you make changes on that file you don't need to re-start the notebook. The notebook reloads automatically the objects after every changes. – Dogan Askan Oct 08 '19 at 15:09
19

Jupyter is base on ipython, a permanent solution could be changing the ipython config options.

Create a config file

$ ipython profile create
$ ipython locate
/Users/username/.ipython

Edit the config file

$ cd /Users/username/.ipython
$ vi profile_default/ipython_config.py

The following lines allow you to add your module path to sys.path

c.InteractiveShellApp.exec_lines = [
    'import sys; sys.path.append("/path/to/your/module")'
]

At the jupyter startup the previous line will be executed

Here you can find more details about ipython config https://www.lucypark.kr/blog/2013/02/10/when-python-imports-and-ipython-does-not/

Mattia Fantoni
  • 561
  • 5
  • 10
  • 1
    While this works, it looks like it does nothing but executing the append for users at startup. Is there a better way where we can directly modify the path displayed by sys.path? – Sandbo Aug 12 '20 at 11:41
  • For me it worked after using absolute paths (so '/home/user/module' instad of '~/module' ) – Gabriel Apr 07 '21 at 08:39
12

Jupyter has its own PATH variable, JUPYTER_PATH.

Adding this line to the .bashrc file worked for me:

export JUPYTER_PATH=<directory_for_your_module>:$JUPYTER_PATH
N. P.
  • 463
  • 4
  • 10
  • 7
    I have tried this several times in the past and can't remember it ever working. This is definitely not the correct solution. – Peter May 25 '19 at 20:42
  • This doesn't work; it doesn't allow one to import python modules from a path added to `JUPYTER_PATH`. Looking at the [docs](https://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html#envvar-JUPYTER_PATH) it seems to be for Jupyter "data", such as "kernelspecs, nbextensions, or voila templates". – Carl G Mar 02 '20 at 04:36
3

Suppose your project has the following structure and you want to do imports in the notebook.ipynb:

/app
  /mypackage
    mymodule.py
  /notebooks
    notebook.ipynb

If you are running Jupyter inside a docker container without any virtualenv it might be useful to create Jupyter (ipython) config in your project folder:

/app
  /profile_default
    ipython_config.py

Content of ipython_config.py:

c.InteractiveShellApp.exec_lines = [
    'import sys; sys.path.append("/app")'
]

Open the notebook and check it out:

print(sys.path)

['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages/IPython/extensions', '/root/.ipython', '/app']

Now you can do imports in your notebook without any sys.path appending in the cells:

from mypackage.mymodule import myfunc
klapshin
  • 429
  • 3
  • 8
0

The verified solution doesn't work for me, since my notebook is not in my sys.path. This works however;

import os,sys
sys.path.insert(1, os.path.join(os.getcwd()  , '..'))
-1

You can use absolute imports:

/root
    /app
      /config
        newfile.py
      /source
        file.ipynb

# In the file.ipynb importing the newfile.py file
import os
os.chdir(ROOT_PATH)
from app.config import newfile

enter image description here