Python Flask 导入错误:没有名为 Flask 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31252791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask ImportError: No Module Named Flask
提问by bclayman
I'm following the Flask tutorial here:
我正在关注 Flask 教程:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
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:
我到了尝试 ./run.py 的地步,我得到:
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 dohave a folder named flask which one user mentioned may cause issues.
但他们的解决方案没有帮助。作为参考,我确实有一个名为 flask 的文件夹,一位用户提到可能会导致问题。
采纳答案by Rollback
Try deleting the virtualenv you created. Then create a new virtualenv with:
尝试删除您创建的 virtualenv。然后使用以下命令创建一个新的 virtualenv:
virtualenv flask
Then:
然后:
cd flask
Now let's activate the virtualenv:
现在让我们激活 virtualenv:
source bin/activate
Now you should see (flask)
on the left of the command line.
现在您应该会(flask)
在命令行的左侧看到。
Let's install flask:
让我们安装烧瓶:
pip install flask
Then create a file named hello.py
(NOTE: see UPDATE Flask 1.0.2
below):
然后创建一个名为hello.py
(注意:见UPDATE Flask 1.0.2
下文)的文件:
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
更新烧瓶 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:
使用新的 Flask 版本,无需从脚本运行应用程序。 hello.py
现在应该是这样的:
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.
确保hello.py
在运行最新命令时所在的文件夹内。
All the steps before the creation of the hello.py apply for this case as well
创建 hello.py 之前的所有步骤也适用于这种情况
回答by Julian
The only way I could solve was by adding my users python dir to myapp.wsgi file. As an example:
我能解决的唯一方法是将我的用户 python 目录添加到 myapp.wsgi 文件中。举个例子:
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.
我想如果你在全局环境中安装包,你应该没有问题,但是我以用户身份安装了我的 python 包。
回答by aneesh joshi
I had a similar problem with flasgger.
我对 flasgger 也有类似的问题。
The reason for that was that I always use
原因是我总是使用
sudo pip install flask
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
pip install flask
Another gotcha is that sometimes people type pip install Flask
with the cap F
另一个问题是有时人们pip install Flask
用大写 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?
回答by NingAnMe
- Edit
/etc/apache2/sites-available/FlaskApp.conf
- Add the following two lines before the "WSGIScriptAlias" line:
- 编辑
/etc/apache2/sites-available/FlaskApp.conf
- 在“WSGIScriptAlias”行之前添加以下两行:
WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp
WSGIProcessGroup FlaskApp
WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp
WSGIProcessGroup FlaskApp
- Restart Apache:
service apache2 restart
- 重启阿帕奇:
service apache2 restart
I'm following the Flask tutorial too.And I met the same problem.I found this way to fix it.
我也在关注 Flask 教程。我遇到了同样的问题。我找到了这种方法来解决它。
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
回答by Norman Pilusa
I was using python2 but installed this: sudo apt-get install libapache2-mod-wsgi-py3
我正在使用 python2 但安装了这个: sudo apt-get install libapache2-mod-wsgi-py3
Instead of: sudo apt-get install libapache2-mod-wsgi
而不是: sudo apt-get install libapache2-mod-wsgi
Correcting the installation solved the no flask problem.
更正安装解决了无烧瓶问题。
回答by Miguel
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).
进入微博中的flask文件,然后用 激活虚拟环境source bin/activate
,然后进入flask/bin安装flask,其余的包,pip install flask
。您将看到在 bin 目录中列出了烧瓶。尝试./run.py
从微博(或从任何有文件的地方)再次运行。
回答by Khalid Bin Huda
For python 3 use
对于 python 3 使用
pip3 install flask
pip3 安装烧瓶
回答by yunus
Even i too suggest u virtualenv, This might also solve ur problem.
即使我也建议你使用 virtualenv,这也可能解决你的问题。
sudo apt install python-flask
If u want to deploy in productionserver then go ahead with above solution else use virtualenv.
如果您想在生产服务器中部署,请继续上述解决方案,否则请使用 virtualenv。
回答by Abid
In my case the solution was as simple as starting up my virtual environment like so:
在我的情况下,解决方案就像启动我的虚拟环境一样简单:
$ venv/scripts/activate
$ venv/scripts/activate
It turns out I am still fresh to Python :)
事实证明我对 Python 还很陌生:)
回答by Tao W
enter your python interactive mode then:
进入你的python交互模式然后:
import sys
import sys
sys.path
sys.path
it will print your path. Check wether flask is installed in the sys.path.
它会打印你的路径。检查烧瓶是否安装在 sys.path 中。
For MacOS, python path is under /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
对于 MacOS,python 路径在 /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
但是pip默认会在/Library/Python/2.7/site-packages下安装python包
That's why it doesn't work for MacOS.
这就是它不适用于 MacOS 的原因。