Testing Google App Engine apps - python

I am looking to see how others are testing their GAE apps. I've come across a few unit testing frameworks for python (green, pytest, nose, etc).
I then found the tutorial on the GAE site: https://developers.google.com/appengine/docs/python/tools/localunittesting and have been following that.
However, after doing what they say I get the following error:
Traceback (most recent call last):
File "tests/test.py", line 2, in <module>
from google.appengine.ext import db
ImportError: No module named appengine.ext
I am just running
python testrunner.py main/tests usr/local/google_appengine
from the terminal.
Any other ideas?
I have appengine in my pythonpath:
echo $PYTHONPATH
-> /usr/local/google_appengine

Related

Problems importing from Google App Engine SDK in PyCharm Pro

Using PyCharm Pro 2019.1.1 on MacOS, writing my app in Python 3 w/Flask, Jinja2, etc. A version of my app is already deployed using App Engine Flexible, and I want the next version to use Datastore for my production environment variables. Having problems trying to follow this post, which requires the following import:
from google.appengine.ext import ndb
For the life of me, I cannot seem to get this import working. I'm sure it will work once my app is deployed (famous last words?), but I'd love to get it working locally too.
Initially tried pip install google-appengine and pip install appengine-sdk, only to discover neither could be installed on my venv.
Google Cloud SDK v241.0.0 is installed at /Users/<me>/google-cloud-sdk, and gcloud commands work in the terminal. gcloud components list shows the app-engine-python component is installed, which is probably irrelevant since I'm using a venv in PyCharm.
PyCharm's documentation instructed me to select the App Engine SDK directory under Languages & Frameworks > Google App Engine.
Selecting /Users/<me>/google-cloud-sdk yields this error:
Traceback (most recent call last):
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/pydevconsole.py", line 5, in <module>
from _pydev_comm.rpc import make_rpc_client, start_rpc_server, start_rpc_server_and_make_client
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/_pydev_comm/rpc.py", line 4, in <module>
from _pydev_comm.server import TSingleThreadedServer
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/_pydev_comm/server.py", line 4, in <module>
from _shaded_thriftpy.server import TServer
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/third_party/thriftpy/_shaded_thriftpy/server.py", line 5, in <module>
import logging
File "/Users/<me>/google-cloud-sdk/lib/surface/logging/__init__.py", line 22, in <module>
from googlecloudsdk.calliope import base
ModuleNotFoundError: No module named 'googlecloudsdk'
Selecting /Users/<me>/google-cloud-sdk/platform/google_appengine seems to get me a bit further, but still yields an error:
Traceback (most recent call last):
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/pydevconsole.py", line 5, in <module>
from _pydev_comm.rpc import make_rpc_client, start_rpc_server, start_rpc_server_and_make_client
File "/Users/<me>/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/191.6605.12/PyCharm.app/Contents/helpers/pydev/_pydev_comm/rpc.py", line 1, in <module>
import socket
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 53, in <module>
from enum import IntEnum, IntFlag
ImportError: cannot import name 'IntFlag' from 'enum' (/Users/<me>/google-cloud-sdk/platform/google_appengine/lib/grpcio-1.9.1/enum/__init__.py)
As the console notes, there is no IntFlag object in 'enum'. These errors are immediately shown when I launch Python Console; I don't even get a cursor. Where am I going wrong?
The post you referenced applies to the 1st generation standard environment - Python 2.7 - that's the only environment in which the ndb library is supported:
The Python NDB Client Library Overview
For the other environments you need to use the generic datastore client library. References:
Using Cloud Datastore - for the 2nd generation standard environment
Using Cloud Datastore - for the flexible environment
Potentially of interest: How to tell if a Google App Engine documentation page applies to the standard or the flexible environment

ModuleNotFoundError: No module named 'tasklets'

I'm playing with GAE but I realized that when I try to start the .py file I get this error. What can I do?
Thanks
python3 test.py runserver
Traceback (most recent call last):
File "test.py", line 6, in <module>
from google.appengine.ext import ndb
File "/usr/local/lib/python3.6/dist-packages/google/appengine/ext/ndb/__init__.py", line 7, in <module>
from tasklets import *
ModuleNotFoundError: No module named 'tasklets'
The NDB library is only available in the standard environment GAE, which only works with python 2.7.
So either switch to the standard environment (and python 2.7) or use the flexible environment instructions for Using Cloud Datastore.
Maybe of interest: How to tell if a Google App Engine documentation page applies to the standard or the flexible environment

Flask App Not Running in Localhost

I'm working through Miguel Grinberg's Flask Mega-Tutorial, and am unable to run the basic app in Part I. I am using Ubuntu, and getting the following traceback:
Traceback (most recent call last):
File "./run.py", line 2, in <module>
from app import app
File "/home/makisupa43/dev/microblog/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
I have double checked code and all looks correct. Not sure if I'm getting screwed up with VirtualEnv or if it is a separate issue.
Reading through the tutorial, it looks like Miguel skips the step where you actually activate the virtual environment. Run this command from the directory where you're doing all your pip installs:
. flask/bin/activate
This should put you in the correct virtual environment, which will make everything you've installed with pip available to your run.py script.

How to install the python library - google-api-python-client-gae?

I'm trying to code a program to check files in Google Cloud Storage using Python.
Now I'm read the google doc -> https://developers.google.com/api-client-library/python/start/installation.
At the last step - "App Engine", it asks user to do following:
->> cd myproject
->> unzip google-api-python-client-gae-1.1.zip
So what's the next steps? I'm still having problem to run the Google sample python code about GCS, which throw error:
Traceback (most recent call last):
File "gcs_test.py", line 22, in <module>
from google.appengine.ext import webapp
ImportError: No module named google.appengine.ext
I tried to add below code but still got same error:
from sys import path
path.append(r"C:\Py_dev\gcs_test\google-api-python-client-gae-1.2.zip")
path.append(r"C:\Py_dev\gcs_test\C:\Py_dev\gcs_test\apiclient")
Thanks for all kind help!
google.appengine is the library used by App Engine apps, not by the api client library, the library you are looking for is apiclient
If you wish to run a normal python script instead of an App Engine app, you should remove references to App Engine specific libraries.
If you are running an app engine app, make sure you are using dev_appserver.py or the actual app engine servers, not just running the script using python

Google App Engine - Production Only - Module Import Error

I am getting this error when running the app in production. It works fine in my local environment. But, after I upload to server, it throws this exception.
The test_handler module exists in test package (and it works fine in local). I believe that the entire folder structure will be uploaded to the server, when we upload.
Also, is there a way to see the entire folder structure in the Google Application Account online?
============================================================================
<type 'exceptions.ImportError'>: cannot import name test_handler
Traceback (most recent call last):
File "/base/data/home/apps/mad-scribe/1.346944987034829366/url_handler.py", line 15, in <module>
from test import test_handler
You can now download the source if you were the developer who uploaded it.
This should give you a concrete understanding of what was uploaded.
AppEngine Docs: Downloading Source Code
The issue was with the package name. I named the package as 'test', which might have stepped on the shoes of some other package.
from test import test_handler
Renaming the package name (test -> test_handlers) fixed the issue.
Thanks to all who responded to this question.

Categories

Resources