Google Cloud Platform deploys python scripts
I’m figuring out how to deploy my script on Google Cloud Platform.
I have created a directory or folder with all the libraries in the script.py
and /lib
folders.
What I don’t get is setting up my app.yaml
to run script.py
(python 2.7) and access lib
to when needed.
I
also don’t know if I need to make requirements.txt
because I’m using a third-party library.
Here is everything I imported in script.py
import requests
import re
import mysql.connector
from urlparse import urlparse
from urlparse import urljoin
from bs4 import BeautifulSoup
Also, I have BeautifulSoup, requests, and mysql.connector in my lib
.
I don’t know the others I think they are built-in python 2.7 because I can’t install them using pip.
By the way, I’m using Windows 10.
Apply.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
I get an error like this
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
Solution
From Handlers element Script line 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 recommend spending some time browsing Quickstart for Python App Engine Standard Environment in the code snippet, where you see the basic structure of a simple app.
The requirements.txt
file can be used to specify a list of packages to install in the lib
directory, as follows:
pip install -r requirements.txt -t lib
But this is not absolutely necessary, and packages can also be explicitly specified directly on the pip
command line.