如何正确地将 PyTZ 添加到 Google App Engine 应用程序?

发布于 2024-08-10 11:02:56 字数 1827 浏览 11 评论 0原文

这有点尴尬,但我一直无法找到关于这个主题的好资源。

我正在开发一个需要复杂时区转换的 Google App Engine 应用程序。由于我远未达到规定的配额,因此我选择使用 PyTZ。但是,我一定做错了什么。到目前为止我所做的是:

  1. 将 PyTZ 下载为 tarball
  2. 安装它并复制 pytz 目录到我的应用程序的根目录 (它是 webapp 的兄弟 目录,app.yaml 所在的位置)。

但是,如果我尝试实例化时区,PyTZ 似乎永远找不到任何时区。以下是来自 GAE 交互式控制台的示例会话:

from pytz import timezone

rome = timezone('Europe/Rome')

输出如下:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/admin/__init__.py", line 210, in post
    exec(compiled_code, globals())
  File "<string>", line 3, in <module>
  File "/Library/Python/2.5/site-packages/pytz-2009j-py2.5.egg/pytz/__init__.py", line 157, in timezone
UnknownTimeZoneError: 'Europe/Rome'

我做错了什么?预先感谢您的帮助。

注意:如果我只是在本地使用 python 交互式 shell,事情就会按预期工作:

>>> from pytz import datetime, timezone
>>> rome = timezone('Europe/Rome')
>>> rome.localize(datetime.datetime.now())
datetime.datetime(2009, 11, 12, 0, 4, 52, 990114, tzinfo=<DstTzInfo 'Europe/Rome' CET+1:00:00 STD>)

编辑:我需要澄清我没有使用 PyTZ 的压缩版本。我已将整个 zoneinfo 目录包含在我的项目中:

pc-morena:pytz lyudmilangelov$ cd zoneinfo/
pc-morena:zoneinfo lyudmilangelov$ ls -l
total 448
drwxr-xr-x@  55 lyudmilangelov  staff   1870 Nov 10 12:48 Africa
drwxr-xr-x@ 135 lyudmilangelov  staff   4590 Nov 10 12:48 America
drwxr-xr-x@  12 lyudmilangelov  staff    408 Nov 10 12:48 Antarctica
drwxr-xr-x@   3 lyudmilangelov  staff    102 Nov 10 12:48 Arctic
drwxr-xr-x@  93 lyudmilangelov  staff   3162 Nov 10 12:48 Asia
...

This is a little embarrassing, but I have not been able to find good resources on this topic.

I'm working on a Google App Engine application that requires sophisticated time zone conversions. Since I am nowhere near the imposed quotas, I have opted to go with PyTZ. However, I must be doing something wrong. What I've done so far is:

  1. Downloaded PyTZ as a tarball
  2. Installed it and copied the pytz
    directory into the root of my app
    (it is a sibling of the webapp
    directory, where app.yaml is located).

However, if I try to instantiate timezones, PyTZ can never seem to find any. Here is a sample session from GAE's interactive console:

from pytz import timezone

rome = timezone('Europe/Rome')

The output is the following:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/admin/__init__.py", line 210, in post
    exec(compiled_code, globals())
  File "<string>", line 3, in <module>
  File "/Library/Python/2.5/site-packages/pytz-2009j-py2.5.egg/pytz/__init__.py", line 157, in timezone
UnknownTimeZoneError: 'Europe/Rome'

What is it I am doing wrong? Thank you in advance for your help.

NOTE: If I just use the python interactive shell locally things work as expected:

>>> from pytz import datetime, timezone
>>> rome = timezone('Europe/Rome')
>>> rome.localize(datetime.datetime.now())
datetime.datetime(2009, 11, 12, 0, 4, 52, 990114, tzinfo=<DstTzInfo 'Europe/Rome' CET+1:00:00 STD>)

Edit: I need to clarify I'm not using a zipped version of PyTZ. I have included the whole zoneinfo directory in my project:

pc-morena:pytz lyudmilangelov$ cd zoneinfo/
pc-morena:zoneinfo lyudmilangelov$ ls -l
total 448
drwxr-xr-x@  55 lyudmilangelov  staff   1870 Nov 10 12:48 Africa
drwxr-xr-x@ 135 lyudmilangelov  staff   4590 Nov 10 12:48 America
drwxr-xr-x@  12 lyudmilangelov  staff    408 Nov 10 12:48 Antarctica
drwxr-xr-x@   3 lyudmilangelov  staff    102 Nov 10 12:48 Arctic
drwxr-xr-x@  93 lyudmilangelov  staff   3162 Nov 10 12:48 Asia
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

凝望流年 2024-08-17 11:02:57

我使用了此处找到的第 3 方说明:https ://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27

只需 pip install pytz 到 lib 目录中,按照说明添加 appengine_config.py 文件和内容。

虽然 pytz 现在是 Google App Engine 库的一部分:
https://cloud.google.com/ appengine/docs/standard/python/tools/built-in-libraries-27
我无法让它发挥作用。在 app.yaml 中,我添加了 - 名称:pytz 和版本:latest (也许版本应该是上面文档中列出的版本)。但这没有用。

