I have a very simple flask application with 2 files app.py and test.py both under same main folder.
app.py:
from flask import Flask
import subprocess
app = Flask(__name__)
#app.route("/")
def home():
proc = subprocess.Popen(['python', 'test.py'])
return "Hello, Flask!"
test.py:
def main():
print('PING')
if __name__ == '__main__':
main()
I created the virtual environment and added flask into it.
When I run app.py using VSCode Python:Flask debugging I got the 'No module named test.py' error like below:
Serving Flask app 'app.py' (lazy loading)
Environment: development
Debug mode: on
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Restarting with stat
Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
127.0.0.1 - - [08/Dec/2021 12:03:46] "GET / HTTP/1.1" 200 -
No module named test.py
But when I run it using integrated terminal by 'python -m flask run'
everything works fine and 'PING' being printed like below:
Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. * 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)
127.0.0.1 - - [08/Dec/2021 12:00:02] "GET / HTTP/1.1" 200 -
PING
I guess I miss some python debugging configuration but cannot realize which one. What should I add in order to fix it during debugging?
The code doesn't show any errors ,but on the output port i.e http://127.0.0.1:5000/ Internal server error is displayed.
This is my code
from flask import Flask,render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/about')
def virtualization():
return render_template('about.html')
#app.route('/services')
def service():
return render_template('services.html')
if __name__ == '__main__':
app.run(debug=True)
And in output terminal
* 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: 264-698-791
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
As Weird as it seems , The project started working when I moved the whole project folder from desktop to a different directory!!
I have all my python projects in this directory maybe something to do with python path.
I have a flask app in which I'm implementing logging into a file. The code is as follows:
from flask import request, Flask
import os
from werkzeug.utils import secure_filename
import logging
app = Flask(__name__)
logging.basicConfig(filename='fileStorage.log', level=logging.DEBUG)
print('helloworld')
When I enter >flask run I get the following response:
* Serving Flask app "PostData.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
helloworld
For some reason, the text * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Doesn't appear. It will appear, however, if I don't set the 'filename' parameter in logging.basicConfig
What am I missing here?
I'm developing my first app in python and it looks like something is wrong with my configuration. If there's an error in jinja template app says nothing and basically hangs up.
Here's my minimal example:
./example.py:
import flask
app = flask.Flask(__name__)
#app.route('/')
def index():
return flask.render_template('example.html');
./templates/example.html:
<html>
{{ url_for('example', filename='doesnt.matter') }}
this page will never render, RIP
</html>
./start.sh: FLASK_APP=example.py FLASK_ENV=development flask run
So when I run flask and go to localhost - request gets stuck and I never get any response. Also the whole server becomes unresponsive and I'm forced to restart it.
Environment:
Python version: 3.6.5
Flask version: 1.1.1
I'm on win7x64 mingw under venv, however when I launch via windows command prompt this behavior continues
console looks like this:
* Serving Flask app "example.py" (lazy loading)
* Environment: development
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 253-384-519
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Also I suppose it's not only about url_for and rather it goes for any error in the render_template function
The problem was in python version. I updated it to the latest (3.8.5) and problem disappeared.
I've read a couple dozen threads on this issue and I cannot find a solution that has worked for me so any help you can provide for me to understand what is breaking the reloader is greatly appreciated. I'm still fairly new to dev (mainly worked in node, angular and django). I've done tests on mac and ubuntu, with both a minimal flask app and a large application setup via their docs, here is the simplest test of the various attempts I've made:
in terminal:
python3 -m venv virtualenv
source virtualenv/bin/activate
pip install flask
vim server.py
then
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return 'Hi'
wq and then back in terminal:
export FLASK_ENV=development
export FLASK_DEBUG=1
export FLASK_APP=server.py
flask run
now change the return statement string from 'hi' to 'hello' and nothing changes until I hit refresh on the browser. Importantly, the server does announce that there was a change - it just doesn't reload the browser. See output below:
(virtualenv) ➜ test git:(master) ✗ Flask run
* Serving Flask app "server.py" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 297-732-354
* Detected change in '/Users/pb/Documents/code/projects/test/server.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 297-732-354
I've tried this with template rendering, with alterations to css files, and like above with simple return statements. Thank you for any help you can offer me.
Flask detects the local file changes, but won't push the changes to your browser. Flask and the browser are fully de-coupled, Flask just stands there and waits for requests, it only responds if it receives a request. After return 'Hello', the connection between the browser and Flask is going to be disconnected.
If you don't make a request to Flask, Flask can't send you the response.
You can use https://github.com/lepture/python-livereload to create a livereload debug server which can auto refresh your browser.
I don't think the browser is supposed to reload the information by itself - you have to refresh the page for anything new to show up, unless you have built that functionality in with some sort of backend application.
This library livereload automatically refreshes the page upon file changes.
One of the latest versions is not working correctly, so you must install the specific version 2.5.1.
Install livereload at 2.5.1
pip install -Iv livereload==2.5.1
Change your wsgi.py (provided by the documentation)
# app is a Flask object
app = create_app()
# remember to use DEBUG mode for templates auto reload
# https://github.com/lepture/python-livereload/issues/144
app.debug = True
server = Server(app.wsgi_app)
# server.watch
server.serve()
Run python wsgi.py instead of flask run
Notice that it should display a different terminal than flask's.