在谷歌云平台上部署python脚本

时间:2017-03-02 06:06:29

标签: python google-app-engine google-cloud-platform app.yaml

我正在弄清楚如何在Google云平台上部署我的脚本。 我已经创建了一个包含script.py的目录或文件夹以及/lib文件夹中的所有库。

我得到的是设置我的app.yaml以运行script.py(python 2.7)并在需要时访问lib

我也不知道自从使用第三方库以来我是否需要制作requirments.txt

这是script.py

中的所有导入
import requests
import re
import mysql.connector
from urlparse import urlparse
from urlparse import urljoin
from bs4 import BeautifulSoup

另外,我在lib中的内容是BeautifulSoup,requests和mysql.connector。 我不知道其他人我认为他们是内置的python2.7,因为我无法使用pip安装它们。

我顺便使用Windows 10。

的app.yaml

runtime: python27
api_version: 1
threadsafe: true



handlers:
- url: /lib/requests
  script: Scrape.app

handlers:
- url: /requests
  script: Scrape.app

handlers:
- url: /mysql/connector
  script: Scrape.app

handlers:
- url: /bs4/
  script: Scrape.app

cron.yaml

cron:
- description: "Scrape"
  url: /
  schedule: every 10 mins
  retry_parameters:
    min_backoff_seconds: 2.5
    max_doublings: 10

我得到错误,如

Updating service [default]...failed.                                                                                                                                                                                                          
ERROR: (gcloud.app.deploy) Error Response: [9] 
Application startup error:
/bin/sh: 1: Python: not found
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~tribal-bonito-157700/20170302t182530.399552845921654287/Scrape.py", line 3, in <module>
import requests
ImportError: No module named requests

1 个答案:

答案 0 :(得分:2)

来自https://developer.android.com/training/scheduling/alarms.html表中的脚本行:

  

脚本:指令必须是python导入路径,例如,    package.module.app 指向WSGI应用程序。使用 Python模块路径的脚本:指令的最后一个组件是   模块中全局变量的名称:该变量必须是a   WSGI应用程序,通常按惯例称为 app

     

注意:就像Python import 语句一样,每个子目录   这个包必须包含一个名为Handlers element

的文件

我建议花一些时间浏览__init__.py的代码段,您将看到简单应用的基本结构。

lib文件可用于指定要在pip install -r requirements.txt -t lib目录中安装的软件包列表,如下所示:

pip

但它不是绝对必要的,也可以直接在{{1}} cmdline上明确指定包。

相关问题