how to make run Flask in Pythonista iOS? - python

I am using Pythonista app on iOS and I cant make Flask module work even with the most basic code.
my code is :
from flask import Flask
Flaskapp = Flask(__name__)
#Flaskapp.route('/')
def helloWorld():
return 'Woala'
Flaskapp.run(debug=True)
but keep receiving:
* Serving Flask app "app" (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
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/app.py", line 9, in <module>
Flaskapp.run(debug=True)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/flask/app.py", line 990, in run
run_simple(host, port, self, **options)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/serving.py", line 1007, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/_reloader.py", line 176, in restart_with_reloader
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 268, in call
with Popen(*popenargs, **kwargs) as p:
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 708, in __init__
restore_signals, start_new_session)
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 1261, in _execute_child
restore_signals, start_new_session, preexec_fn)
PermissionError: [Errno 1] Operation not permitted
I have already installed flask trough pip in launch_stash.py

I received solution on another forum by user augusto, so I believe its worth sharing it here as well, as it works perfectly.
“The only way to use debug is:
if __name__ == '__main__':
app.run(use_reloader=False, debug=True)”
https://forum.omz-software.com/topic/5758/pythonista-flask-error-errno-1

The iOS sandbox has limitations that will prevent you from creating subprocesses
The flask development server is attempting to create a subprocess for detecting changes to your code that will then reload the server when you make changes. You can disable this by passing "--no-reload" flask run --no-reload. This would mean that you would have to manually restart your development server when you make changes

Change Pythonista’s default interpreter to Python 2.7.

Related

Gunicorn fails with "Application object must be callable" in server hook for Flask app, but only if run as service

