Adding Google Analytics API Library to Google App Engine - python

I am trying to run a simple python script on Google App Engine. How do I install the Google Analytics API library?
Library: https://developers.google.com/api-client-library/python/apis/analytics/v3
Instructions: https://cloud.google.com/appengine/docs/python/tools/libraries27#vendoring
I've tried everything and can't get this to run, even though it works on my pc. What I have right now is:
The python scripts in the root folder,
In the /lib folder I copied the folders that were installed from my PC (/googleapiclient and /google_api_python_client-1.4.0-py2.7.egg-info)
And I have appengine_config.py in /lib folder which contains:
from google.appengine.ext import vendor
# Add any libraries installed in the "lib" folder.
vendor.add('lib')
vendor.add('google-api-client')
app.yaml file:
application: psyched-cab-861
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: hello_analytics_api_v3.app
Traceback:
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])
ImportError: No module named helloworld
New Log:
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/api/lib_config.py", line 354, in __getattr__
self._update_configs()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 290, in _update_configs
self._registry.initialize()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 165, in initialize
import_func(self._modname)
File "/base/data/home/apps/s~just-terminus-94303/1.384249106864280829/appengine_config.py", line 5, in <module>
vendor.add('google-api-python-client')
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/vendor/__init__.py", line 44, in add
'No such virtualenv or site directory' % path)
ValueError: virtualenv: cannot access google-api-python-client: No such virtualenv or site directory
I tried editing the appengine_config.py file to
vendor.add('googleapiclient') #The name of the file
I edit it in GAE, and click commit, it saves, but I get the same error as above with the vendor.add('google-api-python-client') error. Why is the file not updating?

As the docs say: put the appengine_config.py in your root folder of the application (and not in the /lib folder).
By the way, the name of the library is google-api-python-client and not google-api-client.
Then you have to install google-python-api-client in your /lib folder:
$ pip install -t lib google-api-python-client

Related

App Engine + Django: ImportError: No module named django.core.wsgi

I'm deploying a simple Django app to Google App Engine. How can I fix the following?
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 96, in LoadObject
__import__(cumulative_path)
File "/base/data/home/apps/.../20171104t152156.405293023907909181/mysite/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
You're getting the ImportError because Django is not in your sys.path.
If you want to use one of the built-in Django versions from the App Engine SDK, simply add this to your app.yaml (it's not necessary to install the Django library separately):
libraries:
- name: django
version: "1.4"
Update:
If you want to include your own Django version with your app (eg. to use a recent version > 1.5), don't add the above line and instead install the library directly into your project's root directory:
$ cd myapp/
$ pip install django -t .

Google App Engine: No module named google.api

I have installed the latest version of google cloud sdk, google-cloud-sdk-app-engine-python on my Ubuntu PC as mentioned in the docs in-order to test google-cloud-endpoints-framework sample app.
But on invoking an api request, I got the below traceback. Seems like there is a conflict between google package inside GAE sdk and the google package installed automatically to the lib folder because of google-endpoints package.
$ dev_appserver.py app.yaml
INFO 2017-03-14 07:51:36,173 devappserver2.py:764] Skipping SDK update check.
INFO 2017-03-14 07:51:36,199 api_server.py:268] Starting API server at: http://localhost:44561
INFO 2017-03-14 07:51:36,213 dispatcher.py:199] Starting module "default" running at: http://localhost:8080
INFO 2017-03-14 07:51:36,213 admin_server.py:116] Starting admin server at: http://localhost:8000
INFO 2017-03-14 07:51:45,811 module.py:806] default: "GET /_ah/start HTTP/1.1" 404 -
ERROR 2017-03-14 07:51:45,877 wsgi.py:263]
Traceback (most recent call last):
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/home/gemini/gae projects/python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo/main.py", line 19, in <module>
import endpoints
File "/home/gemini/gae projects/python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo/lib/endpoints/__init__.py", line 29, in <module>
from apiserving import *
File "/home/gemini/gae projects/python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo/lib/endpoints/apiserving.py", line 74, in <module>
from google.api.control import client as control_client
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 1001, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named google.api
I tried creating a seperate virtualenv but the problem still exists.
Here is the reply from a google guy..
Local development with endpoints framework v2 isn't currently supported, you'll need to deploy the app.
https://github.com/GoogleCloudPlatform/python-docs-samples/issues/853
Your error :
ImportError: No module named google.api
So you need first to install gcloud python module and google-api-python-client module with:
pip install --upgrade gcloud
pip install --upgrade google-api-python-client
from here
I had a similar issue with other Google packages in my lib directory.
I solved/monkey patched it using the following code in my appengine_config.py file:
import sys
import os
import google
from google.appengine.ext import vendor
lib_directory = os.path.dirname(__file__) + "<relative path to lib dir>"
google.__path__.append(os.path.join(lib_directory, 'google'))
logging.info("importing lib %s" % (lib_directory))
vendor.add(lib_directory)

