Is it possible to use google.appengine.api without Python2 in dev?

As I look into leaving Python2 behind, I try to replace functionality that I have always used in  App Engine (mail, memcache, taskqueue, etc) with APIs available in Python3 and using the path of least resistance. I was under the impression that google.appengine.api was now supported by Python3, but I cannot get it to work in dev without using dev_appserver.py (i.e. Python2).

I get this error: Attempted RPC call without active security ticket.
when I try things like:

 

data = memcache.get(memkey)

 

or

 

mail.send_mail(...)

 

It sure works when deployed to the cloud, but not in my local dev. env. unless I use dev_appserver.py to run the app.

My app.yaml looks like this:

 

runtime: python39
app_engine_apis: true
...

 

My app's main.py like this:

 

...
#Flask
from flask import Flask, render_template, request, send_from_directory, redirect

#imports from Standard library
import google.appengine.api #just in case
from google.appengine.api import mail
from google.appengine.api import memcache
from google.appengine.api import wrap_wsgi_app

# Datastore API
from google.cloud import datastore


#Create app instance
app = Flask(__name__)
app.wsgi_app = wrap_wsgi_app(app.wsgi_app)
...

 

also, I have appengine-python-standard>=1.0.0 in requirements.txt.

and I run my app like this:

 

c:\path_to_venv_python39\python.exe main.py

 

Is it possible to use the google.appengine.api library strictly on Python3 and I'm missing something in my app? Or is that the only way to use this library is in combination with dev_appserver.py and Python2?

Thanks.

Solved Solved
0 9 749
1 ACCEPTED SOLUTION

@robertcarlos  @NoCommandLine 

Just wanted to let you know that I created a new feature request for this:

https://issuetracker.google.com/issues/309559752

Thank you for bringing those resources to my attention.

 

View solution in original post

9 REPLIES 9

Hi @micdearmas,

Welcome to Google Cloud Community!

You can still use google.appengine.api as this would create the WGSI middleware that sets the variables required to enable your API calls. You Flask code should look like this:

from flask import Flask
from google.appengine.api import wrap_wsgi_app

app
= Flask(__name__)
app
.wsgi_app = wrap_wsgi_app(app.wsgi_app)

You should also add this in your app.yaml before deploying:

app_engine_apis: true

You can then use the gcloud app deploy command.

It is recommended to use a venv to create a virtual Python 3 environment rather than using dev_appserver. You can still use dev_appserver.

You may refer to the following documentations as your reference in accessing legacy bundled services in Python 3 and migrating to the Python 3 runtime:

Hope this helps.

Thank you @robertcarlos for your reply and the references. I am glad to be able to use google.appengine.api as that has saved me a lot of refactoring in my code base (across several apps).

However, the question was whether we could use it in our local dev. env. without using Python2 and dev_appserver. From one of the links(pages) you referenced, I found this:

dev_appserver.JPG

 

 

 

I believe that answers my question although is not what I was hoping for. I already have the code in my apps migrated to Python3, but it seems that as long as I use anything in google.appengine.api, I do have to run the app in my dev. env. using dev_appserver for which Python2 is needed.

Please, correct if I'm wrong.

Thanks again.

 

Hi @micdearmas,

Apologies for that. It should be for testing local environment when migrating to Python 2 to 3. You can still use  dev_appserver with Python 3 and set  CLOUDSDK_DEVAPPSERVER_PYTHONto the path of your Python 2 interpreter.

Got it, but what if I no longer wanted to use dev_appserver in my dev. env. (because I don't use Python2 for anything else), but needed to use functionality available in google.appengine.api. Is there a way to do that? Thanks.

Hi @micdearmas,

I think you can import google.appengine.apiin your main.py and call it on your WSGI app object. 

import google.appengine.api

def app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    yield b'Hello world!\n'

app = google.appengine.api.wrap_wsgi_app(app)

 You can check this Github link for more information.

If this wouldn't work, I would suggest to file this as a feature request so that our engineers could take a look at it. We don't have a specific ETA on this but you can keep track of its progress once the ticket has been created.

Ok, I'll see if I put a feature request because I believe I'm already doing everything as explained in that link,(I use Flask, so I do it as explained for that case)  but whenever I try to use anything from google.appengine.api, I get this error:

google.appengine.runtime.apiproxy_errors.RPCFailedError: Attempted RPC call without active security ticket

I have the security set as this:

 

set GOOGLE_APPLICATION_CREDENTIALS=C:\path_to_security\application_credentials.json

 

in the same batch file that runs the app like this:

 

c:\path_to_venv_python39\python.exe main.py

 

so that I can use things like this:

 

from google.cloud import datastore
ds_client = datastore.Client()
...
# do datastore operations
...

 

and that part it's working fine, but I cannot get working that and google.appengine.api in the same app.

By the way, I'm using Cloud SDK 431.

 

 

 

 

Attempted RPC call without active security ticket

I get the same error too when trying to use any of the bundled APIs without dev_appserver.py. Others also get the same error. Maybe this response from someone on Github will shed some light.

 

..... NoCommandLine ......
 https://nocommandline.com
A GUI for Google App Engine

Thank you @NoCommandLine , it looks like the issue was reported two years ago and it was closed earlier this year...wonder why it was closed.

Do you know if there's an already existing 'feature request' for this functionality? That way I could vote for it instead of trying to create a new one.

 

@robertcarlos  @NoCommandLine 

Just wanted to let you know that I created a new feature request for this:

https://issuetracker.google.com/issues/309559752

Thank you for bringing those resources to my attention.