I've written a Flask app that's being served with Gunicorn and running on Raspberry Pi OS (Buster). The app is supposed to run automatically as a service on system boot. The issue is, the app fails when run as a service... but only when run as a service...
It used to work until I introduced server hooks into my Gunicorn configuration file. There's a few of them, but the first to be called, and thus fail is:
gunicorn.conf.py:
def on_starting(server):
import wsgi
wsgi.on_starting(server)
wsgi.py:
def on_starting(server):
api_instance = server.app.wsgi()
shared_memory_manager = Manager()
api_instance.requestless_variables = shared_memory_manager.dict()
api_instance.log = server.log
server.log.info("Loading API...")
With the following traceback:
Traceback (most recent call last):
File "/home/pi/.local/bin/gunicorn", line 8, in <module>
sys.exit(run())
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/base.py", line 231, in run
super().run()
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/base.py", line 72, in run
Arbiter(self).run()
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 198, in run
self.start()
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 138, in start
self.cfg.on_starting(self)
File "/home/pi/nano/manager/src/api/gunicorn.conf.py", line 56, in on_starting
api_instance = server.app.wsgi()
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/util.py", line 430, in import_app
raise AppImportError("Application object must be callable.")
gunicorn.errors.AppImportError: Application object must be callable.
As you can see, the error seems to be with api_instance = server.app.wsgi(), which appears in each of my server hooks and is likewise my point of failure in each.
The absolute weirdest thing about this is that the app/Gunicorn works perfectly if instantiated directly from the terminal:
/home/pi/.local/bin/gunicorn -c /home/pi/nano/manager/src/api/gunicorn.conf.py --bind unix:nano_api.sock --umask 007
But produces the above error if instantiated from the following service:
[Unit]
Description=Gunicorn instance serving the Nano API
After=network.target
[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/nano/manager/src/api
ExecStart=/home/pi/.local/bin/gunicorn -c /home/pi/nano/manager/src/api/gunicorn.conf.py --bind unix:nano_api.sock --umask 007
[Install]
WantedBy=multi-user.target
Anyone have any ideas as to what might be causing this issue and how to fix it? Many thanks!
Well, after a solid 10 hours of debugging, I finally figured out the problem...
I'm ashamed to say it was just a naming conflict. I had a package named "api" containing a module named "api.py" that housed a Flask instance named, you guessed it, "api."
Since I've given these three items distinct names and fixed my references, everything's ran smoothly.

raise ValueError("cannot have a multithreaded and multi process server.") ValueError: cannot have a multithreaded and multi process server

Serving Flask app "server" (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
Running on http://0.0.0.0:9002/ (Press CTRL+C to quit)
Restarting with stat
Debugger is active!
Debugger PIN: 314-390-242
Exception in thread Thread-1:
Traceback (most recent call last):
File "/home/user/Downloads/yes/envs/emotionservice/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/home/user/Downloads/yes/envs/emotionservice/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/user/Downloads/yes/envs/emotionservice/lib/python2.7/site-packages/werkzeug/serving.py", line 963, in inner
fd=fd,
File "/home/user/Downloads/yes/envs/emotionservice/lib/python2.7/site-packages/werkzeug/serving.py", line 803, in make_server
raise ValueError("cannot have a multithreaded and multi process server.")
ValueError: cannot have a multithreaded and multi process server.
it solved.
set threaded=True
processes=1
If you want to use flask in multiprocessing mode (processes > 1). include threaded=False, otherwise it throws same error.
app.run(host=addr, port=port, threaded=False, processes=50)

Python flask reloader "Errno 2 No such file or directory" error when running on docker

I have a problem running a little Flask app with Docker. Everything works fine with debug off, but when I try to run it with debug on it fails at the reloader stage. It works fine on Windows, the problem only comes up with Docker.
In my docker repo I install caffe and all the dependencies I need (flask and wekrzeug). I thought the problem could be with the reloader so I also installed watchdog but it still shows up. Doesn't matter whether I run app.py itself or call python -m flask run.
docker run --volume=%cd%:/workspace -p 5001:5000 caffe:cpu
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with inotify reloader
Traceback (most recent call last):
File "app.py", line 53, in <module>
app.run(debug = True, host='0.0.0.0')
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 988, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
File "/usr/lib/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I've encountered the same problem here and the solution for me was to remove the executable flag from the "run.py" file.
$ chmod -x run.py
The error was very similar, but in my case I'm using an Ubuntu 18.04 WSL on a Windows 10 machine.
Try to run the app as a module:
python -m flask run
See https://github.com/pallets/flask/issues/1829 for more information.

Is it possible to run flask without port? [duplicate]

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
How do I get Flask to run on port 80?
(15 answers)
Closed 4 years ago.
I tried to launche Flask in hosting using:
if __name__ == "__main__":
app.run('0.0.0.0', 8000)
But hoster has 8080 port as closed, it is possible to run Flask with default port 80?
I tried it gives me this error:
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "__init__.py", line 461, in <module>
app.run('0.0.0.0', 80)
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 814, in run_simple
inner()
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 774, in inner
fd=fd)
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 660, in make_server
passthrough_errors, ssl_context, fd=fd)
File "/home/o/oliwin4/project/public_html/myenv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 577, in __init__
self.address_family), handler)
File "/usr/lib/python2.7/SocketServer.py", line 419, in __init__
self.server_bind()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/usr/lib/python2.7/SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 1] Operation not permitted
So, hoster said tht 80 port is open.
The Flask documentation states that:
While lightweight and easy to use, Flask’s built-in server is not
suitable for production as it doesn’t scale well. Some of the options
available for properly running Flask in production are documented
here.
The documentation also states several methods of using WSGI servers to deploy your web application. WSGI (Web Server Gateway Interface) is a standard that forwards requests from web servers to web applications written in Python. You'll want to use a WSGI server to run your website on your host, since it sounds like you're trying to deploy a Flask app to a production setting.
To answer your original question, the Flask library intentionally throws an error when you try to run it on port 80, because they specifically say that you should not use Flask's built-in server for production use. That's this part of your stacktrace:
Use a production WSGI server instead.
* Debug mode: off
and
socket.error: [Errno 1] Operation not permitted
For the lazy, or if the above link goes dead, here is an example using Gunicorn (copied directly from the above documentation link)
Gunicorn
Gunicorn ‘Green Unicorn’ is a WSGI HTTP Server for UNIX. It’s a
pre-fork worker model ported from Ruby’s Unicorn project. It supports
both eventlet and greenlet. Running a Flask application on this server
is quite simple:
gunicorn myproject:app
Gunicorn provides many command-line options – see gunicorn -h. For
example, to run a Flask application with 4 worker processes (-w 4)
binding to localhost port 4000 (-b 127.0.0.1:4000):
gunicorn -w 4 -b 127.0.0.1:4000 myproject:app

Flask Google Cloud App Engine: OSError: [Errno 98] Address already in use

I am trying to deploy a flask app on google cloud app engine. It runs smooth in my virtual environment locally but I get an 502 error running it in the cloud.
Now I am trying to debug my code on the cloud server, using debug mode and SSH into my instance. Using docker exec -it [ID] /bin/bash I am able to to access the root of my application. Now I upon running python app.py I get the following error:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "app.py", line 479, in <module>
app.run(port=8080)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 941, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple
inner()
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner
fd=fd)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 660, in make_server
passthrough_errors, ssl_context, fd=fd)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__
self.address_family), handler)
File "/usr/local/lib/python3.6/socketserver.py", line 453, in __init__
self.server_bind()
File "/usr/local/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/lib/python3.6/socketserver.py", line 467, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
I've been trying to kill those processes listed when I run:
ps -fA | grep python
However, this does not solve the problem of the address being in use. Also changing the port in the app.run() does not solve the issue for me.
I had a similar problem, it was caused by the Flask app also being run when the module was loaded, because I had
if __name__ == "__main__":
app.run()
at the bottom. Note that the recent requirement to name your sever file "main.py" could cause this bug to emerge.
I think the problem is that you don't need to specialize the port for cloud. Google Cloud finds the port to run your app on its own. So instead of app.run(port=8080) just write app.run()
While I was not able to figure out how to "free" the running address, I solved the problem by starting another flask process by running it on a different port like so:
flask run --port=80

Categories