Let’s say I have the following structure:
JavaScript
x
5
1
dir_1
2
├── functions.py
3
└── dir_2
4
└── code.ipynb
5
In, code.ipynb
, I simply want to access a function inside functions.py
and tried this:
JavaScript
1
2
1
from ..functions import some_function
2
I get the error:
JavaScript
1
2
1
attempted relative import with no known parent package
2
I have checked a bunch of similar posts but not yet figured this out… I am running jupyter notebook from a conda env
and my python version is 3.7.6
.
Advertisement
Answer
In your notebook do:
JavaScript
1
6
1
import os, sys
2
dir2 = os.path.abspath('')
3
dir1 = os.path.dirname(dir2)
4
if not dir1 in sys.path: sys.path.append(dir1)
5
from functions import some_function
6