[英]import succeeds for one file but not another in the same directory
I have a project folder named proja. 我有一个名为proja的项目文件夹。 It contains several files:
它包含几个文件:
"__init__.py"
classa.py
classb.py
classc.py
I have a subfolder in proja named folderb. 我在proja中有一个名为folderb的子文件夹。 It has an "" init .py"", also.
它也有一个“” init .py“”。 This folder contains a py script with the following import statements.
此文件夹包含带有以下导入语句的py脚本。 It imports classa but fails on classb.
它导入classa,但在classb上失败。
from classa import classa
from classb import classb
from class import classc
It doesn't matter if I run the script from the proja: "python folderb/imptest.py" or of I cd to folderb and run: "python imptest.py". 不管是从proja运行脚本:“ python folderb / imptest.py”,还是从cd到cd并运行:“ python imptest.py”。
It always fails with: "ImportError: No module named classb..." 它总是失败并显示:“ ImportError:没有名为classb ...的模块”
Why will it import from one file but not another in the same location? 为什么要从一个文件而不是同一位置的另一个文件导入?
Make sure there is a function or method of name classb
in the file classb.py
确保文件
classb.py
有一个名为classb
的函数或方法
Once run the file with this : 使用以下命令运行文件:
import classa
import classb
import classc
If this commands work then file paths are ok and you should look inside the files. 如果此命令有效,则文件路径正常,您应该查看文件内部。
If you do this and it works check the inside of file classb.py
, 如果执行此操作并且可以正常工作,请检查文件
classb.py
的内部,
otherwise post the error. 否则发布错误。 It will help.
我会帮你的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.