I have the following repository structure:
JavaScript
x
5
1
directoryA
2
- moduleA.py (it contains MyClass class)
3
directoryB
4
- moduleB.py
5
In moduleB.py I need to import MyClass, I use the following command:
JavaScript
1
2
1
from directoryA.moduleA import MyClass
2
When I run the main() function in moduleB.py I have the following error:
ModuleNotFoundError: No module named ‘directoryA’
I run the moduleB.py from directoryB in the following way:
JavaScript
1
2
1
py moduleB.py
2
How could I fix this problem?
Advertisement
Answer
The best you can do to organizing your scripts would be using a file called main.py
in the main folder, which must be in the same directory as directoryA
and directoryB
.
In main.py
, just import the files from child directories:
JavaScript
1
5
1
from directoryA.moduleA import MyClass
2
import directoryB.moduleB
3
4
# Do whatever you want, but sometimes, just don't go too overboard.
5