How do I run custom python script in Google App engine

Dean

Apologies for the long detailed question. Here goes...

The file has the name send_daily_report.py and uses some libraries which are detailed in a requirements.txt file.

My app.yaml looks as follows:

runtime: python27
threadsafe: false

handlers:
- url: /send_daily_report
  script: send_daily_report.py

libraries:
- name: ssl
  version: latest

My cron.yaml looks as follows:

cron:
- description: "Send unsent subscriptions to Glocell rewards every 1 minute"
  url: /send_daily_report
  schedule: every 1 day from 00:00

And finally my requirements.txt has the following contents:

Babel==2.5.3
cachetools==3.1.0
certifi==2017.11.5
chardet==3.0.4
coloredlogs==8.0
colorlog==3.1.0
et-xmlfile==1.0.1
google-api-core==1.10.0
google-auth==1.6.3
google-cloud-core==0.29.1
google-cloud-storage==1.15.0
google-resumable-media==0.3.2
googleapis-common-protos==1.5.10
humanfriendly==4.6
idna==2.6
jdcal==1.3
money==1.3.0
mysql-connector-python==8.0.5
mysqlclient==1.3.12
numpy==1.16.3
openpyxl==2.5.0
pandas==0.24.2
protobuf==3.7.1
pyasn1==0.4.5
pyasn1-modules==0.2.5
python-dateutil==2.8.0
python-magic==0.4.15
pytz==2017.3
requests==2.18.4
rsa==4.0
six==1.12.0
urllib3==1.22

When I do a:

gcloud app deploy app.yaml cron.yaml

it seems to ignore my requirements.txt and doesn't install the dependent. I even ran it in the gcloud dev server (dev_appserver.py ./app.yaml --enable_console) locally and the libraries are definitely not being installed when I use the dev server console to try and import them.

I then tried to use Python 3.7. which actually installs the libraries in requirements.txt but the problem is in the app.yaml in the handlers I cannot specify the script name. For python 3.7 that value is always set to auto as per the gcloud app.yaml reference.

What do I need to do to execute my script in gcloud app engine cron. On a linux system this would have been a simple cronjob setup in the crontab as follows:

0 0 * * * python /send_daily_py

From what i'm reading about Python 3.7, it seems like I need to have django or flex to handle the requests and route them to execute my custom .py script which seems like overkill just so that I can execute one script. Surely there is a way around this?. I even tried to include the libraries in a subfolder within my app folder as follows:

pip install --upgrade -r requirements.txt -t ./lib

Then I added init.py within lib, and then I changed all my imports to use

import lib.name_of_library

Which did not work as those libraries could subsequently not import sub packages they need.

**

Heeeeelp!

**

Dan Cornilescu

App Engine is PaaS product, not an IaaS one (on which you could, indeed, just run a linux image and install the cron you mentioned). You cannot run arbitrary standalone python scrips in GAE. You might be able to achieve what you want by re-working the script to meet the GAE apps requirements - basically make the functionality callable from inside a HTTP(S) handler.

For the 1st generation standard environment (python27 runtime):

  • the requirements.txt file isn't used by GAE. As you discovered, you can use it to vendor in your python dependencies, but there's more to do, see Copying a third-party library.
  • your script functionality needs to be re-worked as a WSGI app, which is what you configure in your app.yaml. From Handlers element:

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 ** ** by convention.

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

For the 2nd generation standard environment (python37 runtime):

  • dependencies are automatically installed from your requirements.txt file, see Specifying Dependencies
  • only auto can be specified in a script: statement in app.yaml, because the app itself is specified via the entrypoint: config. So you need need to rework your script to be invoked as handler in that app. From Runtime and app elements:

For your app to receive HTTP requests, entrypoint should contain a command which starts a web server that listens on the port specified by the PORT environment variable.

The flexible environment (with similar re-work as for the 2nd gen standard one) could be a better fit, especially because you can configure instances with more ram/cpu resources (which you might need judging by your requirements.txt file) than in the standard environment.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Run a python script on schedule on Google App Engine

How do I run Selenium tests with Google App Engine?

How can I make a cron.yaml file to run python script every hour for google cloud app engine

How do I run Google's aefix tool when upgrading a Google App Engine app to the flexible environment?

Google app engine or do i need Google apps script?

How do I run a python script on boot on Google Coral?

Use Google App Engine to run a basic python script daily

How do I run a cron job 45 minutes past the hour on google app engine?

Rot13 Example: How do I run this piece code in Google App Engine?

How do I use Google API to add custom domain mapping in App Engine?

Can I run PHP files on Google App Engine for Python?

How can I run PHP Service on Google Cloud App Engine

How to run MQTT subscriber scripts written in python on google app engine

Python and Google App Engine: How to import a custom module?

How do I run private docker images on Google Container Engine

How do I run traefik behind Kubernetes on Google Container Engine?

How to pass a parameter to a python script deployed on Google App Engine?

How to run Google App Engine app indefinitely

How do I install and run a python script

How do I install a custom kernel on a google compute engine instance?

How do I manage third-party Python libraries with Google App Engine? (virtualenv? pip?)

How do I enable Numpy on Google App Engine with Python38

How do I rollback the appcfg in Google App Engine's Python SDK?

Run python script on Google Cloud Compute Engine

How do I enable session support in Google App Engine (Java)?

How do I update a query from Google App Engine NDB?

How do I know if an Google App Engine instance is Standard or Flexible?

How do I debug Google App Engine servlets?

How do I get SoapClient working with Google App Engine (GAE)?