3

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
Boneyflesh
  • 323
  • 1
  • 4
  • 16

1 Answers1

3

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.

Dan Cornilescu
  • 37,297
  • 11
  • 54
  • 89
  • can i set in `app.yaml` when to run the code? for example every 24hrs app enging is gonna run the code. – Boneyflesh Mar 02 '17 at 09:04
  • oh boy,,heres another term i stumbled upon`cron.yaml`. any ideas where to add `X-Appengine-Cron: true` or am i doing this wrong? – Boneyflesh Mar 02 '17 at 09:17
  • Spend some time on the documentation site, you need get your bearings first. Check the left-side navigation bar, you'll find articles on How-to Guides, Concepts, Tutorials, References, etc. For cron start with https://cloud.google.com/appengine/docs/standard/python/config/cron – Dan Cornilescu Mar 02 '17 at 13:58