AttributeError: 'NoneType' object has no attribute 'SSLContext' running flask script in PyCharm 2018.3.7 on Win10

Viewed 4725

Running the Hello world script using flask:

# coding=utf-8
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

I get the following error(s):

C:\anaconda3\python.exe C:/anaconda3/Scripts/flask-script.py run
Traceback (most recent call last):
  File "C:/anaconda3/Scripts/flask-script.py", line 9, in <module>
    sys.exit(main())
  File "C:\anaconda3\lib\site-packages\flask\cli.py", line 966, in main
    cli.main(prog_name="python -m flask" if as_module else None)
  File "C:\anaconda3\lib\site-packages\flask\cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "C:\anaconda3\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "C:\anaconda3\lib\site-packages\click\core.py", line 1135, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  File "C:\anaconda3\lib\site-packages\click\core.py", line 641, in make_context
    self.parse_args(ctx, args)
  File "C:\anaconda3\lib\site-packages\click\core.py", line 940, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "C:\anaconda3\lib\site-packages\click\core.py", line 1477, in handle_parse_result
    self.callback, ctx, self, value)
  File "C:\anaconda3\lib\site-packages\click\core.py", line 96, in invoke_param_callback
    return callback(ctx, param, value)
  File "C:\anaconda3\lib\site-packages\flask\cli.py", line 742, in _validate_key
    is_context = isinstance(cert, ssl.SSLContext)
AttributeError: 'NoneType' object has no attribute 'SSLContext'

Process finished with exit code 1

Configuration in PyCharm should be correct: config

Flask 1.1.1 already installed in anaconda3 environment. Im using PyCharm 2018.3.7 on Win10 Home.

Could you please help me how to fix this or what does the error says? Thank you in advance!

4 Answers

FLASK_APP should have your flask module.

flask quickstart would be helpful.

you need to tell your terminal the application to work with by exporting the FLASK_APP environment variable

BTW, pycharm provides easy way to create flask app. This link also helps you.

Here is my simple example.

$ cat hello.py 
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "hello world"
$ env FLASK_APP=hello.py  flask run
 * Serving Flask app "hello.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) 
127.0.0.1 - - [12/Apr/2020 12:13:04] "GET / HTTP/1.1" 200 -

You must run it from the anaconda prompt in the right environment.

(data-class) C:\Users\Faramarz>cd /d D:\...\surfs_up

(data-class) D:\...\surfs_up>set FLASK_APP=app.py

(data-class) D:\...\surfs_up>flask run
 * Serving Flask app "app.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)
127.0.0.1 - - [18/Oct/2020 17:27:47] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [18/Oct/2020 17:27:47] "GET /favicon.ico HTTP/1.1" 404 -

I had this problem and could not get my Flask App to work through VS Code. There is something wrong with my environment so I searched my computer for the Anaconda Powershell, cd into the correct folder, and then typed: set FLASK_APP=app.py and ran that.

When it did not throw an error I ran flask run in the next line and it gave me my local address. One error that the powershell threw out was it could not find my file because it did not exist so I changed the file to app.py and it worked.

This has occured due to not setting env variables properly.

try these in terminal

set FLASK_APP=your_application_file_name.py
set FLASK_ENV=development

If this still fails try adding env variables to user variables using GUI. In my case the adding FLASK_APP : your_application_file_name.py did the job.

https://i.stack.imgur.com/Ge4R9.png

enter image description here

Related