可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm following the Flask tutorial here:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
I get to the point where I try ./run.py and I get:
Traceback (most recent call last):
File "./run.py", line 3, in <module>
from app import app
File "/Users/benjaminclayman/Desktop/microblog/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
This looks similar to:
ImportError: No module named flask
But their solutions aren't helpful. For reference, I do have a folder named flask which one user mentioned may cause issues.
回答1:
try deleting the virtualenv you created.
create a new virtualenv
virtualenv flask
then
cd flask
let's activate the virtualenv
source bin/activate
now you should see (flask) on the left of the command line.
Let's install flask
pip install flask
Then create a file hello.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
and run it with
python hello.py
UPDATE Flask 1.0.2
With the new flask release there is no need to run the app from your script.
hello.py should look like this now:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
and run it with
FLASK_APP=hello.py flask run
make sure to be inside the folder where hello.py is when running the latest command.
All the steps before the creation of the hello.py apply for this case as well
回答2:
For python 3 use
pip3 install flask
回答3:
The only way I could solve was by adding my users python dir to myapp.wsgi file. As an example:
sys.path.append('/home/deployer/anaconda3/lib/python3.5/site-packages')
I guess that if you install the packages in the global enviroment, you should have no problem, but I had my python packages installed as user.
回答4:
I had a similar problem with flasgger.
The reason for that was that I always use
sudo pip install flask
but for some reason that's not always the way to go.
Sometimes, you have to do just
pip install flask
Another gotcha is that sometimes people type pip install Flask
with the cap F
Posting this here in case somebody gets stuck.
Let me know if it helped.
Useful Link:
What is the difference between pip install and sudo pip install?
回答5:
I was using python2 but installed this:
sudo apt-get install libapache2-mod-wsgi-py3
Instead of:
sudo apt-get install libapache2-mod-wsgi
Correcting the installation solved the no flask problem.
回答6:
- Edit
/etc/apache2/sites-available/FlaskApp.conf
- Add the following two lines before the "WSGIScriptAlias" line:
WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp
WSGIProcessGroup FlaskApp
- Restart Apache:
service apache2 restart
I'm following the Flask tutorial too.And I met the same problem.I found this way to fix it.
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
回答7:
Go to the flask file in microblog, then activate the virtual environment with source bin/activate
, then go to flask/bin and install flask, and the rest of the packages, pip install flask
. You will see flask listed inside bin directory. Try to run ./run.py
again from microblog (or from wherever you have the file).
回答8:
In my case the solution was as simple as starting up my virtual environment like so:
$ venv/scripts/activate
It turns out I am still fresh to Python :)
回答9:
this is what worked for me,
sudo -H pip install flask
Or for pip3(python3) use :
sudo -H pip3 install flask
Sidenote
If you're using virtualenv it's a good idea to
pip freeze >> requirements.txt
to allow for the installed packages to be listed in one place.
The sudo
command and -H
flag. For more on sudo
's -H
flag, look at Paul's answer.
Hope this helps you.
回答10:
After activating the virtual environment and installing Flask, I created an app.py file. I run it like this : python -m flask run
. Hope this will help!
回答11:
Even i too suggest u virtualenv,
This might also solve ur problem.
sudo apt install python-flask
If u want to deploy in productionserver then go ahead with above solution else use virtualenv.
回答12:
enter your python interactive mode
then:
import sys
sys.path
it will print your path. Check wether flask is installed in the sys.path.
For MacOS, python path is under
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
But pip'll install python package by default under
/Library/Python/2.7/site-packages
That's why it doesn't work for MacOS.
回答13:
It worked for me after upgrading pip:
curl https://bootstrap.pypa.io/get-pip.py | python
Found that answer here: https://stackoverflow.com/a/49748494/3197202
Then I could just install flask:
pip install flask