0

My folder structure is as follows:

parent_folder
├── folder
│   └── file.ipynb
└── LEID.py

I would like to import a function named LEID in LEID.py to file.ipynb

What I have tried:

from LEID import LEID   

doesn't work

import sys
sys.path.append('path_to_parent_folder')
from parent_folder.LEID import LEID   

doesn't work

import sys
sys.path.insert(1, 'path_to_LEID.py')
from LEID import LEID

doesn't work

Andrew
  • 54
  • 1
  • 4

2 Answers2

0

Similar to Andrew Ryan's comment, you could try something like below:

import os
import sys
import inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir) 

from LEID import LEID

That should help to get the pathing right between file references

jrynes
  • 187
  • 1
  • 9
  • Thanks for your help! I tried your code, I can see correct syntax highlighting in "from LEID import LEID" but still returns "ModuleNotFoundError: No module named 'LEID'". Anyway I gave up this method, now I just import module using the full path as shown [here](https://stackoverflow.com/questions/67631/how-do-i-import-a-module-given-the-full-path) – Andrew Oct 19 '22 at 06:32
0

Turns out the second method would have worked if I included '/' at the end of the directory.

import sys 

sys.path.append('C:/folder_containing_my_module/') # works

sys.path.append('C:/folder_containing_my_module') # doesn't work
Andrew
  • 54
  • 1
  • 4