简体   繁体   English

将Python模块添加到Google App Engine项目

[英]Add a Python module to a Google App Engine project

I'm trying to use the feed-parser module for this project im working on. 我正在尝试使用feed-parser模块进行此项目。 When I upload the files to App Engine and I run the script it comes back with the error that the there is no module named feed-parser. 当我将文件上传到App Engine并运行脚本时,它返回错误,提示没有名为feed-parser的模块。

So I'm wondering if and how can I install this module on App Engine, so that I can fix this error and use RSS. 因此,我想知道是否以及如何在App Engine上安装此模块,以便可以解决此错误并使用RSS。

Error: 错误:

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

  • Development 1: 发展1:

So I've tried installing the module in the lib directory i created(in this fail example i forgot the /lib at the --prefix=..). 因此,我尝试将模块安装在我创建的lib目录中(在此失败示例中,我忘记了--lib = ..处的/ lib)。 And i get PYTHONERROR as is shown in the shell. 而且我得到PYTHONERROR,如外壳所示。 Ive done some research on python paths and the solutions i tried didn't work for me. 我已经对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.

Then i tried with the "pip" command but then i get this: 然后,我尝试使用“ pip”命令,但随后得到以下信息:

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

According to what I have read "pip" should be a default program installed with python 2.7 and up. 根据我所读的内容,“ pip”应该是安装了python 2.7及更高版本的默认程序。 So to be sure i did install python3.5 and ran it with that and still get the same error. 因此,请确保我确实安装了python3.5并以此运行它,但仍然收到相同的错误。 I typed this with both pythons: 我用两个python都输入了这个:

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

-- -

Not sure if this would work, but via terminal i went to the default directory where feed parser has been installed on my system and copied it to the lib directory i made. 不知道这是否行得通,但是我通过终端进入了我的系统上已经安装了feed解析器的默认目录,并将其复制到了我创建的lib目录中。 Then I've created the config file with the following: 然后,我使用以下内容创建了配置文件:

from google.appengine.ext import vendor

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

Deployed it and im still getting the same error as above no module named feeedparser. 部署它,即时消息仍然出现与上面相同的错误,没有名为feededparser的模块。

Apologies if im doing something stupidly wrong, im still in the learning process. 抱歉,如果我做的事情很愚蠢,我仍然在学习过程中。

The App Engine documentation that explains how to add third party modules is here 此处说明了如何添加第三方模块的App Engine文档

In summary, you will need to add a folder, usually named 'lib', to the top level off your app, and then install feedparser into that folder using the commands described in the documentation. 总而言之,您将需要在应用程序的顶层添加一个通常名为“ lib”的文件夹,然后使用文档中介绍的命令将feedparser安装到该文件夹​​中。 You will also need to create an appengine_config.py file as descibed in the documentation. 您还需要按照文档中的描述创建一个appengine_config.py文件。

Note that not all third party packages can be uploaded to App Engine - those with C extensions are forbidden. 请注意,并非所有第三方软件包都可以上传到App Engine-禁止使用带有C扩展名的软件包。 Feedparser looks OK to me though. Feedparser在我看来还可以。

EDIT: further comments based on edit "development1" to the question. 编辑:基于对问题的编辑“ development1”的进一步评论。

Your appengine_config.py looks good. 您的appengine_config.py看起来不错。

You "lib" folder should be your application folder, that is the same folder as your app.yaml and appengine_config.py files. 您的“ lib”文件夹应该是您的应用程序文件夹,即与您的app.yamlappengine_config.py文件相同的文件夹。

You need to install the feedparser package into the lib folder. 您需要将feedparser软件包安装到lib文件夹中。 The Google docs recommend that you do this by running the command Google文档建议您通过运行以下命令来执行此操作

pip install -t lib feedparser

This command will install the feedparser package into your lib folder. 此命令会将feedparser软件包安装到您的lib文件夹中。

You need to install and run a version of pip that works with Python2.x - the Python3 version will create a version of feedparser that only runs under Python3. 您需要安装并运行与Python2.x兼容的pip版本-Python3版本将创建仅在Python3下运行的feedparser版本。

If you can't install a pip for Python2 this question might provide the right solution, otherwise I'd suggest you ask a separate question about how to install feedparser into a custom install directory on a Mac.I don't have a Mac so I can't help with this. 如果您无法为Python2安装pip,则此问题可能会提供正确的解决方案,否则,建议您提出一个单独的问题,有关如何将feedparser安装到Mac上的自定义安装目录中。我没有Mac,因此我对此无能为力。

Once you have feedparser installed in your lib folder, verify that your app works locally; 在lib文件夹中安装feedparser后,请验证您的应用程序是否在本地运行; in particular verify that it's using your lib/feedparser installation: try logging feedparser.__file__ after importing feedparser to check the location of the file being imported. 尤其要验证它是否正在使用您的lib / feedparser安装:导入feedparser后,尝试记录feedparser.__file__以检查要导入的文件的位置。

Once everything is working locally you should be able to upload to GAE. 一切在本地运行后,您应该可以上传到GAE。

So in summary, your appengine_config.py looks good, but you need to make sure that the right version of feedparser is installed into the lib folder in your app folder before uploading to App Engine. 因此,总而言之,您的appengine_config.py看起来不错,但是在上传到App Engine之前,您需要确保将正确版本的feedparser安装到app文件夹中的lib文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM