简体   繁体   English

导入成功在同一个目录中的一个文件但不成功

[英]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.

相关问题 为什么 flask 无法提供 static 目录中的一个文件,但可以成功提供同一目录中的另一个文件 - Why could flask fail at serving one file from a static directory but succeeds at serving another one from the same directory 如何将一个文件导入到python中同一目录下的另一个文件中? - How to import a file into another file in the same directory in python? 为什么我不能将 .py 文件导入同一目录中的另一个文件? - Why cannot I import a .py file to another within the same directory? 无法从Python同一目录中的另一个文件导入类 - Can not import a class from another file in the same directory in Python 错误:如何将方法从一个文件导入同一目录中的另一个文件 - Error: How to import method from one file to other in a same directory 无法从另一个目录导入文件,该目录从同一目录导入文件(int Python) - Can't import file from another directory which imports a file from the same directory (int Python) PyCharm 导入文件同目录 - PyCharm import file in the same directory 从另一个目录导入文件 - Import a file from another directory 尝试将文件从一个文件夹导入到另一个目录中的另一个文件时出现 ModuleNotFoundError - ModuleNotFoundError when trying to import file from one folder into another file located in another directory 如何移动与同一目录中的另一个文件以相同 X 字符开头的文件,并移动最轻的一个 - How to move a file that starts with the same X characters as another file in the same Directory, and move the lightest one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM