Reputation: 394
I have 2 folders containing python files like above. I've already marked them as Source Root on IntelliJ so that I can import modules normally between them
Today, I tried to import the module common_element
into common_utils
using:
import common_element
And the IDEA is just fine, now errors happen. I can browse between 2 modules normally. But when I executing it, it thrown "ModuleNotFoundError" like below:
I don't know the reason why, every file in common_ui
can import files fromcommon_utils
& run the project without errors, but NOT vice versa
What should I do now? Thank you!
Upvotes: 0
Views: 1050
Reputation: 728
You should add an empty __init__.py
file inside the common_ui
directory to make python treat this directory as a python module.
Upvotes: 1
Reputation: 8564
If want to do it without adding __init__.py
and creating packages, you can do this:
import os, sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
import common_ui.common_element
Upvotes: 0