在 Google App Engine 中导入我自己的模块

发布于 2024-09-13 16:49:06 字数 668 浏览 10 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美羊羊 2024-09-20 16:49:06

来自 appconfig(跳过文件)文档:

应用程序目录中路径与 static_dir 路径或 static_files 上传路径匹配的文件被视为静态文件。 应用程序目录中的所有其他文件都被视为应用程序和数据文件

skip_files 元素指定应用程序目录中的哪些文件不上传到 App Engine。该值可以是正则表达式,也可以是正则表达式列表。上传应用程序时,与任何正则表达式匹配的任何文件名都会从要上传的文件列表中省略。

(不要将 SimPy 放入跳过列表中)。

无需在app.yaml中指定包含的程序文件。
确保您的应用程序目录包含名为 SimPy 的子目录
带有 __init__.py 文件,当然还有 Simulation.py

From the appconfig (skipping files) doc:

Files in your application directory whose paths match a static_dir path or a static_files upload path are considered to be static files. All other files in the application directory are considered to be application program and data files.

The skip_files element specifies which files in the application directory are not to be uploaded to App Engine. The value is either a regular expression, or a list of regular expressions. Any filename that matches any of the regular expression is omitted from the list of files to upload when the application is uploaded.

(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.

回忆追雨的时光 2024-09-20 16:49:06

app.yaml 文件的文档位于应用程序配置页面< /a>.

处理程序部分本质上是通过将请求的 url 与依次指定的每个模式进行比较来工作的,当找到第一个匹配时运行匹配的处理程序(或者如果 static_dirstatic_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 or static_files is specified instead of script.

In the app.yaml you've shown, the url exactly matching / will cause the handler physicalsim.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).

掐死时间 2024-09-20 16:49:06

添加一个空的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文