开发者

Python import error: no module named

I am very new at Python. I have an existing example project that has the scripts YYY in path XXX/YYY, and a script A.py that call these one by one.

I only want to add a script ZZZ.py to the YYY scripts so that call after them. I add this script in the same path (XXX/ZZZ.py) and try to import it in the 开发者_开发知识库A.py and call it.

But I got this error:

python import error no module named XXX/ZZZ.py

I wonder: What is the difference? Why can Python import XXX/YYY py files but return this error for ZZZ.py?


If your modules are structured like this:

/XXX

    __init__.py
    ZZZ.py
    /YYY

        __init__.py

        *more scripts here*

And if the directory containing XXX is in your PYTHONPATH, then

import XXX.ZZZ as Z

should work.


If you want to import ZZZ, do import XXX.YYY.ZZZ as Z. This assumes that YYY is a directory, and also assumes you actually put that ZZZ.py inside of YYY.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