【问题标题】:Python module' object is not callablePython 模块的对象不可调用
【发布时间】:2015-12-20 18:16:34
【问题描述】:

这是一个python菜鸟问题... 文件结构是这样的

./part/__init__.py
./part/Part.py
./__init__.py
./testCreation.py

当运行 python3 testCreation.py 我得到一个

part = Part() TypeError: 'module' object is not callable

不要抱怨进口。所以我想知道问题是什么!?

同样来自 Java,如果只在带有子路径的包或模块中(省略 init.py 文件)为 python 组织类更好,有人可以评论一下吗?

【问题讨论】:

  • Part 是一个模块,而不是一个类。
  • 您可能需要from part.Part import Part,但minimal reproducible example 会有所帮助。

标签: python


【解决方案1】:

在Python中,你需要区分模块名类名。在您的情况下,您有一个名为 Part 的模块和(推测)该模块中名为 Part 的类。您现在可以通过两种可能的方式在另一个模块中使用这个类:

  1. 导入整个模块:

     import Part
    
     part = Part.Part()  # <- The first Part is the module "Part", the second the class
    
  2. 只将该模块中的类导入本地(模块)范围:

     from Part import Part
     part = Part()  # <- Here, "Part" refers to the class "Part"
    

请注意,按照惯例,Python 中的模块通常以小写命名(例如part),并且只有类以大写字母命名。这也在 Python 的标准化编码风格指南 PEP8 中定义。

【讨论】:

  • 感谢您也向标准添加提示!
  • UpperCamelCase 还有另一个名字:PascalCase
猜你喜欢
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 2017-01-12
  • 2021-06-12
  • 1970-01-01
相关资源
最近更新 更多