Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python module' object is not callable

Tags:

python

This is a python rooky question... The file structure is like that

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

when running python3 testCreation.py I get a

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

no complain about import. so I wonder what the issue is !?

also coming from Java, can some one comment if organising classes for python is better just in packages with subpaths or in modules (ommit the init.py file) ?

like image 720
user3732793 Avatar asked May 27 '26 18:05

user3732793


1 Answers

In Python, you need to distinguish between module names and class names. In your case, you have a module named Part and (presumable) a class named Part within that module. You can now use this class in another module by importing it in two possible ways:

  1. Importing the entire module:

     import Part
    
     part = Part.Part()  # <- The first Part is the module "Part", the second the class
    
  2. Import just the class from that module into your local (module) scope:

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

Note that by convention, in Python modules are usually named in lowercase (for instance part), and only classes are named in UpperCamelCase. This is also defined in PEP8, the standardized coding style guide for Python.

like image 135
helmbert Avatar answered May 30 '26 06:05

helmbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!