Error in running FLASK on anaconda Prompt - flask

I want to start the flask server using anaconda Prompt.
while starting,it is showing the following error.How to resolve error.
I am getting the following error.
Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
Debug mode: off
Usage: flask run [OPTIONS]
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.

The problem is you need to set flask's debug mode to true.
set FLASK_APP = yourfilename.py
set FLASK_DEBUG = 1
and then use:
flask run
to run your server.
Note: If you are on mac/linux use export instead of set

Have you used:
$ export FLASK_APP=yourfilename.py
$ export FLASK_DEBUG=1
$ flask run

Related

While importing 'myapp.app' an import error was raised

I'm building a flask application, located in a subdirectory within my project called myapp. Running gunicorn --bind 0.0.0.0:$PORT myapp.app:app works fine, no errors, regardless of what FLASK_APP is set to. When I try to use Flask's CLI, however, it fails to find my app (reasonable), but when I set FLASK_APP to myapp.app., it appears to be doubling up the import path, resulting in an import error:
FLASK_APP=myapp.app flask run
* Serving Flask app 'myapp.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
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: While importing 'myapp.myapp.app', an ImportError was raised.
How can I solve this? Is this a bug in Flask?
There was a __init__.py in my project directory that was causing this.
I dug into the source code of flask in cli.py and found the following code in prepare_import:
# move up until outside package structure (no __init__.py)
while True:
path, name = os.path.split(path)
module_name.append(name)
if not os.path.exists(os.path.join(path, "__init__.py")):
break
Because I had an __init__.py in my project directory (also called myapp), this made flask try to import myapp.app from the ancestor of my project, resulting in myapp.myapp.app.

No such command 'init_db', FLASK-APP ENVIRONMENT

