[英]Python import error: no module named
I am very new at Python. 我是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. 我有一个具有脚本现有的示例项目
YYY
路径XXX/YYY
,和脚本A.py
调用这些一个接一个。
I only want to add a script ZZZ.py
to the YYY
scripts so that call after them. 我只想将脚本
ZZZ.py
添加到YYY
脚本中,以便在它们之后调用。 I add this script in the same path ( XXX/ZZZ.py
) and try to import it in the A.py
and call it. 我将此脚本添加到同一路径(
XXX/ZZZ.py
)中,然后尝试将其导入A.py
并调用它。
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
? 为什么Python可以导入
XXX/YYY
py文件,但是为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 并且如果包含
XXX
的目录在您的PYTHONPATH中,则
import XXX.ZZZ as Z
should work. 应该管用。
If you want to import ZZZ, do import XXX.YYY.ZZZ as Z
. 如果要导入ZZZ,
XXX.YYY.ZZZ as Z
导入XXX.YYY.ZZZ as Z
This assumes that YYY is a directory, and also assumes you actually put that ZZZ.py inside of YYY. 这假设YYY是一个目录,并且还假设您实际上将ZZZ.py放在YYY内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.