Import of another python function does not work

38 Views Asked by At

I have the following directory structure:

  • GNS
    • gns_export.py
  • new_structure
    • DB_Manager.py

and other Files that are not important. When I now try to import a function from DB_manager.py in gns_export.py, I get an error.

In gns_export.py: from new_structure.DB_Manager import *

Exception: ModuleNotFoundError: No module named 'new_structure'

2

There are 2 best solutions below

1
milchmannverleih On
0
Zero On

If you're just importing the module and not running the file directly then @milchmannverleih's answer would work.

If for some reason you're trying to run it directly, then you can do it in the following way.

import os
import sys

sys.path.append(os.pardir)
from new_structure.DB_Manager import *

You can add your parent directory in the sys.path of the program and it'd be able to find new_structure folder there.