【问题标题】:Can't understand how to use classes from other files无法理解如何使用其他文件中的类
【发布时间】:2019-06-08 13:19:43
【问题描述】:

我不明白。我在一个名为“example.py”的python文件中有一个类,在我桌面上同一文件夹中的另一个python文件中,我正在运行“从示例导入*”

我正在尝试使用该类,但它给了我一个错误,即示例模块不存在,或者我正在使用的类未定义

不确定所有这些依赖项是如何工作的......

我对此很陌生,所以请帮助我,这让我很受挫。

NameError:名称“TheClassName”未定义

【问题讨论】:

标签: python dependencies


【解决方案1】:

你的问题让我想起了我刚接触 Python 时的自己。

让我们从一些基本指南开始。

Python's reference guide on Imports and modules

How To Import Modules in Python 3

但长话短说,我认为添加一个点可以解决您的问题。

from .example import myclass

【讨论】:

  • 还是不行。给我这个错误 1 ​​import check ----> 2 from .module import TheClassName 3 ## find_end(item) 在 item 4 ## find_end: TheClassName -> Int 5 def find_end(item) 的 end 字段中产生值: ValueError:尝试在非包中进行相对导入
猜你喜欢
  • 2013-01-18
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多