0

How can I import a function in a script, where the function is defined in the parent's child folder?

In the following folder structure, I would like to use

root_folder/
    utils_folder:
        __init__.py
        helper_functions.py (where Function_A is defined)
    module_A_folder:
        Script_A.py (Function_A will be imported and used here)
       

Script_A.py needs to use Function_A.

The __init__.py of utils_folder is defined:

from .helper_functions import Function_A

When I tried to import Function_A in Script_A.py like this:

from ..utils import Function_A

I received the following error:

ImportError: attempted relative import with no known parent package

How can I make this work? I am with Python 3.9 x64.

alextc
  • 3,206
  • 10
  • 63
  • 107

1 Answers1

0

Try:

from utils_folder.helper_functions import Function_A
Tom Smith
  • 1
  • 1
  • Thanks. I tried it but this threw an error of `ModuleNotFoundError: No module named 'utils_folder'`. – alextc Nov 17 '22 at 00:24