How do I set the PYTHON_PATH variable for my Google App Engine app?
I know you can set this in the Preferences of the SDK, but I worry that it won't be correct when the app is deployed. What is the PYTHON_PATH开发者_运维百科 value out on Google's Cloud? Can I set this?
Going backwards a step, I just want to be able to do:
from something import something_else
My app can't resolve this reference, which is what led me to trying to get the PYTHON_PATH.
Once your code is deployed, it will have access to all of the supported libraries without you taking any action. In order to add your application-specific directories to the path, you will need to do it programatically inside your handler. I use a pattern like this:
paths = [
os.path.join(os.path.dirname(__file__), 'mylib'),
os.path.join(os.path.dirname(__file__), 'app', 'tags'),
os.path.join(os.path.dirname(__file__), 'app', 'controllers'),
os.path.join(os.path.dirname(__file__), 'app', 'common'),
os.path.join(os.path.dirname(__file__), 'app', 'models'),
os.path.join(os.path.dirname(__file__), 'app')
]
for path in paths:
if os.path.exists(path):
# Don't add paths that don't exist.
sys.path.append(path)
If you find that you are having a problem importing a module that is not part of your application, it may be that the module is not supported on AppEngine. See the docs for more information.
EDIT: Everyone structures their AppEngine applications a little differrently, but generally, you'll need to have this code at the main entry point for each .py file that is listed as a handler in your app.yaml. Generally, a handler file will have something that looks like this:
def main():
# Do all of my initialization and run my
# WSGIApplication
if __name__ == "__main__":
main()
You can out the path-setting code inside the main() function.
精彩评论