GAE/Python ImportError only on the deployed version - python

Hi everyone I'm struggling with AppEngine, I made my webapp on the development server, everything worked fine but once I deploy it gives me an ImportError.
My main directory is as follows:
-/
-Several
-Folders
-...
*admin.py
*app.yaml
*db_objects.py
*index.yaml
*img_getter.py
*keys.py
*main.py
*main_handler.py
My app.yaml file:
application: myapplication (this is not the real name)
version: 1
runtime: python27
api_version: 1
threadsafe: yes
default_expiration: "7d"
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /css
static_dir: css
- url: /img
static_dir: img
- url: /images
static_dir: images
- url: /js
static_dir: js
- url: /admin.*
script: admin.app
login: admin
- url: /checkout.*
script: main.app
login: required
- url: /confirm.*
script: main.app
login: required
- url: /changeinfo.*
script: main.app
login: required
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
When I go to myaddress.appengine.com/admin it gives me this error:
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~myapplication/1.383371027775991819/admin.py", line 7, in <module>
import img_getter
ImportError: No module named img_getter
This is the beginning of admin.py:
#coding=utf-8
import webapp2
from main_handler import Handler
import db_objects
from google.appengine.ext import db
import img_getter
from google.appengine.api import memcache
import keys
from datetime import date
class MainHandler(Handler):
def get(self):
self.redirect("/admin/noticias")
...
And this is the beginning of the img_getter.py file:
import gdata.photos.service
import gdata.media
import gdata.geo
def foo(variable):
...
def bar(variable):
...
#functions to get images from the google picassa service
The thing is I've been developing this webapp on the developer server and it's been working like a charm, I've even cleared the datastore and tried on several computers and it's still working, but when I try to test it on Google servers I'm finding this error.
I haven't define any reference on the app.yaml file as I think it's not necesary because it's not a library depending from google python API, am I wrong?
Does anybody know what the problem is?
Thank you very much :)

You might need to import it as a package.
https://docs.python.org/2/tutorial/modules.html#packages
Create a folder called 'test' (or whatever you want) for img_getter.py and put it in there
Add a file called __init__.py and save it in the folder you put img getting in. This file can be empty
call the import as
import test.img_getter
Reference
How to import python script files in folders on Google App Engine?
Cheers and good luck!

Related

Google App Engine - Endpoints Failure

I'm trying to deploy a gae project with endpoints but it's giving me errors that's should be happening.
I Don't Think there is anything wrong with my code but i keep getting this error when trying to deploy:
There is a /_ah/spi handler configured, but no Google Cloud Endpoints service was found in the app
my app.yaml :
application: app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
# --- Handlers
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
secure: always
- url: /js
static_dir: static/js
- url: /img
static_dir: static/img
- url: /css
static_dir: static/bootstrap/css
- url: /fonts
static_dir: static/fonts
- url: /partials
static_dir: static/partials
- url: /_ah/spi/.*
script: game.api
secure: always
# --- Libraries
libraries:
- name: webapp2
version: "2.5.2"
- name: pycrypto
version: latest
- name: endpoints
version: latest
- name: jinja2
version: latest
and my game.py :
from datetime import datetime
import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
from google.appengine.api import memcache
from google.appengine.api import taskqueue
from google.appengine.ext import ndb
# --- Setup Code
WEB_CLIENT_ID = 'app-id'
# --- Classes
class Hello(messages.Message):
"""String that stores a message."""
greeting = messages.StringField(1)
class TestOne(messages.Message):
''' TestOne message '''
msg = messages.StringField(1)
# --- API & Endpoints
#endpoints.api(name='whoamigame', version='v1')
class WhoAmIGameApi(remote.Service):
#endpoints.method(message_types.VoidMessage, TestOne, path='testone', http_method='GET', name='testonemethod')
def testOneM(self, req):
testOneMsgReturn = TestOne(msg='Test One...')
return testOneMsgReturn
# --- API
api = endpoints.api_server([WhoAmIGameApi])
Every time i try to deploy it, the endpoints fail. i'm not sure how to fix this issue. the code and syntax is perfect to me. everything is defined. the sdk fails every time.

Google App Engine Task Queue error

I'm developing a matchmaking system with gae and python. I was finding a consistent system for automatic matchmaking, and I found the task queue. I implemented a cron job to run every 10 minutes to add a queue. However I'm getting the following error:
When running /queue_generator:
Traceback (most recent call last):
File"/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
TypeError: 'module' object is not callable
The code that I have is:
(SessionID is just a db model)
queue_generator.py
import webapp2, time
from main import SessionID
from google.appengine.api import taskqueue
from google.appengine.ext import db
class Game(db.Model):
Users = db.ListProperty(str)
Score = db.IntegerProperty()
Turn = db.StringProperty()
class MainHandler(webapp2.RequestHandler):
def get(self):
taskqueue.add(url='/matchcreator',params={"id":str(time.time())})
class Gamegenerator(webapp2.RequestHandler):
def get(self):
while True:
q = Queue.get()
if len(q.queue) >= 4:
sids = []
for i in range(0,3):
sids.append(q.queue[i])
q.queue.remove(i)
q.put()
return self.response.set_status(204)
def post(self):
while True:
q = Queue.get()
if len(q.queue) >= 4:
sids = []
for i in range(0,3):
sids.append(q.queue[i])
q.queue.remove(i)
q.put()
return self.response.set_status(204)
app = webapp2.WSGIApplication([
('/queue_generator', MainHandler),
("/matchcreator",Gamegenerator)
], debug=True)
Why is this error occuring?
Edit
app.yaml
application: brobbinsgame
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /css
static_dir: css
- url: /home.css
static_files: style.css
upload: style.css
application_readable: true
- url: /register
script: register.app
- url: /logout
script: logout.app
- url: /line
script: line.app
- url: /queue_generator
script: queue_generator
login: admin
- url: /home
script: home.app
- url: /resetsid
script: resetsid.app
login: admin
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: pycrypto
version: "latest"
For every path except "/queue_generator, you have correctly referenced the app object. But for that one path, you reference the module directly. You need to define it in exactly the same way:
- url: /queue_generator
script: queue_generator.app
login: admin
Also notice that there is no way to get to "/matchcreator": "/queue_generator" is the only URL that will be routed to that file. You need to either expose "/matchcreator" in app.yaml as well, or do the more usual thing which is to route all the paths to a main app which imports all the handlers and defines the specific routing there.
This error is occurring because you are calling a Module instead of a Class.. the call should be : Module.Class.Method()

