python annoy library import error in GAE project - python

I am working on GAE using python jinja2 framework, while importing third party library "annoy" I am getting import error.
I am following the GAE Installing a third-party library method.
My project structure like
*projectfolder/
*lib/
*annoy/
main.py
app.yaml
appengine_config.py
Here is my main.py code
import os
import jinja2
from google.appengine.api import users
import webapp2
import csv
from jinja2 import Template
import ann_utils
JINJA_ENVIRONMENT = jinja2.Environment(
# TODO: to add other directories here that contains the templates.
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class Sent2Vec(webapp2.RequestHandler):
"""docstring for ."""
def get(self):
template = JINJA_ENVIRONMENT.get_template('sent2vec.html')
self.response.headers['Content-Type'] = 'text/html'
self.response.write(template.render()
ann_utils.py library
from annoy import AnnoyIndex
import random
f = 40
t = AnnoyIndex(f)
for i in xrange(1000):
v = [random.gauss(0, 1) for z in xrange(f)]
t.add_item(i, v)
t.build(10)
t.save('test.ann')
u = AnnoyIndex(f)
u.load('test.ann')
print(u.get_nns_by_item(0, 1000))
app.yaml
runtime: python27
threadsafe: ture
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
- name: annoy
version: latest
handlers:
- url: /.*
script: main.app
appengine_config.py
from google.appengine.ext import vendor
vendor.add('lib')
Here is the tracetrack:
INFO 2017-01-24 08:33:27,378 devappserver2.py:769] Skipping SDK update check.
INFO 2017-01-24 08:33:27,435 api_server.py:205] Starting API server at: http://localhost:60106
INFO 2017-01-24 08:33:27,451 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO 2017-01-24 08:33:27,451 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR 2017-01-24 08:34:53,286 wsgi.py:263]
Traceback (most recent call last):
File "/home/affine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/affine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/home/affine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/home/affine/ML-Visualization/main.py", line 8, in <module>
from ann_utils import create_tree
File "/home/affine/ML-Visualization/ann_utils.py", line 1, in <module>
from annoy import AnnoyIndex
File "/home/affine/ML-Visualization/lib/annoy/__init__.py", line 15, in <module>
from annoylib import *
File "/home/affine/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 963, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named annoy.annoylib
INFO 2017-01-24 08:34:53,290 module.py:788] default: "GET / HTTP/1.1" 500 -

Related

Python Flask - Session Not Being Set with Waitress

I am using waitress on IIS with Flask.
I have a route that sets a session as follows
#gp_clinicals_bp.route("/setting", methods=["POST","GET"])
def gp_clinicals():
session["gp_clinicals_meds_repeat"] = "123456"
return session["gp_clinicals_meds_repeat"]
This returns, unsurprisingly:
123456
I have another route as follows:
#gp_clinicals_bp.route("/testing", methods=["POST","GET"])
def gp_clinicals_test():
return session["gp_clinicals_meds_repeat"]
Now, I get nothing back and the following in the error log
2022-08-02 20:03:51,126 - ERROR - app.py - log_exception - 1455 - Exception on /gp-clinicals/medication-repeat [GET]
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\flask\app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python310\lib\site-packages\flask\app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python310\lib\site-packages\flask\app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python310\lib\site-packages\flask\app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "E:\Apps\dev\digital\gp_clinicals\gp_clinicals.py", line 158, in gp_clinicals_medication_repeat
return str(session["gp_clinicals_meds_repeat"])
KeyError: 'gp_clinicals_meds_repeat'
2022-08-02 20:03:51,128 - ERROR - __init__.py - handle_error_500 - 92 - 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
I'm the only one on the server. Below is a snippet from my initilisation file:
"""Initialize Flask app."""
import logging
import os
from logging.handlers import RotatingFileHandler
from flask import Flask, render_template, flash,request
from flask_session import Session
from flask_assets import Environment
from config import Config
if Config.FLASK_ENV == "development" and Config.DD_SERVICE:
patch_all()
sess = Session()
def init_app():
"""Create Flask application."""
app = Flask(__name__, instance_relative_config=False)
app.config["CACHE_TYPE"] = "null"
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config["SESSION_TYPE"] = "filesystem"
app.config.from_object("config.Config")
assets = Environment()
assets.init_app(app)
sess.init_app(app)
What's going wrong? In the flask_session folder I am seeing files being created
Any advice? I tried using single apostophes instead of quotes but no go there either
The site in IIS is running through a reverse proxy in IIS - could that be doing it?

