0

I'm converting a python App Engine Application to use modules as described in this article, https://cloud.google.com/appengine/docs/python/modules/. I would like to use a custom handler as a base class in each of my modules to addd some common functionality. Do I need to repeat my custom handler code in each module, or is there a way to import that class?

For example, I'd like my architecture to look something like this:

MyProject
├── common
│   ├── my_handler.py
├── module1
│   │   ├── module1.yaml
│   │   ├── main.py
├── module2
│   │   ├── module2.yaml
│   │   ├── main.py

/common/my_handler.py is not part of an app engine module and looks like this:

import webapp2
class BaseHandler(webapp2.RequestHandler):
    """
        BaseHandler for all requests
    """
    pass  

And then in the /module1/main.py file, I'd like to do have something like this:

import webapp2

from common.my_handler import BaseHandler

class module1Handler(BaseHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Hello, this is module 1!')

app = webapp2.WSGIApplication(
    [('/', module1Handler),
    ],
    debug=True)

This won't work, the server throws an error as it cannot find common.my_handler.py:

ImportError: No module named common.my_handler 

The modules seem to be sandboxed. Is it possible to import /common/my_handler.py from within /module1/main.py?

Harshal Patil
  • 6,284
  • 8
  • 37
  • 55
WmbgPhil
  • 1
  • 1

1 Answers1

0

There are a few things you could try here such as:

from ..common import BaseHandler

or adding the path to the global path:

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import common.my_handler as BaseHandler
Community
  • 1
  • 1
Ryan
  • 2,434
  • 1
  • 10
  • 19