Search code examples
pythongoogle-app-enginemodulegae-module

import & structure python modules / classes


im working on some basic python stuff within the google app engine and I was unable to figure out the correct way to structure my handlers.

  • /main.py
  • /project/handlers/__init__.py
  • /project/handlers/AccountHandler.py

the AccountHandler is basically a class

class AccountHandler(webapp.RequestHandler):

when im using from project.handlers import AccountHandler python always give me a

TypeError: 'module' object is not callable

how do i have to name/import/structure my classes?

cheers, Martin


Solution

  • To quote from the docs:

    A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

    The AccountHandler you are importing is the module /project/handlers/AccountHandler.py in this case. The file AccountHandler.py is not callable, and the interpreter tells you this. To call the class you defined in your file just use:

    from project.handlers.AccountHandler import AccountHandler
    # Alternately
    # from project.handler import AccountHandler
    # AccountHandler.AccountHandler() # will also work.