I am trying to set up a simple flask/socketio application however when I try and run it I get this error:
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask_socketio\__init__.py", line 45, in __call__
return super(_SocketIOMiddleware, self).__call__(environ,
return self.engineio_app.handle_request(environ, start_response)
return self.eio.handle_request(environ, start_response)
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 379, in handle_request
r = self._handle_connect(environ, start_response,
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 530, in _handle_connect
sid = self.generate_id()
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 504, in generate_id
secrets.token_bytes(12) + self.sequence_number.to_bytes(3, 'big'))
AttributeError: module 'secrets' has no attribute 'token_bytes'
and I can't figure out why. Any ideas?
Here is the code:
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'abc'
socketio = SocketIO(app)
#app.route('/')
def index():
return 'hello world'
if __name__ == '__main__':
socketio.run(app, debug=True)
Something like this happened to me while I was executing black (the code formatter), even without arguments:
C:\GIT\myprogram>c:\Python310-64\python.exe -m black
Traceback (most recent call last):
File "c:\Python310-64\lib\runpy.py", line 187, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "c:\Python310-64\lib\runpy.py", line 146, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "c:\Python310-64\lib\runpy.py", line 110, in _get_module_details
__import__(pkg_name)
File "src\black\__init__.py", line 45, in <module>
File "c:\Python310-64\lib\site-packages\black\files.py", line 34, in <module>
from black.handle_ipynb_magics import jupyter_dependencies_are_installed
File "src\black\handle_ipynb_magics.py", line 49, in <module>
AttributeError: module 'secrets' has no attribute 'token_hex'
I hade a file named secrets.py in my current directory, which triggered this issue.
Related
FlaskWebDir
helloapp
__init__.py
templates
hello.py
main.py
__ init __.py:
from flask import Flask
from helloapp import routes
app = Flask(__name__)
if __name__=='__main__':
app.run()
routes.py:
from flask import render_template
#app.route("/")
def hello():
### code
#app.route("/user/<username>/")
def hello_user(username):
### code
#app.route("/users/")
def display_users():
###code
main.py:
from helloapp import app
$ flask run
* Serving Flask app "main.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "/home/as/FlaskWebDir/projenv/bin/flask", line 8, in <module>
sys.exit(main())
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/flask/cli.py", line 967, in main
cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/click/decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/flask/cli.py", line 848, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/flask/cli.py", line 305, in __init__
self._load_unlocked()
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/flask/cli.py", line 330, in _load_unlocked
self._app = rv = self.loader()
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/flask/cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "/home/as/FlaskWebDir/projenv/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/as/FlaskWebDir/main.py", line 1, in <module>
from helloapp import app
File "/home/as/FlaskWebDir/helloapp/__init__.py", line 2, in <module>
from helloapp import routes
File "/home/as/FlaskWebDir/helloapp/routes.py", line 5, in <module>
#app.route("/")
NameError: name 'app' is not defined
In your routes, add:
# Previous import
from helloapp import app
#app.route('/')
def hello():
# ...
# ...
The app you are importing here is used in your decorators.
This is my code.
from flask import Flask
app=Flask(__name__)
#app.route('/')
def home():
return "WebApp"
if __name__=="__main__":
app.run(debug=True)
And this is the error I encounter.
Traceback (most recent call last):
File "F:/Python Projects/webapp1.py", line 7, in <module>
app.run(debug=True)
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 938, in run
cli.show_server_banner(self.env, self.debug, self.name, False)
File "C:\Program Files\Python36\lib\site-packages\flask\cli.py", line 629, in show_server_banner
click.echo(message)
File "C:\Program Files\Python36\lib\site-packages\click\utils.py", line 218, in echo
file = _default_text_stdout()
File "C:\Program Files\Python36\lib\site-packages\click\_compat.py", line 675, in func
rv = wrapper_func()
File "C:\Program Files\Python36\lib\site-packages\click\_compat.py", line 436, in get_text_stdout
rv = _get_windows_console_stream(sys.stdout, encoding, errors)
File "C:\Program Files\Python36\lib\site-packages\click\_winconsole.py", line 295, in _get_windows_console_stream
func = _stream_factories.get(f.fileno())
io.UnsupportedOperation: fileno
I am new to Flask and currently have very little knowledge about it. Thanks for answering!
It's a bug of the click package issue 1021.
You can avoid it by running in the shell instead of in python idle.
error in odoo 10 when i try to write basic controller for web
from odoo import http
show error
2017-07-09 13:20:10,128 9183 ERROR ? werkzeug: Error on request:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 177, in run_wsgi
execute(self.server.app)
File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 165, in execute
application_iter = app(environ, start_response)
File "/odoo/odoo-server/odoo/service/server.py", line 250, in app
return self.app(e, s)
File "/odoo/odoo-server/odoo/service/wsgi_server.py", line 184, in application
return application_unproxied(environ, start_response)
File "/odoo/odoo-server/odoo/service/wsgi_server.py", line 170, in application_unproxied
result = handler(environ, start_response)
File "/odoo/odoo-server/odoo/http.py", line 1307, in __call__
self.load_addons()
File "/odoo/odoo-server/odoo/http.py", line 1328, in load_addons
m = __import__('odoo.addons.' + module)
File "/odoo/odoo-server/odoo/modules/module.py", line 81, in load_module
execfile(modfile, new_mod.__dict__)
File "/odoo/odoo-server/addons/web_printscreen_zb/__init__.py", line 25, in <module>
import controllers
File "/odoo/odoo-server/addons/web_printscreen_zb/controllers.py", line 28, in <module>
import openerp.addons.web.http as openerpweb
File "/odoo/odoo-server/odoo/modules/module.py", line 109, in load_module
mod = importlib.import_module(canonical)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named http
i read the documentation of odoo to write web controller but same result.
and
i have tried to
from odoo.addons.web import http
same result
the problem has been solved
by move the code from controller.py to another file file.py
i don't know why this ..!
Try the below
import odoo.http as http
I have problem running flask run for OpenTok server code. How can I eradicate the error? Thanks in advance.
This is the error:
(opentokenv) ➜ opentok-server python -m flask run
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Library/Python/2.7/site-packages/flask/__main__.py", line 15, in <module>
main(as_module=True)
File "/Library/Python/2.7/site-packages/flask/cli.py", line 513, in main
cli.main(args=args, prog_name=name)
File "/Library/Python/2.7/site-packages/flask/cli.py", line 380, in main
return AppGroup.main(self, *args, **kwargs)
File "/Library/Python/2.7/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/Library/Python/2.7/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Library/Python/2.7/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Library/Python/2.7/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/Library/Python/2.7/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "/Library/Python/2.7/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/Library/Python/2.7/site-packages/flask/cli.py", line 423, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/Library/Python/2.7/site-packages/flask/cli.py", line 152, in __init__
self._load_unlocked()
File "/Library/Python/2.7/site-packages/flask/cli.py", line 176, in _load_unlocked
self._app = rv = self.loader()
File "/Library/Python/2.7/site-packages/flask/cli.py", line 237, in load_app
rv = locate_app(self.app_import_path)
File "/Library/Python/2.7/site-packages/flask/cli.py", line 90, in locate_app
__import__(module)
File "/Users/azambaderi/Documents/Web_Projects/opentok-server/opentok.py", line 2, in <module>
from opentok import OpenTok
ImportError: cannot import name OpenTok
This is the Python code:
from flask import Flask, render_template
from opentok import OpenTok
import os
try:
api_key = os.environ['API_KEY']
api_secret = os.environ['API_SECRET']
except Exception:
raise Exception('You must define API_KEY and API_SECRET environment variables')
app = Flask(__name__)
opentok = OpenTok(api_key, api_secret)
session = opentok.create_session()
#app.route("/")
def hello():
key = api_key
session_id = session.session_id
token = opentok.generate_token(session_id)
return render_template('index.html', api_key=key, session_id=session_id, token=token)
if __name__ == "__main__":
app.debug = True
app.run()
I tried your code and it worked for me, until I changed the filename to opentok.py (from the stack trace it appears that is the name you are using).
I think the quickest solution here is to change your filename from opentok.py to something else as it conflicting with the library name. (Don't forget to remove the opentok.pyc file too).
If you wish to keep the name you should see this answer about Absolute and Relative imports
I followed the tutorial for Werkzeug "Shortly" here
And I get this error message after submitting a valid url.
Traceback (most recent call last)
File "/home/sadik/NLM/shortly/shortly.py", line 87, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/wsgi.py", line 579, in __call__
return self.app(environ, start_response)
File "/home/sadik/NLM/shortly/shortly.py", line 83, in wsgi_app
response = self.dispatch_request(request)
File "/home/sadik/NLM/shortly/shortly.py", line 33, in dispatch_request
return getattr(self, 'on_' + endpoint)(request, **values)
File "/home/sadik/NLM/shortly/shortly.py", line 45, in on_new_url
short_id = self.insert_url(url)
File "/home/sadik/NLM/shortly/shortly.py", line 72, in insert_url
short_id = self.redis.get('reverse-url:' + url)
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/client.py", line 705, in get
return self.execute_command('GET', name)
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/client.py", line 464, in execute_command
connection.send_command(*args)
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/connection.py", line 334, in send_command
self.send_packed_command(self.pack_command(*args))
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/connection.py", line 316, in send_packed_command
self.connect()
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/connection.py", line 253, in connect
raise ConnectionError(self._error_message(e))
ConnectionError: Error 111 connecting localhost:6379. Connection refused.
The error message indicates that there is something wrong with localhost:6379
The relevant part of code is here:
def create_app(redis_host='localhost', redis_port=6379, with_static=True):
app = Shortly({
'redis_host': redis_host,
'redis_port': redis_port
})
if with_static:
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/static': os.path.join(os.path.dirname(__file__), 'static')
})
return app
if __name__ == '__main__':
from werkzeug.serving import run_simple
app = create_app()
run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)
That means of course that the server is running on localhost:5000. So why is there another port number in the create_app function? That confuses me a bit.
I'm not familiar with shortly or werkzeug but it looks like you're missing the redis server, install one using your favourite package manager and try again.