Flask ImportError:没有名为Flask的模块

问题:Flask ImportError:没有名为Flask的模块

我在这里关注Flask教程:

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

我到了尝试./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

看起来类似于:

ImportError:没有名为flask的模块

但是他们的解决方案没有帮助。作为参考,我确实有一个名为flask的文件夹,一个用户提到该文件夹​​可能会引起问题。

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.


回答 0

尝试删除您创建的virtualenv。然后使用以下命令创建一个新的virtualenv:

virtualenv flask

然后:

cd flask

现在让我们激活virtualenv:

source bin/activate

现在,您应该(flask)在命令行左侧看到。

让我们安装烧瓶:

pip install flask

然后创建一个名为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()

并运行:

python hello.py

更新Flask 1.0.2

使用新的flask版本,无需从脚本中运行该应用程序。 hello.py现在应该看起来像这样:

from flask import Flask
app = Flask(__name__)

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

并运行:

FLASK_APP=hello.py flask run

hello.py运行最新命令时,请确保位于其中的文件夹中。

创建hello.py之前的所有步骤同样适用于这种情况

Try deleting the virtualenv you created. Then create a new virtualenv with:

virtualenv flask

Then:

cd flask

Now let’s activate the virtualenv

source bin/activate

Now you should see (flask) on the left of the command line.

Edit: In windows there is no “source” that’s a linux thing, instead execute the activate.bat file, here I do it using Powershell: PS C:\DEV\aProject> & .\Flask\Scripts\activate)

Let’s install flask:

pip install flask

Then create a file named hello.py (NOTE: see UPDATE Flask 1.0.2 below):

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


回答 1

对于python 3使用

pip3安装烧瓶

For python 3 use

pip3 install flask


回答 2

我唯一可以解决的方法是将用户python目录添加到myapp.wsgi文件中。举个例子:

sys.path.append('/home/deployer/anaconda3/lib/python3.5/site-packages')

我猜想,如果将软件包安装在全局环境中,应该没有问题,但是我已经以用户身份安装了python软件包。

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.


回答 3

激活虚拟环境并安装Flask之后,我创建了一个app.py文件。我这样运行:python -m flask run。希望这会有所帮助!

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!


回答 4

我对flasgger也有类似的问题。

原因是我经常使用

sudo pip install flask

但是由于某些原因,这并不总是可行的。有时候,你必须要做的只是

pip install flask

另一个陷阱是有时人们会pip install Flask大写字母F键入

如果有人卡住,请将其张贴在这里。让我知道是否有帮助。

有用的链接: pip install和sudo pip install有什么区别?

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

这对我有用

sudo -H pip install flask

或者对于pip3(python3)使用:

sudo -H pip3 install flask

边注

如果您使用的是virtualenv,则最好 pip freeze >> requirements.txt 将已安装的软件包放在一个位置。该sudo命令和-H标志。欲了解更多有关sudo-H标志,看看保罗的 回答。希望对您有帮助。

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.


回答 6

我正在使用python2,但安装了它:sudo apt-get install libapache2-mod-wsgi-py3

代替:sudo apt-get install libapache2-mod-wsgi

纠正安装解决了no flask问题。

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.


回答 7

就我而言,解决方案就像启动虚拟环境一样简单:

$ venv/scripts/activate

事实证明,我对Python还是很新鲜的:)

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 :)


回答 8

  1. 编辑 /etc/apache2/sites-available/FlaskApp.conf
  2. 在“ WSGIScriptAlias”行之前添加以下两行:

WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp WSGIProcessGroup FlaskApp

  1. 重新启动Apache:service apache2 restart

我也遵循了Flask教程,并且遇到了同样的问题,我找到了解决问题的方法。

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

  1. Edit /etc/apache2/sites-available/FlaskApp.conf
  2. Add the following two lines before the “WSGIScriptAlias” line:

WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp WSGIProcessGroup FlaskApp

  1. 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


回答 9

转到微博客中的flask文件,然后使用激活虚拟环境source bin/activate,然后转到flask / bin并安装flask,以及其余的软件包pip install flask。您会在bin目录中看到烧瓶。尝试./run.py从微博客(或任何有文件的地方)再次运行。

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).


回答 10

即使我也建议您使用virtualenv,这也可以解决您的问题。

sudo apt install python-flask

