Python模块的对象不是callab

2024-03-28 11:56:56 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个python新手问题。。。 文件结构是这样的

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

运行python3 testCreation.py时,我得到一个

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

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

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


Tags: 文件pyobjectinitisnot结构python3
1条回答
网友
1楼 · 发布于 2024-03-28 11:56:56

在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),只有类以大写形式命名。这也在PEP8(Python的标准化编码样式指南)中定义。

相关问题 更多 >