python-logstash error thrown while running locally

If I import logstash, running locally I get the following error
Connected to pydev debugger (build 162.1812.1)
/home/vagrant/.envs/emailservice/lib/python3.4/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.cache is deprecated, use flask_cache instead.
.format(x=modname), ExtDeprecationWarning
Traceback (most recent call last):
File "/home/vagrant/.pycharm_helpers/pydev/pydevd.py", line 1580, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/home/vagrant/.pycharm_helpers/pydev/pydevd.py", line 964, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/home/vagrant/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/emailService/app.py", line 59, in <module>
import logstash
File "/home/vagrant/.envs/emailservice/lib/python3.4/site-packages/logstash/__init__.py", line 2, in <module>
from event import Event
ImportError: No module named 'event'
Process finished with exit code 1
my app.py file looks mostly like this. I run it locally through a vagrant session. If I remove the import logstash from the local branch of the if, the application starts up fine and I get local console log output.
import logging
import os
import sys
from flask import Flask
from flask_restful import Api
from flask_cache import Cache
from flask_sqlalchemy import SQLAlchemy
from opbeat.contrib.flask import Opbeat
from tasks import make_celery
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'SUCHSECRETSWOW')
app.config.from_object(os.environ.get('APP_SETTINGS', 'config.DevelopmentConfig'))
cache = Cache(app)
db = SQLAlchemy(app)
api = Api(app)
celery = make_celery(app)
if len(app.config['OPBEAT_ORGANIZATION_ID']):
opbeat = Opbeat(
app,
organization_id=app.config['OPBEAT_ORGANIZATION_ID'],
app_id=app.config['OPBEAT_APP_ID'],
secret_token=app.config['OPBEAT_SECRET_TOKEN'],
)
#app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
def clear_cache():
cache.clear()
def start_resources():
from emailService.api import HealthApi
api.add_resource(HealthApi, '/health')
def start_tasks():
from emailService.tasks import KickoffFetchEmailPeriodTask
if __name__ == '__main__':
if app.config.get('DEVELOPMENT', False):
#The reason this exists is purely because of my error.
import logstash
app.logger.setLevel(logging.DEBUG)
app.logger.addHandler(logging.StreamHandler())
else:
import logstash
app.logger = logging.getLogger('python-logstash-logger')
app.logger.setLevel(logging.INFO)
app.logger.addHandler(logstash.LogstashHandler('myhost.veryhost.suchhost', 5959, version=1))
app.logger.addHandler(logging.StreamHandler())
clear_cache()
start_tasks()
start_resources()
app.logger.debug('Starting app')
app.run(host='0.0.0.0', port=16600, debug=True, use_reloader=False)
All of the google searches result in a great big fat sum total of nothing helpful.
You're probably running into this issue, you have pip installed logstash instead of python-logstash
Run this and it should work afterwards:
> pip uninstall logstash
> pip install python-logstash

GAE Simple Request Handler only run once

