0

When running flask run it's running fine till FLASK_APP variable is not set, but it gives error when I set FLASK_APP variable.

Error code is attached below.

(venv) (base) C:\Users\Desktop\Python\Flask>flask run
 * 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)

(venv) (base) C:\Users\Desktop\Python\Flask>echo %FLASK_APP%
%FLASK_APP%

(venv) (base) C:\Users\Desktop\Python\Flask>set FLASK_APP=Flask.py

(venv) (base) C:\Users\Desktop\Python\Flask>echo %FLASK_APP%
Flask.py

(venv) (base) C:\Users\Desktop\Python\Flask>flask run
 * Serving Flask app "Flask.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
Usage: flask run [OPTIONS]

Error: While importing "Flask", an ImportError was raised:

Traceback (most recent call last):
  File "c:\users\desktop\python\flask\venv\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "C:\Users\Desktop\Python\Flask\Flask.py", line 1, in <module>
    from .app import app, db
ImportError: attempted relative import with no known parent package


(venv) (base) C:\Users\Desktop\Python\Flask>

Also,how flask is able to identify which python file to run if FLASK_APP variable is not set ?

Vasucd
  • 357
  • 2
  • 10
pkd
  • 471
  • 1
  • 6
  • 17
  • Show your main file for this flask app, and read carefully the error. Seems that you have problems with imports... and second thing I dont think that Flask.py is a good name for a main app file. – darvark Jan 08 '20 at 13:16
  • please refer to this question for file and directory structure : https://stackoverflow.com/questions/59505046/error-while-importing-module-app-using-from-flask-app-import-db – pkd Jan 08 '20 at 13:20
  • Ok, so comapre this "from .app import app, db" and reply from that thread you paste. try remove that "." from import. Explanation of relative imports you can find here https://stackoverflow.com/questions/16981921/relative-imports-in-python-3 – darvark Jan 08 '20 at 13:22
  • it worked, but in pycharm it's showing in red underline and it's running without any error, any idea why it's showing red underline for the statement from app import db – pkd Jan 08 '20 at 13:28
  • do not trust pycharm, test this with interpreter.. I learn that pycharm sometimes has problems with imports, but interpreter doesn't. and if it's free edition of pycharm it doesn't have native support for flask (as far as I remember) – darvark Jan 08 '20 at 13:30

0 Answers0