如果您想在生产服务器中进行部署,则继续上述解决方案,否则请使用virtualenv。

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.


回答 11

当我在Windows中遇到类似错误时,这对我有用。1.安装virtualenv

pip install virtualenve
  1. 创建一个virtualenv

    虚拟环境瓶

  2. 导航到脚本并激活virtualenv

    启用

  3. 安装烧瓶

    python -m pip安装烧瓶

  4. 检查烧瓶是否已安装

    python -m pip列表

This is what worked for me when I got a similar error in Windows; 1. Install virtualenv

pip install virtualenve
  1. Create a virtualenv

    virtualenv flask

  2. Navigate to Scripts and activate the virtualenv

    activate

  3. Install Flask

    python -m pip install flask

  4. Check if flask is installed

    python -m pip list


回答 12

输入您的python交互模式,然后:

import sys

sys.path

它将打印您的路径。检查sys.path中是否安装了保温瓶。

对于MacOS,python路径位于/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages下

但是pip’ll默认会在/Library/Python/2.7/site-packages下安装python软件包

这就是为什么它不适用于MacOS。

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

flask脚本很适合启动本地开发服务器,但是每次更改代码后,都必须手动重新启动它。那不是很好,Flask可以做得更好。如果启用调试支持,则服务器将在代码更改时重新加载自身,如果出现问题,它还将为您提供有用的调试器。要启用调试模式,您可以在运行服务器之前导出FLASK_DEBUG环境变量:例如您的文件是hello.py

$ export FLASK_APP=hello.py
$ export FLASK_DEBUG=1
$ flask run

The flask script is nice to start a local development server, but you would have to restart it manually after each change to your code. That is not very nice and Flask can do better. If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong. To enable debug mode you can export the FLASK_DEBUG environment variable before running the server: forexample your file is hello.py

$ export FLASK_APP=hello.py
$ export FLASK_DEBUG=1
$ flask run

回答 14

在我使用Docker的情况下,未复制我的.env文件,因此未设置以下env变量:

.env.local: FLASK_APP=src/app.py

所以在我中Dockerfile我必须包括:

FROM deploy as dev
COPY env ./env

在docker-compose.yml中引用了

env_file: ./env/.env.local

我必须注意的另一件事是path变量,以确保使用我的环境

ENV PATH $CONDA_DIR/envs/:my_environment_name_from_yml_file:/bin:$CONDA_DIR/bin:$PATH```

in my case using Docker, my .env file was not copied, so the following env vars were not set:

.env.local: FLASK_APP=src/app.py

so in my Dockerfile i had to include:

FROM deploy as dev
COPY env ./env

which was referenced in docker-compose.yml

env_file: ./env/.env.local

another thing i had to pay attention to is the path variable to ensure my environment is used

ENV PATH $CONDA_DIR/envs/:my_environment_name_from_yml_file:/bin:$CONDA_DIR/bin:$PATH```

回答 15

我的回答仅适用于使用Visual Studio Flesk Web项目的所有用户:

只需右键单击“ Python环境”,然后单击“添加环境”

my answer just for any users that use Visual Studio Flesk Web project :

Just Right Click on “Python Environment” and Click to “Add Environment”


回答 16

如果您使用的是Pycharm,则这是虚拟环境问题。

因此,在创建Python项目时,您必须选择“现有解释器”选项->单击“系统解释器”->选择正确的选项,例如“ * \ AppData \ Local \ Programs \ Python \ Python3.6 \ python.exe”。

您也可以使用“ New Virtual Env”,但我已经给出了适用于Pycharm用户的快速修复程序。

If you are using Pycharm then this is the virtual environment issue.

So, at the time of creating your Python project you will have to select “Existing interpreter” option -> click “system Interpreter” -> select the correct option for example “*\AppData\Local\Programs\Python\Python3.6\python.exe”.

You can use ‘New Virtual Env’ as well, but I have just given the quick fix that should work for Pycharm users.


回答 17

另一件事-如果你使用python3,请确保您正在使用启动服务器python3 server.py,而不是python server.py

Another thing – if you’re using python3, make sure you are starting your server with python3 server.py, not python server.py


回答 18

升级点数后对我有用:

curl https://bootstrap.pypa.io/get-pip.py | python

在这里找到答案:https : //stackoverflow.com/a/49748494/3197202

然后我可以安装flask:

pip install flask

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