【问题标题】:Add a Python module to a Google App Engine project将 Python 模块添加到 Google App Engine 项目
【发布时间】:2016-03-18 17:53:56
【问题描述】:

我正在尝试将 feed-parser 模块用于我正在进行的这个项目。当我将文件上传到 App Engine 并运行脚本时,它会返回错误,即没有名为 feed-parser 的模块。

所以我想知道是否以及如何在 App Engine 上安装此模块,以便修复此错误并使用 RSS。

错误:

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/s~vlis-mannipulus-bot/1.391465315184045822/main.py", line 7, in <module>
    import feedparser
ImportError: No module named feed parser

  • 开发一:

所以我尝试将模块安装在我创建的 lib 目录中(在这个失败示例中,我忘记了 --prefix=.. 处的 /lib)。我得到了 PYTHONERROR,如 shell 所示。我对 python 路径进行了一些研究,但我尝试过的解决方案对我不起作用。

kevins-MacBook-Pro-2:~ KevinH$ cd /Users/KevinH/Downloads/feedparser   -5.2.1 
kevins-MacBook-Pro-2:feedparser-5.2.1 KevinH$ sudo python setup.py     install --prefix=/Users/KevinH/Documents/Thalia\ VMbot/Thalia-VMbot/
Password:
running install
Checking .pth file support in /Users/KevinH/Documents/Thalia     VMbot/Thalia-VMbot//lib/python2.7/site-packages/
/usr/bin/python -E -c pass
TEST FAILED: /Users/KevinH/Documents/Thalia VMbot/Thalia-    VMbot//lib/python2.7/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH

You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from.  The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

/Users/KevinH/Documents/Thalia VMbot/Thalia-VMbot//lib/python2.7/site-   packages/

and your PYTHONPATH environment variable currently contains:

''

Here are some of your options for correcting the problem:

* You can choose a different installation directory, i.e., one that is
on PYTHONPATH or supports .pth files

* You can add the installation directory to the PYTHONPATH environment
variable.  (It must then also be on PYTHONPATH whenever you run
Python and want to use the package(s) you are installing.)

* You can set up the installation directory to support ".pth" files by
using one of the approaches described here:

https://pythonhosted.org/setuptools/easy_install.html#custom-  installation-locations

Please make the appropriate changes for your system and try again.

然后我尝试使用“pip”命令,但后来我得到了这个:

can't open file 'pip': [Errno 2] No such file or directory

根据我阅读的内容,“pip”应该是安装了 python 2.7 及更高版本的默认程序。所以为了确保我确实安装了 python3.5 并用它运行它并且仍然得到同样的错误。我用两个 python 都输入了这个:

kevins-MacBook-Pro-2:feedparser KevinH$ python3 pip -m install feedparse

--

不确定这是否可行,但通过终端,我进入了系统上已安装提要解析器的默认目录,并将其复制到我创建的 lib 目录中。然后我使用以下内容创建了配置文件:

from google.appengine.ext import vendor

# Add any libraries installed in the "lib" folder.
vendor.add('lib')

部署它,我仍然得到与上面相同的错误,没有名为 feedparser 的模块。

抱歉,如果我做错了愚蠢的事情,我还在学习过程中。

【问题讨论】:

  • 您的应用中的 feedparser 文件在哪里?另外,如果您有 appengine_config.py 文件的内容,您可以发布一下吗?
  • @snakecharmerb 它不在我的应用程序文件中,它自动在我的系统上安装了模块。我应该找到一种方法将其安装在 aap 根文件中,不是吗?不,我还没有 appengine_config.py 文件。根据我的研究,您处理模块的文件是什么?

标签: python google-app-engine


【解决方案1】:

解释如何添加第三方模块的 App Engine 文档是 here

总而言之,您需要在应用程序的顶层添加一个通常名为“lib”的文件夹,然后使用文档中描述的命令将 feedparser 安装到该文件夹​​中。您还需要按照文档中的说明创建 appengine_config.py 文件。

请注意,并非所有第三方包都可以上传到 App Engine - 那些带有 C 扩展名的包是被禁止的。 Feedparser 在我看来还不错。

编辑:基于对问题的编辑“development1”的进一步cmet。

您的appengine_config.py 看起来不错。

您的“lib”文件夹应该是您的应用程序文件夹,即与您的 app.yamlappengine_config.py 文件相同的文件夹。

您需要将 feedparser 包安装到 lib 文件夹中。 Google 文档建议您通过运行命令来执行此操作

pip install -t lib feedparser

此命令会将 feedparser 包安装到您的 lib 文件夹中。

您需要安装并运行一个适用于 Python2.x 的 pip 版本 - Python3 版本将创建一个只能在 Python3 下运行的 feedparser 版本。

如果您无法为 Python2 安装 pip this question 可能会提供正确的解决方案,否则我建议您单独询问有关如何将 feedparser 安装到 Mac 上的自定义安装目录中的问题。我不有一台 Mac,所以我无能为力。

在您的 lib 文件夹中安装 feedparser 后,请验证您的应用是否在本地运行;特别要验证它是否使用了您的 lib/feedparser 安装:在导入 feedparser 后尝试记录 feedparser.__file__ 以检查正在导入的文件的位置。

一旦一切都在本地运行,您应该能够上传到 GAE。

总而言之,您的appengine_config.py 看起来不错,但您需要确保在上传到 App Engine 之前将正确版本的 feedparser 安装到您的 app 文件夹中的 lib 文件夹中。

【讨论】:

  • 当我尝试将其安装在非默认目录中时出现此错误:`您正在尝试将软件包安装到不在 PYTHONPATH 上且 Python 未读取的目录中“ .pth”文件来自。您指定的安装目录(通过 --install-dir、--prefix 或 distutils 默认设置)是 `
  • 我已经设法在 lib 目录中获取提要 parser.egg 文件并添加了 appegine 配置文件,但我仍然收到相同的错误 X__X 如果我用这些发展编辑第一个问题让你看到?
  • 是的,请复制并粘贴您输入的命令和输出。
  • 好的,我已经编辑了“开发 1”下的问题。我很感激你花时间帮助我:)
  • 是的!我已经设法安装 pip 并将模块安装在 lib 文件夹中。我部署到 GAE 并没有收到错误“无模块”?。非常感谢先生。
猜你喜欢
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 2013-06-12
  • 2011-10-08
相关资源
最近更新 更多