2

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) ?

2
  • Part is a module, not a class. Commented Dec 20, 2015 at 18:21
  • 1
    You probably want from part.Part import Part, but a minimal reproducible example would help. Commented Dec 20, 2015 at 18:22

1 Answer 1

20

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for adding the hints to standards also !
UpperCamelCase has another name: PascalCase

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.