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?

asked Apr 5, 2022 at 13:35
2

1 Answer 1

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.

answered Apr 5, 2022 at 13:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.