dev_appserver.py not starting development server

I've been following this tutorial here in an attempt to build an app on Google app engine:
https://github.com/GoogleCloudPlatform/appengine-django-skeleton
I've hit a brick wall though in that when I run dev_appserver.py, 2 out of 3 servers run fine. But the development server just returns 500 errors.
The exact console output is the following:
ubuntu#ubuntu:~/Desktop/appengine-django-skeleton$ dev_appserver.py --port=9999 app.yaml
INFO 2015-12-15 15:50:11,507 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO 2015-12-15 15:50:11,691 sdk_update_checker.py:257] The SDK is up to date.
INFO 2015-12-15 15:50:11,807 api_server.py:205] Starting API server at: http://localhost:60930
INFO 2015-12-15 15:50:11,993 dispatcher.py:197] Starting module "default" running at: http://localhost:9999
INFO 2015-12-15 15:50:11,993 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR 2015-12-15 15:50:16,395 wsgi.py:263]
Traceback (most recent call last):
File "/home/ubuntu/Desktop/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/ubuntu/Desktop/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/home/ubuntu/Desktop/google_appengine/google/appengine/runtime/wsgi.py", line 96, in LoadObject
__import__(cumulative_path)
File "/home/ubuntu/Desktop/appengine-django-skeleton/mysite/wsgi.py", line 29, in <module>
application = get_wsgi_application()
File "/home/ubuntu/Desktop/appengine-django-skeleton/lib/django/core/wsgi.py", line 13, in get_wsgi_application
django.setup()
File "/home/ubuntu/Desktop/appengine-django-skeleton/lib/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ubuntu/Desktop/appengine-django-skeleton/lib/django/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/ubuntu/Desktop/appengine-django-skeleton/lib/django/django/apps/config.py", line 131, in create
"'%s' isn't a subclass of AppConfig." % entry)
ImproperlyConfigured: 'django.contrib.admin.apps.AdminConfig' isn't a subclass of AppConfig.
And I'm guessing that the key line here is
ImproperlyConfigured: 'django.contrib.admin.apps.AdminConfig' isn't a subclass of AppConfig.
No code changes have been made to the file from github. Only the configuration changes listed on the github page.
I have manged to solve this problem by installing all the modules required on virtualenv not inside the lib directory. After cloning the repository, run this command.
pip install -r requirements.txt

Cannot run Hello World on Google App Engine Windows

