如何在 Google App Engine 中包含第三方 Python 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14850853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:39:14  来源:igfitidea点击:

How to include third party Python libraries in Google App Engine?

pythongoogle-app-engine

提问by Moazzam Khan

How to add third party python libraries in Google App Engine, which are not provided by Google? I am trying to use BeautifulSoup in Google App Engine and unable to do so. But my question is for any library I want to use in Google App Engine.

如何在 Google App Engine 中添加非 Google 提供的第三方 Python 库?我正在尝试在 Google App Engine 中使用 BeautifulSoup,但无法这样做。但我的问题是针对我想在 Google App Engine 中使用的任何库。

采纳答案by Lipis

Actually I think this answerfits better here.

其实我认为这个答案更适合这里。

If you want to use 3rd party libraries that are not included in this list, then you'll have to add them manually.

如果要使用未包含在此列表中的3rd 方库,则必须手动添加它们。

In order to include manually any other library you have to have them inside the directory where the app.yamllives. So for example if you have the following structure:

为了手动包含任何其他库,您必须将它们放在存在的目录中app.yaml。例如,如果您具有以下结构:

hello
├── libs
│   └── bs4 
├── hello.py 
└── app.yaml

then in your hello.pyyou have to put these two lines in the beginning of the file:

然后在您的文件中,hello.py您必须将这两行放在文件的开头:

import sys
sys.path.insert(0, 'libs')

After doing that you'll be able to use any 3rd party library that you're going to put in that libsdirectory.

完成此操作后,您将能够使用要放入该libs目录中的任何 3rd 方库。

For example:

例如:

from bs4 import BeautifulSoup

回答by Fábio Diniz

Just put Beautifulsoup in the root of your project and upload it all

只需将 Beautifulsoup 放在您项目的根目录中并全部上传

回答by Paul Collingwood

You simply copy the folder containing the library you want to use into your app engine project.

您只需将包含要使用的库的文件夹复制到您的应用引擎项目中。

Then when you deploy it's uploaded with your application and is available for use.

然后,当您部署它时,它会与您的应用程序一起上传并可供使用。

EDIT: Jesse's answeris how I now do this. So do that!

编辑:杰西的回答是我现在如何做到这一点。所以这样做!

回答by Rafael Vogel

The way it worked here is:

它在这里工作的方式是:

import sys
# sys.path.insert(0, 'libs') #"Old" way, not working for me.
sys.path.append(os.path.join(os.path.dirname(__file__), "libs")) # This works!

Then import normally:

然后正常导入:

from bs4 import BeautifulSoup

回答by Jesse Webb

Google has provided a documented way for included third-party libraries in your GAE project.

Google 为您的 GAE 项目中包含的第三方库提供了一种记录方式。

See the "Adding Third-party Packages to the Application" section of the Libraries in Python 2.7 docs.

请参阅Python 2.7 文档中的库“向应用程序添加第三方包”部分

If you want to include additional pure-python third-party packages, you can do so by setting up vendoring. Vendoring allows you to install packages to a subdirectory of your project and include them in your code. To use vendoring, create (or modify) appengine_config.pyin the root of your project.

如果你想包含额外的纯 python 第三方包,你可以通过设置 vendoring 来实现。Vendoring 允许您将包安装到项目的子目录中并将它们包含在您的代码中。要使用 vendoring,请在项目的根目录中创建(或修改)appengine_config.py

from google.appengine.ext import vendor
# Add any libraries installed in the "lib" folder.
vendor.add('lib')

And then just put all your libs' source code in your libdir

然后把你所有的库的源代码放在你的lib目录中

> pip install beautifulsoup4 -t lib

So your project directory structure looks like this:

所以你的项目目录结构是这样的:

project
- lib
  - bs4
- your_code.py

This will allow your project's source files to import libs' packages/modules as if they were added to your PYTHON_PATH. For example:

这将允许您项目的源文件导入 libs 的包/模块,就像它们被添加到您的PYTHON_PATH. 例如:

# file: your_code.py
import bs4  # no need for 'from lib import bs4'
# do stuff with bs4...

You can also easily install everything from a requirements.txt file by doing the following command

您还可以通过执行以下命令轻松安装 requirements.txt 文件中的所有内容

> pip install -t lib -r requirements.txt

回答by Vishvajit Pathak

pip install -t lib package_name

pip install -t lib package_name

lib: the location for third party libraries

lib:第三方库的位置

Then you are good to use this package like a normal library you use from ipython or terminal.

然后,您可以像从 ipython 或终端使用的普通库一样使用此包。