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
Related
I am using a Flask application run by gunicorn which works fine if I run it directly on my server but fails if I run it inside Docker with the error
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib64/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/opt/app-root/src/backend.py", line 242, in runAlertManager
db = get_db()
File "/opt/app-root/src/backend.py", line 29, in get_db
db = g._database = sqlite3.connect(DATABASE)
sqlite3.OperationalError: unable to open database file
If I get a cli inside the docker container I can use the sqlite3 client just fine on that file. If I remove the Threading part it also works fine just not sure why it does work directly but not in Docker
Simplified I have something like this
from threading import Thread
from time import sleep
from flask import g, Flask, request, jsonify, current_app
app = Flask(__name__)
def get_db():
db = getattr(g, '_database', None)
if db is None:
print(DATABASE)
db = g._database = sqlite3.connect(DATABASE)
return db
def runAlertManager(app):
'''
Runs AlertManager in a separate process
'''
with app:
db = get_db()
while True:
#do something
sleep(10)
x = Thread(target=runAlertManager, args=(app.app_context(), ))
x.start()
just the path pointing to /db/database.db which is mounted into the container and exists.
The entire directory an SQLite database exists in needs to be writable; SQLite needs to create some sidecar files (e.g. for database.db, database.db-wal or database.db-journal).
Instead of mounting just the file, mount the directory it's in.
I was trying to build flask-web API. When I am trying to run the app.py code, I have got this error, please refer the code which I have mentioned below. Do I have to install some package to get 'package' into 'main'?
I tried to run in different platforms such jupyter notebook, spyder and Google Colab too, but it didn't work, and getting similar issues.
Here is my app.py code whic is giving error:
from flask import Flask, request, jsonify, render_template
import pickle
app = Flask(__name__)
model = pickle.load(open('model.pkl', 'rb'))
#app.route('/')
def home():
return render_template('index.html')
#app.route('/predict',methods=['POST'])
def predict():
'''
For rendering results on HTML GUI
'''
int_features = [int(x) for x in request.form.values()]
final_features = [np.array(int_features)]
prediction = model.predict(final_features)
output = round(prediction[0], 2)
return render_template('index.html', prediction_text='Employee Salary should be $ {}'.format(output))
#app.route('/predict_api',methods=['POST'])
def predict_api():
'''
For direct API calls trought request
'''
data = request.get_json(force=True)
prediction = model.predict([np.array(list(data.values()))])
output = prediction[0]
return jsonify(output)
if __name__ == "__main__":
app.run(debug=True)
Here is the error message I have got:
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
Traceback (most recent call last):
File "<ipython-input-21-3add8626fce0>", line 36, in <module>
app.run(debug=True)
File "C:\Users\ishwo\Anaconda3\lib\site-packages\flask\app.py", line 990, in run
run_simple(host, port, self, **options)
File "C:\Users\ishwo\Anaconda3\lib\site-packages\werkzeug\serving.py", line 1007, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "C:\Users\ishwo\Anaconda3\lib\site-packages\werkzeug\_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "C:\Users\ishwo\Anaconda3\lib\site-packages\werkzeug\_reloader.py", line 159, in restart_with_reloader
args = _get_args_for_reloading()
File "C:\Users\ishwo\Anaconda3\lib\site-packages\werkzeug\_reloader.py", line 76, in _get_args_for_reloading
if __main__.__package__ is None:
AttributeError: module '__main__' has no attribute '__package__'}
This question already has an answer here:
Quickstart Flask application failing for some reason
(1 answer)
Closed 6 years ago.
Edit: This question apparently has an answer here
Quickstart Flask application failing for some reason
As well as a (closed and apparently fixed) GitHub issue here:
https://github.com/pallets/werkzeug/issues/798
Unfortunately I'm still experiencing the problem, even with Werkzeug 0.12-dev (the latest code from GitHub).
I've just started developing a simple Flask application with Python 3.4 on Windows. Literally, at the moment, here are the only contents of fileserve.py.
import os.path
from flask import abort, Flask
base_directory = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config.from_pyfile(os.path.join(base_directory, 'fileserve.cfg'))
#app.route('/')
def index():
abort(403)
if __name__ == '__main__':
app.run(debug=True)
When I try running the application, I get the following traceback:
Traceback (most recent call last):
File "fileserve.py", line 22, in <module>
app.run(debug=True)
File "C:\Users\jscholes\dev\fileserve\env\lib\site-packages\flask\app.py", line 772, in run
run_simple(host, port, self, **options)
File "C:\Users\jscholes\dev\fileserve\env\lib\site-packages\werkzeug\serving.py", line 633, in run_simple
application = DebuggedApplication(application, use_evalex)
File "C:\Users\jscholes\dev\fileserve\env\lib\site-packages\werkzeug\debug\__init__.py", line 249, in __init__
if self.pin is None:
File "C:\Users\jscholes\dev\fileserve\env\lib\site-packages\werkzeug\debug\__init__.py", line 259, in _get_pin
self._pin, self._pin_cookie = get_pin_and_cookie_name(self.app)
File "C:\Users\jscholes\dev\fileserve\env\lib\site-packages\werkzeug\debug\__init__.py", line 169, in get_pin_and_cookie_name
h.update('cookiesalt')
TypeError: Unicode-objects must be encoded before hashing
This error occurs even with a simple "hello world" app:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello world'
app.run(debug=True)
Is Flask's support for Python 3 just not very good? Is there a workaround for this? Working without the option of debug mode is not a great prospect.
Upgrade your werkzeug installation.
In my install, with werkzeug.__version__ == 0.11.7, line 169 reads:
h.update(b'cookiesalt')
For the last few weeks I've been building a website mostly based on Miguel Grinberg's book 'Flask Web Development'. This is my 'manage.py' file for reference:
import os
from app import create_app, db
from app.models import User, Role, Permission, Post
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
Permission=Permission, Post=Post)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
And this is my app/__init.py file:
from flask import Flask #session, flash, url_for
from flask.ext.mail import Mail
from flask.ext.moment import Moment
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
from config import config
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'
mail = Mail()
moment = Moment()
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
login_manager.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
from main import main as main_blueprint
from .auth import auth as auth_blueprint
app.register_blueprint(main_blueprint)
app.register_blueprint(auth_blueprint, url_prefix = '/auth')
return app
As you can see it's a relatively bare-bones project, but it was going well until a few days ago when I accidentally deleted my migrations folder, which up until that point was working without perfectly fine. When I attempted to set up the migrations folder again, I got this error:
(website1)joshua#joshua-ThinkPad-Edge-E430 ~/website/website1 $ python manage.py db migrate
Traceback (most recent call last):
File "manage.py", line 29, in <module>
manager.run()
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
res = handle(*args, **config)
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_migrate/__init__.py", line 153, in migrate
config = _get_config(directory, opts=['autogenerate'])
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_migrate/__init__.py", line 51, in _get_config
config.set_main_option('script_location', directory)
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/alembic/config.py", line 201, in set_main_option
self.file_config.set(self.config_ini_section, name, value)
File "/home/joshua/anaconda/lib/python2.7/ConfigParser.py", line 753, in set
ConfigParser.set(self, section, option, value)
File "/home/joshua/anaconda/lib/python2.7/ConfigParser.py", line 396, in set
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'alembic'
I'm not really sure what to do about this, I've tried dropping then re-creating my database and running db migrate again but I got the same error as before. I also tried uninstalling and reinstalling alembic but with no luck.
I'm not really sure what do do at this point, up until now whenever I've had an issue with the tutorial I took the time to figure it out on my own since I want to understand how everything works, but I'm totally stumped on this one.
Courtesy Dauros: I neglected to run python manage.py db init.
As part of a project, I've been trying to port a Tornado server to work on the Google App Engine. Since the App Engine doesn't implement the asynchronous functions of normal Tornado, I've been trying to convert the main Application to a WSGIApplication. The normal main code works fine (forgive the imports and formatting, it's a mess from trying to follow other examples):
import wsgiref
import tornado.wsgi
import tornado.web
import tornado.httpserver
import os
import Handlers
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
def application():
handlers=[(r"/", Handlers.MainHandler),
(r"/Login", Handlers.LoginHandler),
(r"/ViewHistory",Handlers.ViewHistoryHandler),
(r"/UploadFile", Handlers.UploadHandler),
(r"/Index", Handlers.IndexHandler),
(r"/About", Handlers.AboutHandler),
(r"/Profile", Handlers.ProfileHandler)]
settings=dict(template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True)
return tornado.web.Application(handlers, **settings)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
I can access the web pages, browse the site, works great. If I change line 24 to return a tornado.wsgi.WSGIApplication, like thus:
import wsgiref
import tornado.wsgi
import tornado.web
import tornado.httpserver
import os
import Handlers
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
def application():
handlers=[(r"/", Handlers.MainHandler),
(r"/Login", Handlers.LoginHandler),
(r"/ViewHistory",Handlers.ViewHistoryHandler),
(r"/UploadFile", Handlers.UploadHandler),
(r"/Index", Handlers.IndexHandler),
(r"/About", Handlers.AboutHandler),
(r"/Profile", Handlers.ProfileHandler)]
settings=dict(template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True)
return tornado.wsgi.WSGIApplication(handlers, **settings)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
it also runs just fine. However, when I try to access any of the web pages, it gives me the following error:
[E 140131 10:02:18 iostream:357] Uncaught exception, closing connection.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/iostream.py", line 354, in wrapper
callback(*args)
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/httpserver.py", line 328, in _on_headers
self.request_callback(self._request)
TypeError: __call__() takes exactly 3 arguments (2 given)
[E 140131 10:02:18 ioloop:491] Exception in callback <functools.partial object at 0xf6e3ecfc>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/ioloop.py", line 477, in _run_callback
callback()
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/iostream.py", line 354, in wrapper
callback(*args)
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/httpserver.py", line 328, in _on_headers
self.request_callback(self._request)
TypeError: __call__() takes exactly 3 arguments (2 given)
Which I can't make heads or tails of, and Google hasn't turned up anyone with the same problem (except for the guy who recommended mucking around in the Tornado files, which won't help on the App Engine AFAIK). Has anyone else seen this error before, or can spot why the WSGIApplication crashes when the default Application does not?
The name "application" is unfortunately overloaded here - tornado.web.Application and WSGIApplication differ in their interface to the server. tornado.web.Application can be used by a tornado HTTPServer, but WSGIApplication must be run in a WSGI container. In app engine deployment, you'd just mention your WSGIApplication instance directly in the config file, with no mention of tornado.httpserver. To use adapt a WSGIApplication to tornado.httpserver, use a tornado.wsgi.WSGIContainer: HTTPServer(WSGIContainer(application()))