I used the 3rd party instructions found here: https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27

Simply pip install pytz into the lib directory, add the appengine_config.py file and the contents as instructed.

While pytz is now part of Google App Engine's lib per:
https://cloud.google.com/appengine/docs/standard/python/tools/built-in-libraries-27
I couldn't get it to work. In the app.yaml I added - name: pytz and version: latest (perhaps the version should have been one listed in the above doc). But it didn't work.

疯了 2024-08-17 11:02:56

根据问题的标题,我想(但还没有这样的声誉)投票支持 jgeawax 的答案,因为这是我从搜索中发现这个问题时使用的答案。不过,我还要补充一点,按照那里提到的说明进行操作后,我在 github 上创建了一个最小项目,该项目说明了在应用程序引擎中使用 pytz 和压缩的 zoneinfo 文件。可能会节省 30 分钟的时间来在这里进行实验:http://github.com/mpstx/appengine_py_pytz_zipimport_zoneinfo< /a>

Based on the title of the question, I would like (but don't have the so reputation yet) to vote up jgeewax's answer since it's what I used when I found this question from a search. However, I'll also add that after following the instructions mentioned there, I created a minimal project on github which illustrates using pytz in app engine with the zipped zoneinfo files. Might save someone 30 minutes to get an experiment up and running here: http://github.com/mpstx/appengine_py_pytz_zipimport_zoneinfo

夜未央樱花落 2024-08-17 11:02:56

为了解决这个问题,我采用了与 gae-pytz 不同的策略,我创建了 pytz -appengine。特别是 pytz-appengine 将所有时区信息放入 ndb 数据存储区。

pytz 的单元测试通过(至少只要它们通过上游)。 pytz-appengine 的主要动机是让按需更新时区数据库变得简单。有一个构建脚本 build.py 可以自动执行此操作。

我欢迎任何反馈。

In response to this problem, went a different tact from gae-pytz and I created pytz-appengine. In particular pytz-appengine puts all the timezone information into the ndb datastore.

The unit tests for pytz pass (at least as long as they pass upstream). The main motivation with pytz-appengine was to make it trivial to update the timezone database on demand. There is a build script build.py that does this automatically.

I welcome any feedback on.

分分钟 2024-08-17 11:02:56

看起来您没有上传 pytz 的整个 zoneinfo 子树 - 在我手头的 pytz 版本中,22 个目录中有 570 个文件。不知道为什么——默认情况下,如果它们位于您的应用程序目录中,则应该上传它们。尝试 appcfg.py --verbose update (甚至 --noisy) 触摸一些后检查是否上传它们

许多小文件在应用程序引擎中很麻烦,但是解决这个问题需要对 init 中的 open_resource 功能进行一些调整.py 使其从 zip 文件内部获取“资源”,而不是尝试为每个“资源”打开一个实际的单独文件。此博客提供了更多详细信息如何去做(特别是评论中的观察很有用)。

Looks like you're not uploading the whole zoneinfo subtree of pytz -- 570 files in 22 directories, in the version of pytz I have at hand. Not sure why -- by default if they're in your app dir they should get uploaded. Try appcfg.py --verbose update (or even --noisy) after touching a few to check if it uploads them

Many little files are a bother in app engine, but fixing that requires a little tweak to function open_resource in init.py to make it get the "resource" from inside a zipfile instead of trying to open an actual separate file per "resource". This blog offers more details on how to go about it (esp. useful are the observations in the comments).

那一片橙海, 2024-08-17 11:02:56

我想通了,结果比我预想的还要尴尬。

问题是(正如我在问题中指定的那样)我已将 PyTZ 设为 webapp兄弟。但是,为了让 GAE 能够加载它,它必须是 webapp。这并不奇怪,但如果这就是问题所在,我预计 import pytz 会失败。

无论如何,将 PyTZ 移动到 webapp 下可以解决该问题,并且该模块仍然可以从 webapp 的同级(例如 test)访问。

I figured it out and it was more embarrassing than I anticipated.

The problem was that (as I specified in the question) I had made PyTZ a sibling of webapp. However, in order for GAE to be able to load it, it needs to be a child of webapp. This is not terribly surprising, but I expected import pytz to fail had that been the problem.

Regardless, moving PyTZ under webapp fixes the issue and the module is still accessible from siblings of webapp (e.g. test).

花心好男孩 2024-08-17 11:02:56

另一种方法是使用 gae-pytz 的打包版本,其中包括进一步优化:
http://pypi.python.org/pypi/gaepytz

你只需要 pytz 文件夹压缩档案,
在您的 gae 项目中使用它非常简单:

from pytz.gae import pytz

An alternative is to use the packaged version of gae-pytz that includes further optimizations:
http://pypi.python.org/pypi/gaepytz

You only need the pytz folder in the zipped archive,
Using it in your gae project is as easy as:

from pytz.gae import pytz
初熏 2024-08-17 11:02:56

pytz 现在是内置 GAE 库。你可以直接导入它。

pytz is now a built in GAE library. you can import it directly.

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