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?
-
Does this answer your question? python import module from parent packagedepperm– depperm2022年04月05日 13:39:02 +00:00Commented Apr 5, 2022 at 13:39
-
or this or thisdepperm– depperm2022年04月05日 13:39:43 +00:00Commented Apr 5, 2022 at 13:39
1 Answer 1
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.
Sign up to request clarification or add additional context in comments.
Comments
lang-py