使用 Google 的 Python App Engine 的 Flask 骨架,如何使用 Pip 安装额外的库?

With Google's Flask skeleton for Python App Engine, how can install additional libraries with Pip?

要安装依赖项,appengine-python-flask-skeleton docs 建议 运行 使用此命令:

pip install -r requirements.txt -t lib

这很简单。

现在说我要添加 Requests package

理想情况下,我只是将其添加到 requirements.txt 文件中:

# This requirements file lists all third-party dependencies for this project.
#
# Run 'pip install -r requirements.txt -t lib/' to install these dependencies
# in `lib/` subdirectory.
#
# Note: The `lib` directory is added to `sys.path` by `appengine_config.py`.
Flask==0.10
requests

然后重新运行命令:

pip install -r requirements.txt -t lib

不过,作为pip笔记的this Github issue,pip与这里Google推荐的-T选项不是幂等的。现有的烧瓶包将被重新添加,这将在 运行 开发应用程序

时导致以下错误
ImportError: cannot import name exceptions

我怎样才能最好地解决这个问题?

升级到最新版本的 pip 解决了我的问题(该问题已关闭):

pip install -U pip

否则,如该线程中所述,您始终可以清除 lib 目录并从头开始重新安装。一个警告注意事项:如果您手动将其他包添加到 lib 目录中,但未在 requirements.txt 中跟踪,它们将会丢失并且必须手动重新安装。

如前所述,更新 pip 解决了很多问题,但就其价值而言,我认为如果使用 virtualenv is an option. Symlink /path/to/virtualenv's/sitepackages/ to lib/ and just always keep an up to date requirements.txt file. There are no duplication of packages this way and one won't have to manually install dependencies. See also

,您可以解决所有这些问题