I have a Python Google App Engine project structured as follows:
app/
handlers/
register_user.py
models/
user.py
The user.py
file contains a class User(ndb.Model)
.
I'm trying to access the User
class from register_user.py
to create and put a new user in the database. Normally, I'd just import it like this:
from ..models.user import User
But this errors out because I'm trying to import something from above my root package - so I'm guessing models is my root package, and I can't get back to the app
package?
Right now, I'm able to work around it by importing like this:
import importlib
User = importlib.import_module('models.user').User
I think this is kind of messy, though. So what's the "right" way of importing my User class?
Edit: The full stack trace:
Attempted relative import beyond toplevel package (/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py:1552)
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~polly-chat/1.394430414829237783/main.py", line 48, in post
receive_message(messaging_event)
File "/base/data/home/apps/s~polly-chat/1.394430414829237783/messaging/handler.py", line 39, in receive_message
intent_picker.respond_to_postback(messaging_event)
File "/base/data/home/apps/s~polly-chat/1.394430414829237783/messaging/intent_picker.py", line 71, in respond_to_postback
intent = importlib.import_module('intents.register_user')
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/base/data/home/apps/s~polly-chat/1.394430414829237783/intents/register_user.py", line 1, in <module>
from ..models import messenger_user
ValueError: Attempted relative import beyond toplevel package
(The package names here are different; I simplified them above to make the example more general)