在 Google App Engine 中导入我自己的模块
我是 Google App Engine 的新手,刚刚开始使用它,但我一生都不明白如何导入非标准模块,因此也不完全理解 app.yaml文件的目的。
本质上,我想从我的脚本文件导入 SimPy(约 15 个 python 文件的集合),但我所做的每一个组合都会导致 ImportError,即:
from SimPy.Simulation import *
ImportError: No module named SimPy.Simulation
目前它们都在同一个文件夹中,而我的 app.yaml文件内容如下:
application: physicalsim
version: 1
runtime: python
api_version: 1
handlers:
- url: /
script: physicalsim.py
- url: /
script: Globals.py
- url: /
script: Simulation.py
etc....
现在我很确定我的 yaml 文件的语法错误,但无法真正找到任何有用的文档来说明如何在任何地方执行此操作(有点令人沮丧),我真的不明白我见过的一些 app.yaml 文件中定义的文件夹没有物理结构,它们只是虚拟文件夹吗?
如果你能帮忙的话,干杯,我确信我被认为是个傻瓜:)
I'm brand new to Google App Engine and have just been playing around with it, but for the life of me I don't understand how to import non-standard modules, and for that matter don't fully understand the app.yaml file's purposes.
Essentially I want to import SimPy (a collection of ~15 python files) from my script file, but every combination of things I do results in an ImportError, i.e:
from SimPy.Simulation import *
ImportError: No module named SimPy.Simulation
At present they're all in the same folder, and my app.yaml file reads:
application: physicalsim
version: 1
runtime: python
api_version: 1
handlers:
- url: /
script: physicalsim.py
- url: /
script: Globals.py
- url: /
script: Simulation.py
etc....
Now I'm quite certain I've got the syntax of the yaml file wrong, but can't really find any useful documentation for how to do it anywhere (bit frustrating), I don't really understand the meaning of the folders defined in some of the app.yaml files I've seen bearing in mind there is no physical structure, are they just virtual folders?
Cheers if you can help and I'm sure I've come across as a dunce :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 appconfig(跳过文件)文档:
(不要将
SimPy
放入跳过列表中)。无需在
app.yaml
中指定包含的程序文件。确保您的应用程序目录包含名为
SimPy
的子目录带有
__init__.py
文件,当然还有Simulation.py
。From the appconfig (skipping files) doc:
(Do not put
SimPy
in a skip list).There is no need to specify the included program files in
app.yaml
.Make sure your application directory includes a sub-directory named
SimPy
with an
__init__.py
file and, of course,Simulation.py
.app.yaml 文件的文档位于应用程序配置页面< /a>.
处理程序部分本质上是通过将请求的 url 与依次指定的每个模式进行比较来工作的,当找到第一个匹配时运行匹配的处理程序(或者如果
static_dir
或static_files< 则提供静态文件) 在您显示的 app.yaml 中指定 /code> 而不是
script
,与
/
完全匹配的 url 将导致处理程序physicalsim.py 被调用,因为它们是从同一个 url 提供的,如果这些是physicalsim.py 的支持模块,则不需要在 app.yaml 中包含有关它们的任何内容
。不必对你的 web 应用程序的内部结构有任何影响 url 路径和包含你的处理程序和模块的目录之间不需要有任何对应关系(尽管为了理智起见,你可能想要维护)至少有一些相关性)。
您是否已浏览过入门文档?
app.yaml
所在的目录(即将上传的应用程序的根目录)将位于 python 路径上,您应该能够引用与它相关的所有模块(和/或根据需要在您的处理程序中添加其他内容)。The documentation for the app.yaml file is here in the Application Config page.
The handlers section essentially works by comparing a request's url with each of the patterns specified in turn and when the first match is found running the matching handler (or serving up static files if
static_dir
orstatic_files
is specified instead ofscript
.In the app.yaml you've shown, the url exactly matching
/
will cause the handlerphysicalsim.py
to be called. All the other handlers will be ignored since they're served from the same url. If those are support modules for physicalsim.py you don't need to include anything about them in app.yaml.The urls don't have to have any bearing on what the structure of your webapp looks like internally. There doesn't need to be any correspondence between the url paths and the directories containing your handlers & modules (although for sanity's sake you might want to maintain at least some correlation).
Have you been through the getting started docs?
The directory in which
app.yaml
resides (i.e. the root of your Application as it will be uploaded) will be on the python path, you should be able to refer to all your modules with respect to it (and/or add others in your handler if needed).添加一个空的 init.py 到您想要从中导入模块的任何子文件夹..我不知道这一点...
我在这篇SO帖子中找到了答案。
从子文件夹导入模块
Add an empty init.py to any sub folder you wish to import modules from.. I was not aware of this...
I found the answer in this SO post.
Import module from subfolder