When running the flask app 'app.py' without debug mode, everything runs fine:
app = Flask(__name__)
#app.route("/")
def home():
return "Hey there"
if __name__=="__main__":
app.run()
Server startign:
* 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: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
When running the Flask app in debug mode
app.run(debug=True) I'll get "No module named app" error and the server won't start:
* 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
* Restarting with stat
No module named app
I have noticed that when starting the script in vsCode without debug mode (ctrl+f5) or press the button 'Run Python File' everything works fine.
When I start debugging (f5) in vsCode the 'No module named app' occurs and after the failed server start I jump back to the parent folder (app.py is in /folder/app/app.py - after server start failed I am in the terminal in the '/folder' rather then '/folder/app' like before starting the script.
Does anyone got an idea where the problem is? Appreciate any help
The problem can be resolved by adding a launch.json file to the workspace and adding either an absolute to your app.py file:
"cwd":"path/to/folder/"
or a relative path to the directory of the executed script:
"cwd":"${fileDirname}"
Related
I have made a simple python flask program :
# save this as app.py
from flask import request
from flask import Flask
app = Flask(__name__)
#app.route("/", methods=['POST'])
def hello():
return "Hello, World!"
#app.route("/sms", methods=['POST'])
def sms():
print(request.get_json())
return "sms world"
if __name__ == '__main__':
app.run(port=443, host='0.0.0.0', ssl_context='adhoc')
This handles the HTTP post request. How can I make it handle HTTPS post requests?
When I execute the command flask run I get the following:
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 * Running on http://127.0.0.1:5000/
(Press CTRL+C to quit)
So it still uses HTTP instead of https
pip install pyopenssl
When you run the script (or start with flask run if you prefer), you will notice that Flask indicates that it is running an https:// istance
first install pyopenssl with the command:
pip install pyopenssl
to launch it in https just add the parameter: ssl_context='adhoc'
if __name__ == '__main__':
app.run(port=443, host='0.0.0.0', ssl_context='adhoc')
Once started the following message will be shown:
* Serving Flask app 'test' (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: off
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on https://192.168.0.62:443/ (Press CTRL+C to quit)
Obviously then once the application goes into production this parameter will no longer be needed, but you will have to set your wsgi to communicate in https
You can try ngrok, it is a useful tool to deploy an HTTPS and HTTP service from local to public net.
https://ngrok.com/
It's hard to remember when, but at one point the auto-reload function of Flask started to not work anymore in my project.
This is the output upon starting my app :
FLASK_APP = back/python/app/app.py:app
FLASK_ENV = development
FLASK_DEBUG = 1
In folder C:/path/to/project
ssh://[VirtualMachineIP]:22/root/env/bin/python3.7 -u -m flask run -h 0.0.0.0 -p 1234
* Serving Flask app 'back/python/app/app.py:app' (lazy loading)
* Environment: development
* Debug mode: on
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://[VirtualMachineIP]:1234/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 106-048-128
The development environment and Debug mode are both on. Thus, upon saving changes in a file (while the app is deployed) I get the usual message :
* Detected change in '/path/to/changed/file.py', reloading
Signaling that the app is reloading with the new code. Except it doesn't reload anything, and the message doesn't appear on any further changes until I'm forced to restart the app.
PyCharms runs on Windows and communicates via ssh to my Virtual Machine, where the code is executed. I have installed the following modules:
flask
flask-socketio
eventlet
flask-cors
Any help is welcomed. Thanks :)
The FLASK_DEBUG environment variable is badly supported, it may not behave as expected if set in code. (Quoted from the source of flask).
It suggest to use flask run in debug mode.
eg: $ flask --app hello --debug run
If it still not work, you can force to use reloader like this:
if __name__ == '__main__':
app.run(host=config.HOST, port=config.PORT, debug=True)
Take care, the app.run() must be wrapped with if __name__ == '__main__'.
doc: https://flask.palletsprojects.com/en/2.2.x/config/#DEBUG
I have been using docker desktop in windows 10 and running the flask application with it. So when I change the python code the docker will auto restart and perform. We can be able to check the logs on the docker desktop's container logs. But now it just shows as below:
Serving Flask app "web_app" (lazy loading)
* Environment: development
* Debug mode: on
Instead it needs to be
* 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
* Restarting with stat
* Debugger is active!
* Debugger PIN: 139-055-956
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
So that I can be able to check the live logs. Let me know if there is a solution for this to see the live logs with the mentioned details.
Running this code on Windows 10 Pro
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
And see the following as a result
** * Serving Flask app "test" (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
* Debugger is active!
* Debugger PIN: 198-193-169
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
**
However when I try to open this url http://127.0.0.1:5000/
I see the following error on the webpage
{"error":"Not found"}
Any idea why this is not working on windows?
Do I need to enable any permissions?
Thanks
There was another process listening on the same port. I killed that process and problem was solved for me
When I type Flask run to run my application, everything runs as expected. Dotenv sets FLASK_APP=zigweb/main.py. However, if I go to zigweb folder and type python main.py I get import errors. I'm developing in pycharm, I've separated out my project into a server, a telegram bot, and a flask app so I can dockerise.
Results of flask run
* Serving Flask app "zigweb/main.py" (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
* Debugger is active!
* Debugger PIN: 264-034-887
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Results of python main.py
Traceback (most recent call last):
File "main.py", line 3, in <module>
from .app import app, db, signal_saved
ModuleNotFoundError: No module named '__main__.app'; '__main__' is not a package
main.py
#!/usr/bin/env python3
import os
from .app import app, db, signal_saved
from .app.models import User, Signal
from .app.functions import btc_to_satoshi
import pickle
from .pricedata import start_price_check
from dotenv import load_dotenv
load_dotenv()
#app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User, 'Signal': Signal}
#app.context_processor
def satoshis():
return dict(satoshis=btc_to_satoshi)
def some_func(signal):
query = Signal.query.all()
with open('.\\zigweb\signals.pickle', 'wb') as f:
pickle.dump(query, f, pickle.HIGHEST_PROTOCOL)
app.logger.info(f'Pickled object to {f}')
signal_saved.connect(some_func)
if __name__ == '__main__':
app.run()
start_price_check()
Project structure:
zigbot
nginx
zigbot (not flask)
app
scripts.py
main.py
zigweb (flask)
app
models.py
__init__.py
etc
main.py
some-other-scripts.py
If I change main.py to address the import errors, I don't understand how it then works with flask run if it fails with python main.py. Anyway, if I address the import errors by removing the '.' from .app Pycharm then red-underlines those imports, but it nearly runs.
* 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
* Restarting with stat
C:\Users\phill\Anaconda3\envs\zigbot\python.exe: can't open file 'C:\Users\phill\PycharmProjects\zigbot\main.py': [Errno 2] No such file or directory
UPDATE 2
If I turn OFF debug, the server runs correctly by running python main.py but it produces the ABOVE error if debug is set to 1.