Here's my situation. I have some jupyter notebooks inside some folder and I would like to share some code between those notebooks trough a library I made.
The folder structure is the following:
1.FirstFolder/
notebookA.ipynb
2.SecondFolder/
notebookB.ipynb
mylib/
__init__.py
otherfiles.py
I tried putting the following code at the beginning of the notebook:
# to use modules in parent folder
import sys
import os
from pathlib import Path
libpath = os.path.join(Path.cwd().parent,'mylib')
print(f"custom library functions are in the module:\n\t{libpath}")
sys.path.append(libpath)
import mylib
The print outputs the correct path of the module and then a ModuleNotFoundError comes up and the program crashes:
---> 10 import mylib
11 from mylib import *
ModuleNotFoundError: No module named 'mylib'
Looking up on SO I found that that should have been the way to import a module from a non-default folder. Where is the error?
EDIT: after FinleyGibson's answer I tried sys.path.append(Path.cwd().parent) and restarted the kernel but I still have the same problem.
EDIT2: I tried this and it worked, but I still would like to know why the previous approaches haven't worked.
import sys
import os
from pathlib import Path
tmp = Path.cwd()
os.chdir(Path.cwd().parent)
sys.path.append(Path.cwd())
import mylib
from mylib.dataloading import *
os.chdir(tmp)