Deploy python script on google cloud platform

2019-02-27 21:54发布

问题:

I'm figuring out how to deploy my script on google cloud platform. ive already made a directory or folder that contains the script.pyand all the libraries in /lib folder.

what i dont get is setting up my app.yaml to run script.py (python 2.7) and access lib if it needs to.

I also dont know if i need to make requirments.txt since im using third party libraries.

here are all my imports inside script.py

import requests
import re
import mysql.connector
from urlparse import urlparse
from urlparse import urljoin
from bs4 import BeautifulSoup

Also, what i have in my lib are BeautifulSoup,requests and mysql.connector. i dont know about the others i assume they're python2.7 built in since i cant install them using pip.

im using windows 10 by the way.

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

im getting errors like

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:

From the script row in the Handlers element table:

A script: directive must be a python import path, for example, package.module.app that points to a WSGI application. The last component of a script: directive using a Python module path is the name of a global variable in the module: that variable must be a WSGI app, and is usually called app by convention.

Note: just like for a Python import statement, each subdirectory that is a package must contain a file named __init__.py

I'd recommend spending some time going through the code snippets from Quickstart for Python App Engine Standard Environment, where you'll see a basic structure of a simple app.

A requirements.txt file can be used to specify the list of packages to be installed in the lib directory, like this:

pip install -r requirements.txt -t lib

But it's not absolutely necessary, packages can be explicitly specified directly on the pip cmdline as well.