2

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

Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
magegu
  • 530
  • 4
  • 18

2 Answers2

6

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.
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • `` AccountHandler is the name of the module in this case, AccountHandler.py is the name of the file.`` – Laurence Gonsalves Dec 09 '10 at 21:50
  • *Chuckles* @Laurence -- pedantic note noted. Is the new verbiage better? (And thanks!) – Sean Vieira Dec 09 '10 at 21:53
  • Thanks! Works! How can i import ALl classes/Handlers in the handlers module? – magegu Dec 09 '10 at 22:14
  • @magegu -- in this case `handlers` would be a `package`, rather than a module. You'll want to see http://docs.python.org/tutorial/modules.html#importing-from-a-package (The short of it is you'll need to add an `__all__ = [ an, array, of, modules, and, names, to, load]` in your `__init__.py` file in `handlers` and then you can do `from project.handlers import *` -- but it would be better to pull out only the ones you need by name. – Sean Vieira Dec 09 '10 at 22:18
0

You need to rename init.py to __init__.py

Velociraptors
  • 2,012
  • 16
  • 22