0

I have a file structure like this:

My_Package/
    __init__.py
    helper_fun.py
    sub_dir_1/
        __init__.py
        codes.py

I want to import functions from helper_fun.py while inside codes.py. I tried

from helper_fun import foo
from .helper_fun import foo
from ..helper_fun import foo
from My_package.helper_fun import foo

but none works. How should I use absolute import to always specify import directories from the top-level My_Package?

FSeed
  • 31
  • 4
  • 1
    Possible duplicate of [Absolute vs. explicit relative import of Python module](https://stackoverflow.com/questions/4209641/absolute-vs-explicit-relative-import-of-python-module) – lbragile Nov 29 '19 at 04:27

1 Answers1

1

Not quite an answer but you can use :

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

 import helper_fun

From : Importing modules from parent folder

Diewyns
  • 11
  • 2
  • Thanks for the answer. But I think that is still using relative import. I still want to know how to properly use absolute import. – FSeed Dec 01 '19 at 16:51