Whats the difference between ./webapp.py and python webapp.py [duplicate] - python

This question already has answers here:
Getting Python error "from: can't read /var/mail/Bio"
(6 answers)
Closed 9 months ago.
I have started with python n flask few days ago. I was just trying to run a python file webapp.py on a terminal with following code but got errors:
$ ./webapp.py
from: can't read /var/mail/flask
from: can't read /var/mail/flask
./webapp.py: line 3: syntax error near unexpected token `('
./webapp.py: line 3: `app = Flask(__name__)'
But it runs successfully with the command:
$ python webapp.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
webapp.py
from flask import Flask
from flask import render_template
app = Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)
As a part of curiosity,
Whats the difference between ./webapp.py and python webapp.py?

When running a python script directly (without specifying the interpreter in the command), you need to tell the shell which interpreter will process the script, e.g.:
#!/usr/bin/env python
from flask import Flask
from flask import render_template
This first line is often referred to as "shebang".

Related

How to run Python (Flask) Server in Background? [duplicate]

This question already has answers here:
How to host Python 3.7 flask application on Windows Server?
(1 answer)
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Serving Flask app with waitress on windows
(8 answers)
Closed 1 year ago.
I want to run a local server which executes as specific script when a GET request to the IP is made,
because of that I need to run the server in the background (while a other script is runing too) without printing any stuff.
Here is my Code:
from flask import Flask
app = Flask(__name__)
#app.route('/', methods = ['GET'])
def index():
return "Hello How are you?"
if __name__ == '__main__':
app.run(port = 5000)
I tried it with Threading but I always get stuck at this Output
When running this code I get this Output:
* 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://IP:5000/ (Press CTRL+C to quit)```
You could use pythonw to run the application in the background:
pythonw -m flask run > log.txt 2>&1
make sure the name of the Python file is app.py.

Trying to run flask with the following code [duplicate]

