0

If I have the following directory structure:

Folder1/
└─ Folder2/
──── a.py
──── b.py
└─── test/
────── c.py

a.py

import b

def say_hello():
    print("Hello World")

def main():
    say_hello()

if __name__ == '__main__':
    main()

b.py

def say_bye():
    print('bye!')

c.py

from hello import a

if __name__ == '__main__':
    a.say_hello()

I'm trying to run c.py But I get this error message:

    import b
ModuleNotFoundError: No module named 'b'

what did I do wrong here?

ADL
  • 114
  • 1
  • 3
  • 9
  • Does this answer your question? [python import module from parent package](https://stackoverflow.com/questions/14250058/python-import-module-from-parent-package) – depperm Apr 05 '22 at 13:39
  • or [this](https://stackoverflow.com/questions/14057464/relative-importing-modules-from-parent-folder-subfolder) or [this](https://stackoverflow.com/questions/40825474/python-import-module-from-a-parent-directory) – depperm Apr 05 '22 at 13:39

1 Answers1

0

if a and b are in the same directory add before the import:

sys.path.append(os.path.abspath(os.path.dirname(__file__)))

this will add the relative folder to path.

idan357
  • 342
  • 6
  • 17