I create a flask and when I tried to deploy it on heroku. There are errors when I try to initiate my db:
maiko#xxxx:~/Desktop/concevez_un_site_avec_flask-P1C1$ heroku run init
Running init on ⬢ test-ultime-maiko... up, run.7933 (Free)
Usage: flask [OPTIONS] COMMAND [ARGS]...
Try 'flask --help' for help.
Error: No such command 'init_db'.
after that I try:
maiko#xxxx:~/Desktop/concevez_un_site_avec_flask-P1C1$ flask --help
Traceback (most recent call last):
File "/home/maiko/.local/lib/python3.8/site-packages/flask/cli.py", line 556, in list_commands
rv.update(info.load_app().cli.list_commands(ctx))
File "/home/maiko/.local/lib/python3.8/site-packages/flask/cli.py", line 398, in load_app
raise NoAppException(
flask.cli.NoAppException: 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.
Usage: flask [OPTIONS] COMMAND [ARGS]...
A general utility script for Flask applications.
Provides commands from Flask, extensions, and the application. Loads the
application defined in the FLASK_APP environment variable, or from a
wsgi.py file. Setting the FLASK_ENV environment variable to 'development'
will enable debug mode.
$ export FLASK_APP=hello.py
$ export FLASK_ENV=development
$ flask run
Options:
--version Show the flask version
--help Show this message and exit.
Commands:
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context.

Flask not displaying debug mode [duplicate]

How are you meant to debug errors in Flask? Print to the console? Flash messages to the page? Or is there a more powerful option available to figure out what's happening when something goes wrong?
Running the app in debug mode will show an interactive traceback and console in the browser when there is an error. As of Flask 2.2, to run in debug mode, pass the --app and --debug options to the flask command.
$ flask --app example --debug run
Prior to Flask 2.2, this was controlled by the FLASK_ENV=development environment variable instead. You can still use FLASK_APP and FLASK_DEBUG=1 instead of the options above.
For Linux, Mac, Linux Subsystem for Windows, Git Bash on Windows, etc.:
$ export FLASK_APP=example
$ export FLASK_DEBUG=1
$ flask run
For Windows CMD, use set instead of export:
set FLASK_DEBUG=1
For PowerShell, use $env:
$env:FLASK_DEBUG = "1"
If you're using the app.run() method instead of the flask run command, pass debug=True to enable debug mode.
Tracebacks are also printed to the terminal running the server, regardless of development mode.
If you're using PyCharm, VS Code, etc., you can take advantage of its debugger to step through the code with breakpoints. The run configuration can point to a script calling app.run(debug=True, use_reloader=False), or point it at the venv/bin/flask script and use it as you would from the command line. You can leave the reloader disabled, but a reload will kill the debugging context and you will have to catch a breakpoint again.
You can also use pdb, pudb, or another terminal debugger by calling set_trace in the view where you want to start debugging.
Be sure not to use too-broad except blocks. Surrounding all your code with a catch-all try... except... will silence the error you want to debug. It's unnecessary in general, since Flask will already handle exceptions by showing the debugger or a 500 error and printing the traceback to the console.
You can use app.run(debug=True) for the Werkzeug Debugger edit as mentioned below, and I should have known.
From the 1.1.x documentation, you can enable debug mode by exporting an environment variable to your shell prompt:
export FLASK_APP=/daemon/api/views.py # path to app
export FLASK_DEBUG=1
python -m flask run --host=0.0.0.0
One can also use the Flask Debug Toolbar extension to get more detailed information embedded in rendered pages.
from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
import logging
app = Flask(__name__)
app.debug = True
app.secret_key = 'development key'
toolbar = DebugToolbarExtension(app)
#app.route('/')
def index():
logging.warning("See this message in Flask Debug Toolbar!")
return "<html><body></body></html>"
Start the application as follows:
FLASK_APP=main.py FLASK_DEBUG=1 flask run
If you're using Visual Studio Code, replace
app.run(debug=True)
with
app.run()
It appears when turning on the internal debugger disables the VS Code debugger.
If you want to debug your flask app then just go to the folder where flask app is. Don't forget to activate your virtual environment and paste the lines in the console change "mainfilename" to flask main file.
export FLASK_APP="mainfilename.py"
export FLASK_DEBUG=1
python -m flask run --host=0.0.0.0
After you enable your debugger for flask app almost every error will be printed on the console or on the browser window.
If you want to figure out what's happening, you can use simple print statements or you can also use console.log() for javascript code.
To activate debug mode in flask you simply type set FLASK_DEBUG=1 on your CMD for windows, or export FLASK_DEBUG=1 on Linux terminal then restart your app and you are good to go!!
Install python-dotenv in your virtual environment.
Create a .flaskenv in your project root. By project root, I mean the folder which has your app.py file
Inside this file write the following:
FLASK_APP=myapp
FLASK_ENV=development
Now issue the following command:
flask run
When running as python app.py instead of the flask command, you can pass debug=True to app.run.
if __name__ == "__main__":
app.run(debug=True)
$ python app.py
with virtual env activate
export FLASK_DEBUG=true
you can configure
export FLASK_APP=app.py # run.py
export FLASK_ENV = "development"
to start
flask run
the result
* 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: xxx-xxx-xxx
and if you change
export FLASK_DEBUG=false
* Environment: development
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
For Windows users:
Open Powershell and cd into your project directory.
Use these commandos in Powershell, all the other stuff won't work in Powershell.
$env:FLASK_APP = "app"
$env:FLASK_ENV = "development"
If you have PyCharm Professional, you can create a Flask server run configuration and enable the FLASK_DEBUG checkbox. Go to Run > Edit Configurations, select or create a Flask server configuration, and enable the FLASK_DEBUG checkbox. Click OK, then click the run button.
You can install python-dotenv with
pip install python-dotenv then create a .flask_env or a .env file
The contents of the file can be:
FLASK_APP=myapp
FLASK_DEBUG=True
Use loggers and print statements in the Development Environment, you can go for sentry in case of production environments.

Pycharm Flask server run config says Flask isn't installed, won't set the FLASK_APP variable

I have written a Flask app called "lingua" using PyCharm Professional 2019.2.3. Everything works. The unit tests pass when run from the test configuration. I can run the server from the command line.
However, when I run it from the PyCharm run configuration it doesn't work because the FLASK_APP environment variable is not set.
FLASK_APP not set
FLASK_ENV = development
FLASK_DEBUG = 1
In folder /Users/wmcneill/src/lingua
/Users/wmcneill/src/lingua/venv/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --module --multiproc --qt-support=auto --client 127.0.0.1 --port 62823 --file flask run
pydev debugger: process 53132 is connecting
Connected to pydev debugger (build 192.6817.19)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
pydev debugger: process 53137 is connecting
* Debugger is active!
* Debugger PIN: 274-123-483
When you try to get the server page you get a flask.cli.NoAppException error. The same thing happens if you explicitly set FLASK_APP=lingua in the environment variables of the run configuration. Same results if the "Target type" is "Module name", "Script path", or "Custom". The "Python Interpreter" path in the configuration is correct.
Furthermore, there is a red "x" on the configuration and an erroneous error message at the bottom of the configuration screen that reads "Warning: Flask 0.12 or higher required".
This warning is incorrect.
$ flask --version
Python 3.6.8
Flask 1.1.1
Werkzeug 0.16.0
Furthermore, the Flask server configuration worked on PyCharm yesterday, and I don't know what has changed since then. I have intermittently seen this problem on other PyCharm Flask projects. It comes and goes. I can't figure out what is triggering it.
Any idea what this problem is or how I can debug it?

why is django ignoring some env variables unless I pass them to httpd upon restart?

This is my application environment:
Redhat 6.0
Apache 2.2
Django 1.3.0
Python 2.6.6
cx_Oracle 5.1
mod_wsgi 3.2
Oracle DBMS 11.2 (on a different machine)
Issue:
accessing my django website from a web browser fails because cx_Oracle can't acquire Oracle handle.
Tests:
running django-admin.py shell to query the db DOES WORK (running from a bash session with ORACLE_HOME and LD_LIBRARY_PATH vars set)
running a custom script that adds ORACLE_HOME and LD_LIBRARY_PATH to os.environ and uses cx_Oracle to connect to DB, without using django DOES WORK (running from a bash session with ORACLE_HOME and LD_LIBRARY_PATH vars set)
running the same custom script with "sudo -u apache" does NOT work
running the same custom script with "sudo -u env ORACLE_HOME=foo LD_LIBRARY_PATH=foo myscript.py" DOES WORK
modifying django.db.backends.oracle.base.py so that, before the Database.connect statement, I have os.environ printed to a file, SHOWS I HAVE ORACLE_HOME AND LD_LIBRARY_PATH CORRECTLY SET
explicitly setting ORACLE_HOME and LD_LIBRARY_PATH in os.environment inside adhoc django wsgi.py script DOES NOT WORK
restarting httpd with "sudo env ORACLE_HOME=foo LD_LIBRARY_PATH=foo /etc/init.d/httpd restart" DOES WORK
My question is: why is test 7 different from tests 2, 5, 6? To put it another way, why do I have to pass env vars to parent httpd process instead of just setting them in python/django scripts?
The LD_LIBRARY_PATH must be in the environment prior to a process being executed. The process runtime loader will only read it once at startup, changing the value of the environment after the process has been started makes no difference. This is just how things work.