This question already has answers here:
Warning message while running Flask
(12 answers)
Closed 2 years ago.
from flask import Flask
app = Flask(_name_)
#app.route('/')
def index():
return '<h1>Hello, world</h1>'
I get this error
* Serving Flask app "application.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
This is just a warning as flask server is not meant for production.
You can use
export FLASK_ENV=development
before flask run
It depends if you are running it from command line or adding python code to run it.
If you want to run it with Python, add this to the bottom of your code.
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
(you can change the port number, to access it on a browser, type: 127.0.0.1:80)
If you are running it from command line, you run
flask run
Note this appears every time:
Serving Flask app "application.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
All it tells you is that this server is only used for development or production. Hosting it for commercial/public use would require additional files and web hosting.
You can allow debugging to be on with
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, DEBUG=True)
OR
export FLASK_ENV=development (linux/macos)
set FLASK_ENV=development (Windows)
Hope this helps (follow me on GitHub #haydenso)

Flask not activating debug mode

I am getting started with flask, I am trying to follow some tutorials, but I have been unable to run Flask app in debug mode.
I tried the simplest code I found:
from flask import Flask
app = Flask(__name__)
app.debug = True
# I have also tried with a configuration
# app.config.from_object('config')
# file with constant
# DEBUG = True
#app.route('/')
def hello_world():
return 'Hello World!'
Then I run
export FLASK_APP=hello_world.py
flask run
But I allways get this output
* Serving Flask app "hello_world.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
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
When I run print(app.debug) I get False
This is the output of pip freeze:
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1
And I have python 3.8.2
I have tried the below steps and it worked for me.
Have added these lines of codes to my app.py file.
if __name__ == "__main__":
app.run(debug=True)
Then I ran the program from the Terminal using the command line
python3 app.py
and it worked.
Please find the below screenshot
if you are using windows just type the following in your terminal :
in PowerShell:
$env:FLASK_ENV = "development"
flask run
in command prompt :
C:\path\to\app>set FLASK_ENV=development
flask run
and for mac you probably need to run this :
$ export FLASK_ENV=development
$ flask run
enjoy !!
Try This:
When you run,
export FLASK_APP=hello_world.py
Run this command after the above,
export FLASK_DEBUG=1
Finally,
flask run
I hope this would start the debug mode.
Make sure that you're running the right program, sometimes it happens
that we're making changes in some other program and running something
else.
Also ensure that Setting app.debug = True is altogether a
different thing from setting FLASK_DEBUG mode to on.
Try this, this works at my end.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == "__main__":
app.run(debug=True)
There are two way to run Flask App.
Environment Variable
Programmatically
Environment Variable you need to exporting the FLASK_APP file using command for window set FLASK_APP = *(file_name)*.
set FLASK_APP = app.py`
And for mac
export FLASK_ENV=development
You can also control debug mode separately from the environment by exporting FLASK_DEBUG=1. (set command for mac and export for window)
set FLASK_APP = app.py
set FLASK_DEBUG=1
But sometimes debugger does not work on production servers. It is a major security risk to be used on production machines.
Programmatically you have to include main section on your Flask app.py main file. It will run your app in debug mode easily but do not use flask run command use only python (file_name) command.
from flask import Flask
app = Flask(__name__)
app.debug = True
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
python app.py
Apparently, because debug mode is already off, stopping and rerunning it didn't work.
add the line below to app.py and save.
if __name__ == "__main__":
app.run(debug=True)
restart the editor.
then run
set FLASK_DEBUG=1
then run your project
python3 app.py

Error launching Flask app with error "Failed to find Flask application"

I'm new to Flask. To launch the flask app, I did python -m flask run but I repeatedly get the error:
Failed to find Flask application or factory in module "app". Use "FLASK_APP=app:name to specify one.
I am using a virtualenv on a Windows 10 machine
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)
I expected a web server to start and to be able to navigate to localhost http://127.0.0.1:5000/ where I could view Hello, World! on my browser, but that never happened.
Instead of just "flask" use FLASK_APP=theflaskapp.py, like what Marco suggested:
env FLASK_APP=theflaskapp.py python -m flask run
This should fix it, if not, make sure you are executing the command to run the script in the same directory as it. You should also check if the problem is in flask or not by running "python theflaskapp.py" (In the same directory as the flask app still) and see if it works at all.
Reproducing the problem, and fixing it
Hello, I have reproduced the problem
This is the error code:
Error: Failed to find Flask application or factory in module
"src.app". Use "FLASK_APP=src.app:name to specify one.
Steps of reproducing the error:
Create a file called app.py
inside app.py put this code:
from flask import Flask
def create_app():
app = Flask("abc")
#app.route('/')
def hello_world():
return 'Hello, World!'
Or let the file be empty, like this:
# This is an empty file
# There is no flask application here
Inside CLI run these commands:
export FLASK_APP=app.py
flask run
watch the error appear on the screen
Solution 1 :
make the function return the app
Clearly create a variable called app and make it equal to the return value of the function, like this:
from flask import Flask
def create_app():
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
return app
app = create_app()
Solution 2: Get the app outside of the function:
from flask import Flask
app = Flask("abc")
#app.route('/')
def hello_world():
return 'Hello, World!'
solution for flask Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
on windows when you tried set FLASKAPP=appname.py if your using PowerShell and if get the above error try this 👇👇
$env:FLASK_APP = "filename"
$env:FLASK_ENV = "development"
Flask run
try it if your having trouble launching the flask app.
You need to specify what file you want to work with. Try this.
For Windows:
set FLASK_APP=app.py
python -m flask run
For Mac OS and Linux:
export FLASK_APP=app.py
python -m flask run
I had a similar problem. It seems to work fine from the command prompt with python app.py. However, it would fail to launch with VS Code Debug.
The trick was to add:
server = app.server
before calling the app.run_server() portion
super simple in my case, and almost stupid...I had forgotten to save the file (CTRL+S), and I got exactly the same error. Once I saved the file, it worked directly!
Instead of providing a super straightforward "try this" suggestion, I'd recommend going straight to the flask source code to where this exception gets thrown, and reverse engineer from there. https://github.com/pallets/flask/blob/main/src/flask/cli.py#L78-L82
You can open this in your editor by going to the site-packages/flask/cli.py file and setting breakpoints around where this error gets thrown, and poke around from there.
Another working option is executing this command instead of flask run :
flask run --host=0.0.0.0
I had the same issue and I tried all the mentioned answers here but couldn't work finally this did the trick.
I'm getting the same error. The problem is you have to set the environment variable FLASK_APP in current project.
However, I have easiest way to solve this problem. Instead of using flask run command you can use python app.py.
"app.py" is your application entry point.
The simplest way to run your app without getting any issue is to create the app then run python app.py
from flask import (
Flask,
jsonify
)
# Function that create the app
def create_app(test_config=None ):
# create and configure the app
app = Flask(__name__)
# Simple route
#app.route('/')
def hello_world():
return jsonify({
"status": "success",
"message": "Hello World!"
})
return app # do not foget to return the app
APP = create_app()
if __name__ == '__main__':
# APP.run(host='0.0.0.0', port=5000, debug=True)
APP.run(debug=True)
On venv and my Windows10 only this syntax works:
set FLASK_APP=name_of_file:name_of_var_app
If file is application.py and 'app = Flask(__name__)',
run:
set FLASK_APP=application:app
You should change the name of the file, I have the same problem. So, I changed the name of the app file and It works for me. At first my app file name is 'socket.py' and I change it to 'app.py'.
My problem was that the python file didn't have execute permissions:
chmod a+x app.py
Similar issue! I was running the code in vs code. Installed python extension and after selecting a different python interpreter( in my case 3.10.5 64 bit), got a play button on the top right corner of my vs code interface.
That solved the issue for me.
In my case was a very simple issue, the activate.bat file was setup as Macintosh (CR) I changed to Windows (CR LF) and that worked for me.
Explanation: In windows OS, for the instruction to take effect, for example [set FLASK_ENV = development] a strong carriage return is needed, such as the (CR LF), that is what I could verify in my case. You can try to run the SET instruction in the console, cmd, and check if the FLASK settings are reflected. I understand that it is more a problem of the configuration of the environment and not of the application itself.
Try removing the spaces from this line of code, this is what fixed it for me when I had this error:
app=Flask(__name__)

Flask not working in Virtualenv setup

SetUp
VirtualBox | Ubuntu Server 12.04.2
(flaskve)vks#UbSrVb:~/flaskve$ python --version
Python 2.7.3
ifconfig
192.168.1.100 (the bridge interface on which i interact with VirtualBox)
code I am trying to run.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='192.168.1.100', port=8080, debug=True)
When I do
(flaskve)vks#UbSrVb:~/flaskve$ python start.py
(flaskve)vks#UbSrVb:~/flaskve$
It does not run or do anything, it just returns back to command prompt. Although I am running in debug=True mode.
I then made a new VirtualEnv and install bottle in that. When I tried to run helloworld it shows the same behaviour.
However I then started the python shell on the same virtualenv, imported bottle modules and ran
>>> from bottle import route, run
>>> run(host='192.168.1.100', port=8081, debug=True)
Bottle v0.11.6 server starting up (using WSGIRefServer())...
Listening on http://192.168.1.100:8081/
Hit Ctrl-C to quit.
What could be problem here ?
Even debug does not show anything.
Following link is the output of python -v start.py
http://paste.ubuntu.com/5713138/
The first example uses Flask, not bottle. Maybe you are confusing your code snippets here? :)

Categories

Resources