I need some help here. I run the code below (using Python 3.7) from a Ubuntu 18.04.05 LTS terminal (typing python filename.py ) and I can view the results on the web page (http://127.0.0.1:500/)
However when I run the code using Spyder (v4.1.4), I encounter the error below when I include the code line app.config["DEBUG"] = True; the same thing occurs when alternatively, I include the code line app(debug = True) in the if __name__ =='__main__' statement.
If I remove this line things work.
This question may have been answered before, but I can't unravel the solution from the answers.
app = Flask(__name__)
app.config["DEBUG"] = True
@app.route('/')
def hello():
return 'HELLO'
if __name__=='__main__':
#app.debug = True
app.run()
This is the output from the Python 3.7 interpreter:
* Serving Flask app "__main__" (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
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with inotify reloader
/home/name/anaconda3/bin/python: can't find '__main__' module in /home/name/Documents /name/Contents/Python/flask_project/api'
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1
/home/name/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3425: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
%tb
Traceback (most recent call last):
File "", line 3, in app.run()
File "/home/name/anaconda3/lib/python3.7/site-packages/flask/app.py", line 990, in run run_simple(host, port, self, **options)
File "/home/name/anaconda3/lib/python3.7/site-packages/werkzeug/serving.py", line 1050, in run_simple run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/home/name/anaconda3/lib/python3.7/site-packages/werkzeug/_reloader.py", line 339, in run_with_reloader sys.exit(reloader.restart_with_reloader())
SystemExit: 1
This is probably because you are using anaconda3 as your main interpreter. I have Anaconda installed in my system, and whenever I use it as the main interpreter while using flask, there were many errors. I would recommend you to not use Anaconda and use a virtual environment and use "vanilla" python.
pip install virtualenv
Unlike other frameworks, flask requires a bit of set up to do things. And since it requires set up, a virtual environment is needed. So, what does this virtualenv do? it does what the name entails, create a virtual environment through the process of installing python in a sub folder and other necessary things that you might need within your project.
Once done, create a folder as a test project instead of creating a single file. After that, create a virtual environment:
virtualenv name-of-the-sub-folder
The name could be anything, my workflow always name it env.
Once the installation is done, activate the virtual environment by doing the following:
source name-of-the-sub-folder/bin/activate
You might notice that your terminal has the same word as your virtual environment directory, that's normal, and it also indicates that you've actually activated it. If you encountered any problems, please refer to this:
How to activate virtualenv?
https://flask.palletsprojects.com/en/1.1.x/installation/
https://youtu.be/Z1RJmh_OqeA
Alright, the next step is to create the python file. You could name it however you want, for me, I name it app.py. (Note: If you are using spyder, please change your project's interpreter to the python.exe of where your project file is located. Or, if you are using a text editor, use the activated terminal to run the code python app.py)
Once done, go back to your terminal you've used to activate the virtual environment and do the following:
pip install flask
This will install the framework IN YOUR VIRTUAL ENVIRONMENT, not in your system. Once done, paste the same code you've used to test flask or use the code pasted below:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == "__main__":
app.run(debug=True)
if not os.path.exists('db.sqlite'):
db.create_all()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With