Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sys.path different in Jupyter and Python - how to import own modules in Jupyter?

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?

like image 592
ulf Avatar asked Jan 24 '16 14:01

ulf


People also ask

How do I open a Jupyter Notebook on a different path?

Once you've entered your specific folder with Windows Explorer, you can simply press ALT + D, type in cmd and press Enter. You can then type jupyter notebook to launch Jupyter Notebook within that specific folder.


2 Answers

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 
like image 135
Dogan Askan Avatar answered Oct 13 '22 23:10

Dogan Askan


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/

like image 45
Mattia Fantoni Avatar answered Oct 13 '22 23:10

Mattia Fantoni