定义GAE项目的通用文件 [英] Define common files for GAE projects
问题描述
我在Google App Engine帐户中有两个不同的项目.我计算机中的文件夹结构如下:
I have two different projects in Google app engine account. The folders structure in my computer is as following:
- parent_folder
- common_folder
- project1_folder
- project2_folder
在common_folder中我想在两个项目(project1和project2)中使用的python类很少.
I have few python classes in common_folder that I want to use in both projects (project1 and project2).
我想将通用类导入到我的项目中,因此Google App Engine也会在Production env上识别它们(也许GAE可以在上传时创建通用文件的副本?)
I want to import the common classes into my projects, so Google App Engine will recognize them also on Production env (maybe GAE can create a copy of the common files when uploading?)
我尝试使用以下代码从特定的父目录路径导入我的课程:
I tried using the following code in order to import my class from a specific parent directory path:
import os PARENT_DIR = os.path.dirname(os.path.dirname(__file__)) print PARENT_DIR def load_src(name, fpath): import os, imp return imp.load_source(name, os.path.join(os.path.dirname(__file__), fpath)) load_src("globals", PARENT_DIR +"/common/globals.py") from globals import *
此代码仅在我的本地主机环境中有效,但是当我将其上传到GAE时,它无法识别该类:
This code worked only from my localhost environment but when I uploaded it to GAE, it didn't recognize the class:
> File > "/base/data/home/apps/s~loan-management-system/1.394267205500555512/main.py", > line 39, in load_src > return imp.load_source(name, os.path.join(os.path.dirname(__file__), fpath)) IOError: [Errno 2] No > such file or directory
推荐答案
出现此错误的原因是,在部署期间,模块/服务目录(或单个模块应用程序的应用程序目录)上方没有任何内容上载到GAE.
The reason for the error you see is that nothing above the module/service dir (or app dir for single module apps) is uploaded to GAE during deployment.
您可以在应用程序目录中符号链接必要的文件,请参见 https://stackoverflow.com/a/34291789/4495081 .部署实用程序知道遵循符号链接,并在部署中包括公用文件夹包.
You can symlink the necessary file(s) inside your app dirs, see https://stackoverflow.com/a/34291789/4495081. The deployment utility knows to follow the symlink and include the common folder package in the deployment.
我建议将公用文件夹打包(添加
__ init __.py
文件),然后再I would suggest making the common folder a package (add a
__init__.py
file) then vendor it in inside each application but instead of copying/installing the code just symlink the common code dir inside thelib
dir.然后,您无需手动摆弄路径,在您的应用中,您只需要使用类似的类(例如,假设
MyClass
来自common_folder/class.py
):Then you don't need to fiddle with the path manually, in your app you'd just use the classes like this (assuming for example that
MyClass
comes fromcommon_folder/class.py
):from common_folder.class import MyClass
这篇关于定义GAE项目的通用文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!