Good day!
https://developers.google.com/appengine/docs/python/gettingstarted/helloworld
this is the hello world that I'm trying to run.
I can seeing the
Hello, world!
Status: 500
message. however it will be turned to a "HTTP Error 500" after I hit the refresh.
and... it seems that the appengine only shows me the good result once after I re-save either app.yaml or helloworld.py
This is the trace for the good result
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 187, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 239, in _LoadHandler
raise ImportError('%s has no attribute %s' % (handler, name))
ImportError: <module 'helloworld' from 'D:\work\[GAE] tests\helloworld\helloworld.pyc'> has no attribute app
INFO 2012-06-23 01:47:28,522 dev_appserver.py:2891] "GET /hello HTTP/1.1" 200 -
ERROR 2012-06-23 01:47:30,040 wsgi.py:189]
and this is the trace for the Error 500
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 187, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "C:\Program Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line 239, in _LoadHandler
raise ImportError('%s has no attribute %s' % (handler, name))
ImportError: <module 'helloworld' from 'D:\work\[GAE] tests\helloworld\helloworld.pyc'> has no attribute app
INFO 2012-06-23 01:47:30,127 dev_appserver.py:2891] "GET /hello HTTP/1.1" 500 -
here's my helloworld.py
print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
my main.py. (app is used instead of application)
import webapp2
class hello(webapp2.RequestHandler):
def get(self):
self.response.out.write('normal hello')
app = webapp2.WSGIApplication([
('/', hello),
], debug = True)
and the app.yaml
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /hello
script: helloworld.app
- url: /.*
script: main.app
libraries:
- name: webapp2
version: "2.5.1"
any clue what's causing this?
Regards,
You aren't creating a helloworld.app object in your helloworld.py module.
See the lines
app = webapp2.WSGIApplications([...
in your main.py file? That creates the main.app object that's referenced by the script: main.app handler in your app.yaml.
You're referencing a helloworld.app object a couple of lines above; that object doesn't exist. Python 2.7 in App Engine doesn't support the simple module model -- no WSGI handler, just a simple script -- that's used in the 2.5 "Hello World" demo.
As presveva said, use the 2.7 Getting Started guide for less confusion.

Python. Error with remote_api, google app engine [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Following this guide:
http://code.google.com/appengine/articles/remote_api.html
I edited my app.yaml, updated the app, and tried to connect using api_remote. Then I got this error. Why?
c:\python25\python "C:\Program Files
\Google\google_appengine\remote_api_shell.py" -s medmcqs.appspot.com
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\remote_api_shell.py", line 99,
in <module>
run_file(__file__, globals())
File "C:\Program Files\Google\google_appengine\remote_api_shell.py", line 95,
in run_file
execfile(script_path, globals_)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\remote_a
pi_shell.py", line 140, in <module>
main(sys.argv)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\remote_a
pi_shell.py", line 136, in main
appengine_rpc.HttpRpcServer)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\remote_a
pi_shell.py", line 76, in remote_api_shell
rpc_server_factory=rpc_server_factory)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\remote_api
\remote_api_stub.py", line 645, in ConfigureRemoteApi
app_id = GetRemoteAppIdFromServer(server, path, rtok)
File "C:\Program Files\Google\google_appengine\google\appengine\ext\remote_api
\remote_api_stub.py", line 501, in GetRemoteAppIdFromServer
'Invalid response recieved from server: %s' % response)
google.appengine.ext.remote_api.remote_api_stub.ConfigurationError: Invalid resp
onse recieved from server: <!DOCTYPE html PUBLIC "-//W3C//
This is what my app.yaml looks like...
application: medmcqs
version: 2
runtime: python
api_version: 1
inbound_services:
- mail
handlers:
- url: /static
static_dir: static
- url: /options
script: main.py
- url: /send
script: main.py
login: required
- url: /history
script: main.py
login: required
- url: /suggest
script: main.py
login: required
- url: .*
script: main.py
builtins:
- remote_api: on
UPDATE: Here are the log details:
2012-01-18 01:40:33.659 /_ah/remote_api?rtok=252529974978 200 48ms 1kb Google-remote_api/1.0 win32/6.1.7601.2 Python/2.5.4.final.0
109.224.29.101 - - [18/Jan/2012:01:40:33 -0800] "GET /_ah/remote_api?rtok=252529974978 HTTP/1.1" 200 1605 - "Google-remote_api/1.0 win32/6.1.7601.2 Python/2.5.4.final.0" "medmcqs.appspot.com" ms=49 cpu_ms=23 api_cpu_ms=0 cpm_usd=0.000855 instance=00c61b117cf891196f8b484d5951471ac52ac0
Some suggestions:
modify the .* handler
from :
-url: .*
script: main.py
To:
-url: /.*
script: main.py
Or: Maybe you can manually add the handler and try again to see if there are any differences.
- url: /_ah/remote_api
script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
login: admin
EDIT1
There is another version of remote console code that you may want to try try. It is also from google app engine official website.
#remote_console.py
#!/usr/bin/python
import code
import getpass
import sys
sys.path.append(r"C:\Program Files\Google\google_appengine")
sys.path.append(r"C:\Program Files\Google\google_appengine\lib\yaml\lib")
sys.path.append(r"C:\Program Files\Google\google_appengine\lib\fancy_urllib")
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.ext import db
def auth_func():
return raw_input('Username:'), getpass.getpass('Password:')
if len(sys.argv) < 2:
print "Usage: %s app_id [host]" % (sys.argv[0],)
app_id = sys.argv[1]
if len(sys.argv) > 2:
host = sys.argv[2]
else:
host = '%s.appspot.com' % app_id
remote_api_stub.ConfigureRemoteDatastore(app_id, '/remote_api', auth_func, host)
code.interact('App Engine interactive console for %s' % (app_id,), None, locals())
Usage:
remote_console.py YOUR_APP_ID YOUR_APP_ID.appspot.com
or
remote_console.py s~YOUR_APP_ID YOUR_APP_ID.appspot.com
For some reason, some app id will begin with s~.

