Why am I getting module object is not callable in python 3?

Why am I getting module object is not callable in python 3?

You have a module named app that contains a class named app. If you just do import app in main.py then app will refer to the module, and app.app will refer to the class. Here are a couple of options:

  • Leave your import statement alone, and use myApp = app.app({testFKey:[3,2,2]}) inside of main.py
  • Replace import app with from app import app, now app will refer to the class and myApp = app({testFKey:[3,2,2]}) will work fine

In main.py change second line to:

from app import app

The issue is you have app module and app class within it. But you are importing module, not the class from it:

myApp = app({testFKey: [3, 2, 2]})

(you can also instead replace app inside line above into app.app)

Why am I getting module object is not callable in python 3?

The problem, as both F.J and Tadeck already explained, is that app is the module app, and app.app is the class app defined in that module.

You can get around that by using from app import app (or, if you must, even from app import *), as in Tadecks answer, or by explicitly referring to app.app instead of just app, as in F.Js answer.

If you rename the class to App, that wont magically fix anything—you will still have to either from app import App or refer to app.App—but it will make the problem a whole lot more obvious. And make your code less confusing after youve fixed the problem, too.

This is part of the reason that PEP 8 recommends that:

Modules should have short, all-lowercase names.

Almost without exception, class names use the CapWords convention.

That way, theres no way to mix them up.

Leave a Reply

Your email address will not be published.