如何使用 Google App Engine 管理第三方 Python 库?(virtualenv?pip?)

如何使用 Google App Engine 管理第三方 Python 库?(virtualenv?pip?)

问题描述:

使用 Google App Engine 管理第三方 Python 库的最佳策略是什么?

What's the best strategy for managing third-party Python libraries with Google App Engine?

假设我想使用 Flask,一个 Web 应用程序框架.一篇博客文章说这样做,这似乎不对:

Say I want to use Flask, a webapp framework. A blog entry says to do this, which doesn't seem right:

$ cd /tmp/
$ wget http://pypi.python.org/packages/source/F/Flask/Flask-0.6.1.tar.gz
$ tar zxf Flask-0.6.1.tar.gz
$ cp -r Flask-0.6.1/flask ~/path/to/project/
(... repeat for other packages ...)

必须有更好的方法来管理第三方代码,尤其是如果我想跟踪版本、测试升级或两个库共享一个子目录时.我知道 Python 可以从 zipfiles 导入模块,并且 pip 可以处理一个很棒的 REQUIREMENTS 文件,而且我已经看到 pip 有一个 zip 用于 GAE 的命令.

There must be a better way to manage third-party code, especially if I want to track versions, test upgrades or if two libraries share a subdirectory. I know that Python can import modules from zipfiles and that pip can work with a wonderful REQUIREMENTS file, and I've seen that pip has a zip command for use with GAE.

(注意:有一些类似的问题——1234, 5 — 但它们是针对特定案例的,并不能真正回答我的问题.)

(Note: There's a handful of similar questions — 1, 2, 3, 4, 5 — but they're case-specific and don't really answer my question.)

(2021 年 6 月) 这篇博文已有十多年历史,因此现在需要更新答案.

(Jun 2021) This post is over a decade old, and so an updated answer is warranted now.

  1. Python 3:在 requirements.txt 中列出 3P 库以及任何所需的版本号;它们会在部署时由 Google 自动安装.(如果您决定将应用迁移到 Google Cloud Functions云运行.)
  2. Python 2 没有 内置 3P 库(常规 3P 库):
  1. Python 3: list 3P libraries in requirements.txt along with any desired version#s; they'll be automatically installed by Google upon deployment. (This is the same technique used if you decide to migrate your app to Google Cloud Functions or Cloud Run.)
  2. Python 2 without built-in 3P libraries (regular 3P libraries):

  • 如上创建requirements.txt
  • 在本地安装/自捆绑/复制它们,对 lib 说,通过 pip install -t lib -r requirements.txt
  • 创建 appengine_config.py,如本页第 5 步
    • Create requirements.txt as above
    • Install/self-bundle/copy them locally, say to lib, via pip install -t lib -r requirements.txt
    • Create appengine_config.py as shown in step 5 on this page
  1. Python 2 内置 3P 库(特殊的 3P 库集):

  • 上面链接的所有列出的 3P 库都是内置的",这意味着它们可在 App Engine 服务器上使用,因此您不必将它们复制/自行捆绑到您的应用中(如上面的 #2)
  • app.yaml 像这样
  • (不要将内置库放在 requirements.txt 中,也不要使用 pip install 在本地安装它们,除非您想自行捆绑,因为,假设您需要更新版本的内置库.)
  • 像上面一样创建 appengine_config.py.
    • All listed 3P libraries linked above are "built-in," meaning they're available on App Engine servers so you don't have to copy/self-bundle them w/your app (like in #2 above)
    • It suffices to list them with an available version in the libraries: section of your app.yaml like this
    • (Don't put built-in libraries in requirements.txt nor use pip install to install them locally unless you want to self-bundle because, say if you need a newer version of the built-in library.)
    • Create appengine_config.py like the above.
    • 如果您的 Python 2 应用程序具有内置 非内置 3P 库,请使用上面 #2 和 #3 中的技术(app.yamlrequirements.txt 中的非内置库,并运行上面的 pip install cmd).像 Python 3 这样的第二代运行时的改进之一是所有这些带有 3P 库的游戏都神奇地消失了(参见上面的 #1).

      If you have a Python 2 app with both built-in and non-built-in 3P libraries, use the techniques in both #2 and #3 above (built-in libraries in app.yaml and non-built-in libraries in requirements.txt and run the pip install cmd above). One of the improvements in the second generation runtimes like Python 3 is that all these games with 3P libraries go away magically (see #1 above).