views:

136

answers:

2

My code worked fine when it was all in one file. Now, I'm splitting up classes into different modules. The modules have been given the same name as the classes. Perhaps this is a problem, because MainPage is failing when it is loaded. Does it think that I'm trying to inherit from a module? Can module/class namespace collisions happen?

MainPage.py

import BaseHandler
from models import Item
from Utils import render

class MainPage(BaseHandler):
    def body(self, CSIN=None): #@UnusedVariable
        self.header('Store')
        items = Item.all().order('name').fetch(10)
        render('Views/table.html', self, {'items': items})
        self.footer()

BaseHandler.py

from google.appengine.ext import webapp
from google.appengine.api import users
from Utils import *

# Controller
class BaseHandler(webapp.RequestHandler):
     # ... continues ... 

Failure Traceback:

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3180, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3123, in _Dispatch
    base_env_dict=env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 515, in Dispatch
    base_env_dict=base_env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2382, in Dispatch
    self._module_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2292, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2188, in ExecuteOrImportScript
    exec module_code in script_module.__dict__
  File "C:\Users\odp\workspace\Store\src\Main.py", line 5, in <module>
    import MainPage
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1917, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1819, in FindAndLoadModule
    description)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1770, in LoadModuleRestricted
    description)
  File "C:\Users\odp\workspace\Store\src\MainPage.py", line 10, in <module>
    class MainPage(BaseHandler):
TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

UPDATE I appear to have solved it. This import works much better:

from BaseHandler import BaseHandler

Is it bad style to have the module and class name be the same?

+2  A: 

Yes, module names share the same namespace as everything else, and, yes, Python thinks you are trying to inherit from a module.

Change:

class MainPage(BaseHandler):

to:

class MainPage(BaseHandler.BaseHandler):

and you should be good to go. That way, you're saying "please inherit from the BaseHandler class in the BaseHandler module".

Alternately, you could change:

import BaseHandler

to:

from BaseHandler import BaseHandler
Daniel Stutzbach
+3  A: 

First of all the filenames should be all lowercase. That's Python convention that helps to avoid confusion such as this, at least most of the time.

Next, your import from withing MainHandler.py is wrong. You are importing BaseHandler (the module) and referencing it as if it were a class. The class is actually BaseHandler.BaseHandler. You need to reference it as such.

Try that and it should work for you.

jathanism