相关文章推荐
从未表白的棒棒糖  ·  python - Flask - ...·  2 月前    · 
千杯不醉的牙膏  ·  flask ...·  2 月前    · 
高兴的啄木鸟  ·  php日期选择框-掘金·  1 年前    · 
逼格高的橙子  ·  CSS ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using a Mac , the python version is 2.7.10. and I installed flask

➜  Flask_blog python Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask

I try to follow the tutorial of flash on http://flask.pocoo.org/docs/1.0/

the commands:

➜  Flask_blog export FLASK_APP=flaskblog.py
➜  Flask_blog flask run                    
zsh: command not found: flask
➜  Flask_blog 

code in flaskblog.py:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World!"

The error is command not found: flask

I also tried an other tutorial.

commands:

➜  Flask_blog cd /Users/jzd/Movies/flask/Second_video 
➜  Second_video python one.py                           
Sorry

code in one.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
    return 'Hello World'
if __name__ == '__name__':
    app.run('0.0.0.0')
else:
    print("Sorry")

the condition __name__ == '__name__': did not pass.

I guess the python venv matters.

Really want to know how to fix it.

Verify where you have installed flask:

mortiz@florida:~/Documents/projects$ pip freeze |grep -i flask
Flask==1.0.2
mortiz@florida:~/Documents/projects$ pip2 freeze |grep -i flask
Flask==1.0.2
mortiz@florida:~/Documents/projects$ pip3 freeze |grep -i flask
Flask==1.0.2
Flask-CLI==0.4.0
Flask-Jsonpify==1.5.0
Flask-RESTful==0.3.6
Flask-SQLAlchemy==2.3.2

Verify you are installing flask for your correct python version inside your virtual environment.

Find out your python version "inside your (venv)"

mortiz@florida:~/Documents/projects/python/APIS/new_project_py_2_7$ which python
    /home/mortiz/Documents/projects/python/APIS/new_project_py_2_7/venv/bin/python
(venv) mortiz@florida:~/Documents/projects/python/APIS/new_project_py_2_7$ python --version
Python 3.5.3

Installation of flask for python3

pip3 install flask
python3 -m pip install flask

Installation of flask for python2

pip2 install flask
python2 -m pip install flask

Installation of flask for default python (be careful if you are inside your (venv) or in your shell)

pip install flask
python -m install flask
  

Explanation

For people who run higher versions of Flask consider evaluating your environment as explained here.

For me the problem was installing flask for python2 when the binary of my (venv) ran python3.

maybe you forget, export FLASK_APP you can try this command

 export FLASK_APP=<your flask file>.py FLASK_ENV=development && flask run

you can see in documentation

When installing flask I saw an error in the end of the installation script saying.

WARNING: The script flask is installed in '/home/doe/.local/bin' which is not on PATH.

by doing echo PATH I indeed verified that that path was not included in my PATH variable.

I typed path+=('/home/doe/.local/bin') and export PATH.

Running echo PATH again now included the needed path and then FLASK_APP=my-app.py flask run worked.

Verify that the flask path is appended to the $PATH env variable

That might be on the ~/.zshrc or ~/.bash_profile file. Edit the file (nano ~/.zshrc) and add it there

export PATH=$PATH:/flaskpath

Then restart the terminal

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.