LuCima LuCima - 3 years ago 181
Python Question

Python import error, cannot find module

My project structure is like this:

dir A/

, I've written
from modules.a import xxx
. In
, I want to import module
and in
, I want to import

Here's the problem, I wrote
from components.b import xxx
, I got import error when I ran
. I got the same error if I write
from c import xxx
. But if I ran
directly, it went well. I've tried many times and found that I have to write all import sentences considering
dir A
as the root directory. For example, if I write
from modules.components.b import xxx
from modules.components.c import xxx
, the program goes well.

Someone says that change environment path can solve this problem. But don't I have to set the path again and again once I change the position of the whole project?

Is there any decent solution? I'm very glad to hear someone's help.

Answer Source

Relative imports.

from .components.b import xxx
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download