How to do relative imports on app engine? (python)

2019-09-15 09:52发布

I'm trying to do a relative import in App Engine using python. Here is my basic situation:

app/
     models.py
     app.yaml
     /mymodule/
         test.py
         mymodule.yaml

I'm trying to import models.py.. basically I have the same datastore models that are being using across different modules, so I was hoping to be able to import models.py from within test.py (or any other module).

How do relative imports work with App Engine? Thanks.

Edit: My app.yaml file:

application: [my app name]
version: main
runtime: python27
api_version: 1
threadsafe: true

inbound_services:
- mail

builtins:
- appstats: on

handlers:

    [my handlers]

libraries:
- name: webapp2
  version: "2.5.1"

- name: jinja2
  version: latest

- name: markupsafe
  version: latest

- name: ssl
  version: latest

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-15 10:17

I believe the solution is to put the mymodule.yaml under app. It seems that the root of your code in the PYTHONPATH is the directory where your module's yaml file is.

查看更多
神经病院院长
3楼-- · 2019-09-15 10:24

Create one empty file under app called __init__.py to transform your directory into a package and then you will be able to import like:

from app import models

And you might want to add the same file into your mymodule directory as well.

查看更多
登录 后发表回答