website to deploy in appEngine not showing

Hello: so I am trying to deploy a website to appEngine when I run it with the launcher the log does not have any errors, however when I go to local host the page is blank. I was running this website in python2.5 before with no issues, can you see any errors?
App.yaml
application: myappname
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /(.*\.(gif|png|json|jpg|ico|js|css))
static_files: \1
upload: (.*\.(gif|png|json|jpg|ico|js|css))
- url: .*
script: main.app
- url: /(.*\.json)
mime_type: application/json
static_files: static/\1
upload: static/(.*\.json)
libraries:
- name: webapp2
version: latest
main.py
import webapp2
import os
from google.appengine.ext.webapp2 import util
from google.appengine.ext.webapp2 import template
class MainPage(webapp2.RequestHandler):
def get (self, q):
if q is None:
q = 'index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))
app = webapp2.WSGIApplication([('/(.*html)?', MainPage)])
Change the 3rd and 4th lines (you can combine them) to:
from google.appengine.ext.webapp import util, template
That is, webapp, not webapp2.

Getting broken link error whle Using App Engine service accounts

I'm following this tutorial
https://developers.google.com/bigquery/docs/authorization#service-accounts-appengine
Here is my main.py code
import httplib2
from apiclient.discovery import build
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from oauth2client.appengine import AppAssertionCredentials
# BigQuery API Settings
SCOPE = 'https://www.googleapis.com/auth/bigquery'
PROJECT_NUMBER = 'XXXXXXXXXX' # REPLACE WITH YOUR Project ID
# Create a new API service for interacting with BigQuery
credentials = AppAssertionCredentials(scope=SCOPE)
http = credentials.authorize(httplib2.Http())
bigquery_service = build('bigquery', 'v2', http=http)
class ListDatasets(webapp.RequestHandler):
def get(self):
datasets = bigquery_service.datasets()
listReply = datasets.list(projectId=PROJECT_NUMBER).execute()
self.response.out.write('Dataset list:')
self.response.out.write(listReply)
application = webapp.WSGIApplication(
[('/listdatasets(.*)', ListDatasets)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Here is my app.yaml file code
application: bigquerymashup
version: 1
runtime: python
api_version: 1
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.py
And yes i have added app engine service account name in google api console Team tab with can edit permissions.
When upload the app and try to access the link it says
Oops! This link appears to be broken.
Ealier i ran this locally and tried to access it using link localhost:8080.Then i thought may be running locally might be giving the error so i uploaded my code to
http://bigquerymashup.appspot.com/
but still its giving error.
EDIT:
Updated App.yaml
application: bigquerymashup
version: 1
runtime: python
api_version: 1
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.py
- url: /listdatasets
script: main.py
But getting another error
Traceback (most recent call last): File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp2‌​5.py", line 710, in call handler.get(*groups) TypeError: get() takes exactly 1 argument (2 given)
You need to define a Script Handlers that matches the URL you are trying to locate.
Try: http://[your_app_id_here].appspot.com/listdatasets
Read more about handlers here.

How to migrate my app.yaml to 2.7?

I'm migrating my gae app to python 2.7. This is my new app.yaml:
application: webfaze
version: main
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /mapreduce(/.*)?
script: mapreduce/main.application
- url: /(.*\.(html|css|js|gif|jpg|png|ico|swf))
static_files: static/\1
upload: static/.*
expiration: "1d"
- url: .*
script: main.application
- url: /task/.*
script: main.application
login: admin
But I get this error message:
Error parsing yaml file:
Invalid object:
threadsafe cannot be enabled with CGI handler: mapreduce/main.application
in "webfaze/app.yaml", line 22, column 1
Can you tell me how to resolve the error?
Checking the source code, it looks that you need to define your handlers' path without any slash:
if (handler.script and (handler.script.endswith('.py') or
'/' in handler.script)):
raise appinfo_errors.ThreadsafeWithCgiHandler(
'threadsafe cannot be enabled with CGI handler: %s' %
handler.script)
Move application.py to the root of your project and modify the handler's path accordingly.
Change:
- url: /mapreduce(/.*)?
script: mapreduce/main.application
To:
- url: /mapreduce(/.*)?
script: mapreduce.main.application
You may also need to add an __init__.py to the 'mapreduce' folder if one doesn't exist there already. That will make the python interpret the folder as a module.

Categories

Resources