I have downaloded and installed Python 2.7.6 in my Windows and I have also installed the latest version of the GAE SDK for Windows as well.
I have the following configurations on my GAE:
I am running an Hello World project with the following files:
Main.py
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
app.yaml
application: HelloWorld
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
However, When I click the button Run, in order to runt he project, I get the following errors in the LOG output:
2013-12-04 14:51:49 Running command: "['C:\\Python27\\pythonw.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=8080', '--admin_port=8000', 'D:\\Users\\pedro\\Desktop\\HelloWorld']"
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 197, in <module>
_run_file(__file__, globals())
File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 193, in _run_file
execfile(script_path, globals_)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 872, in <module>
main()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 865, in main
dev_server.start(options)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 657, in start
options.yaml_files)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\application_configuration.py", line 549, in __init__
module_configuration = ModuleConfiguration(yaml_path)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\application_configuration.py", line 82, in __init__
self._yaml_path)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\application_configuration.py", line 266, in _parse_configuration
return appinfo_includes.ParseAndReturnIncludePaths(f)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\appinfo_includes.py", line 63, in ParseAndReturnIncludePaths
appyaml = appinfo.LoadSingleAppInfo(appinfo_file)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\appinfo.py", line 1756, in LoadSingleAppInfo
listener.Parse(app_info)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\yaml_listener.py", line 226, in Parse
self._HandleEvents(self._GenerateEventParameters(stream, loader_class))
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\yaml_listener.py", line 177, in _HandleEvents
raise yaml_errors.EventError(e, event_object)
google.appengine.api.yaml_errors.EventError: Unable to assign value 'HelloWorld' to attribute 'application':
Value 'HelloWorld' for application does not match expression '^(?:(?:[a-z\d\-]{1,100}\~)?(?:(?!\-)[a-z\d\-\.]{1,100}:)?(?!-)[a-z\d\-]{0,99}[a-z\d])$'
in "D:\Users\pedro\Desktop\HelloWorld\app.yaml", line 1, column 14
2013-12-04 14:51:50 (Process exited with code 1)
Thus I have the following questoins:
I have created this project by going to File -> New application, but apparently my auto-generated app.yaml file is incorrect. What am I doing wrong?
I have tried to use Python 3.3 first, but it was not working. GAE only works with Python 2.7 right? I am doing everythign correctly right?
When I click the Browse button I get the error saying that no browser is specified. How do I fix that?
Thanks for any help if possible :S
According to the regex that isn't matching, the app name needs to be lowercase:
>>> regex = r'^(?:(?:[a-z\d\-]{1,100}\~)?(?:(?!\-)[a-z\d\-\.]{1,100}:)?(?!-)[a-z\d\-]{0,99}[a-z\d])$'
>>> print(re.match(regex, 'HelloWorld'))
None
>>> print(re.match(regex, 'helloworld'))
<_sre.SRE_Match object at 0x13ac308>
Your application identifier('HelloWorld') is invalid. You should use the one you created in the Administration Console according to the GAE doc.
The ID will be the subdomain of your GAE application when it deployed in appspot.com. That's why the app id should match the regex r'^(?:(?:[a-z\d\-]{1,100}\~)?(?:(?!\-)[a-z\d\-\.]{1,100}:)?(?!-)[a-z\d\-]{0,99}[a-z\d])$'.

IOError: [Errno 13] file not accessible GAE

I want to access a file in my computer from a google app engine application. If I put the file in the src folder of the application there is no problem but I need this file to be out of that folder.
When I try to access this file I get the next error:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 712, in __call__
handler.post(*groups)
File "C:\Users\path\Documents\path\oauth2client\appengine.py", line 469, in check_oauth
return method(request_handler, *args, **kwargs)
File "C:\Users\path\Documents\path\myapp.py", line 88, in post
filename=open('C:/Users/path_to_file/Documento 1.docx','r')
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 635, in __init__
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: 'C:/Users/path_to_file/Documento 1.docx'
I have read that if a file is defined as static in app.yaml can't be accessible from GAE. This is my app.yaml. I can't figure out if there is something wrong:
application: myapp
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: myapp.py
And this is the part of the application that tries to access the file:
filename='C:/Users/path_to_file/Documento 1.docx'
media_body = MediaFileUpload(filename,resumable=True)
Does someone know why do I get the "file not accessible" error?
Thanks!
Put the file in the same directory as the rest of your applications files (i.e. where the app.yaml is).
All the files you want your application to access have to be in the same place, under your main application directory.
GAE can't access arbitrary files somewhere on your computer, just like it cannot access files saved "somewhere" else when deployed.
filename='myfile.doc'
That's assuming it's in the 'root' (same as app.yaml).
In your app.yaml file you need to make the file visible to your app:
handlers:
- url: /<location to your data files>
script: <your app name>.app
For example in my app.yaml file it looks like this
- url: /data
script: main.app

Categories

Resources