Linh Nhat Nguyen
Linh Nhat Nguyen

Reputation: 394

ModuleNotFoundError when executing but IntelliJ shows no errors

enter image description here

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: enter image description here

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

Answers (2)

Walid
Walid

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

Jarvis
Jarvis

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

Related Questions