Workflow for configuring apache on a webfaction account via ssh and ftp. (django/python)

I'm new at this, however, when it comes to configuring mod_python/apache or wsgi/apache I suffer.
I've been able to use the python debugger tool.. pdb.set_trace() to success, especially when using the django development server, i.e. it out puts to the terminal all of the server activity, including the pdb interface.
So, how does one do something like this when trying to deploy a django website on an host such as webfaction?
Other than ftp into the error_log and read about it post failure, be able to interact with the system, as its happening?
Hopefully I'm clear enough here.
Btw, following is the file that I'm trying to configure.
import os
import sys
from os.path import abspath, dirname, join
from site import addsitedir
from django.core.handlers.modpython import ModPythonHandler
import pdb
class PinaxModPythonHandler(ModPythonHandler):
def __call__(self, req):
# mod_python fakes the environ, and thus doesn't process SetEnv.
# This fixes that. Django will call this again since there is no way
# of overriding __call__ to just process the request.
os.environ.update(req.subprocess_env)
from django.conf import settings
sys.path.insert(0, abspath(join(dirname(__file__), "../../")))
sys.path.insert(0, os.path.join(settings.PINAX_ROOT, "apps/external_apps"))
sys.path.insert(0, os.path.join(settings.PINAX_ROOT, "apps/local_apps"))
sys.path.insert(0, join(settings.PINAX_ROOT, "apps"))
sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))
pdb.set_trace()
return super(PinaxModPythonHandler, self).__call__(req)
def handler(req):
# mod_python hooks into this function.
return PinaxModPythonHandler()(req)
and here's the resulting error page via http:
MOD_PYTHON ERROR
ProcessId: 318
Interpreter: 'web25.webfaction.com'
ServerName: 'web25.webfaction.com'
DocumentRoot: '/etc/httpd/htdocs'
URI: '/'
Location: '/'
Directory: None
Filename: '/etc/httpd/htdocs'
PathInfo: '/'
Phase: 'PythonHandler'
Handler: 'bc.deploy.modpython'
Traceback (most recent call last):
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)
File "/home/dalidada/webapps/birthconfidence/bc/deploy/modpython.py", line 33, in handler
return PinaxModPythonHandler()(req)
File "/home/dalidada/webapps/birthconfidence/bc/deploy/modpython.py", line 29, in __call__
return super(PinaxModPythonHandler, self).__call__(req)
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/django/core/handlers/modpython.py", line 191, in __call__
self.load_middleware()
File "/home/dalidada/webapps/birthconfidence/lib/python2.5/django/core/handlers/base.py", line 40, in load_middleware
raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
ImproperlyConfigured: Error importing middleware django_openid.consumer: "No module named django_openid.consumer"
How to use pdb with mod_wsgi is documented on the mod_wsgi site. See:
http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Python_Interactive_Debugger
Other debugging techniques are shown on the same page.

Categories