Python模块'对象不可调用

use*_*793 0 python

这是一个python的新问题......文件结构就是这样

./part/__init__.py
./part/Part.py
./__init__.py
./testCreation.py
Run Code Online (Sandbox Code Playgroud)

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

part = Part() TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)

没有抱怨进口.所以我想知道问题是什么!

也来自Java,如果组织python的类在包含子路径或模块的程序包中更好(可省略init .py文件),可以发表一些评论吗?

hel*_*ert 10

在Python中,您需要区分模块名称类名.在您的情况下,您有一个名为Part和(可能)的Part模块,该模块在该模块中命名.您现在可以通过两种可能的方式导入此类,从而在另一个模块中使用它:

  1. 导入整个模块:

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

    from Part import Part
    part = Part()  # <- Here, "Part" refers to the class "Part"
    
    Run Code Online (Sandbox Code Playgroud)

请注意,按照惯例,Python模块通常以小写(例如part)命名,并且只有类在UpperCamelCase中命名.这也在PEP8中定义,PEP8是Python的标准化编码样式指南.