Python 没有名为 flask.ext.wtf 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20032922/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:18:41  来源:igfitidea点击:

No module named flask.ext.wtf

pythonflaskflask-wtforms

提问by liv a

I'm following @Miguel flask mega tutorialwhich is great. In chapter 3 he talks about web forms and flaskWTF extension, installing the extension like this sudo pip install Flask-WTFresulted in

我正在关注@Miguel Flask大型教程,这很棒。在第 3 章中他谈到了 web 表单和flaskWTF 扩展,像这样安装扩展sudo pip install Flask-WTF导致

Successfully installed Flask-WTF Flask WTForms Werkzeug Jinja2 itsdangerous markupsafe

成功安装 Flask-WTF Flask WTForms Werkzeug Jinja2 itsdangerous markupsafe

but when executing ./run.pyi get an error:

但是在执行时./run.py出现错误:

No module named flask.ext.wtf`

没有名为 flask.ext.wtf` 的模块

I've google the error and tried to run it like this: flask/bin/python run.pybut got the same error, also tried flask/bin/activate

我已经谷歌了错误并尝试像这样运行它:flask/bin/python run.py但得到了同样的错误,也尝试过flask/bin/activate

Update:if you run into the same error this is what solved the issue for me I've installed the following, for sure they are not all needed but since i didn't go one by one to find out which one did the trick i'm listing them all

更新:如果你遇到同样的错误,这就是为我解决问题的原因正在列出所有这些

flask/bin/pip install flask-login
flask/bin/pip install flask-openid
flask/bin/pip install flask-mail
flask/bin/pip install sqlalchemy
flask/bin/pip install flask-sqlalchemy
flask/bin/pip install sqlalchemy-migrate
flask/bin/pip install flask-whooshalchemy==0.55a
flask/bin/pip install flask-wtf
flask/bin/pip install pytz
flask/bin/pip install flask-babel
flask/bin/pip install flup

回答by jbub

You are probably using the import style from the older versions:

您可能正在使用旧版本的导入样式:

from flask.ext.wtf import Form, TextField, BooleanField
from flask.ext.wtf import Required

The import style changed starting from 0.9.0 version. Be sure to update your imports:

导入样式从 0.9.0 版本开始更改。请务必更新您的导入:

from flask.ext.wtf import Form
from wtforms.fields import TextField, BooleanField
from wtforms.validators import Required

You can find the note about this change in the upgrade section of docs:

您可以在文档的升级部分找到有关此更改的说明:

https://flask-wtf.readthedocs.org/en/latest/upgrade.html#version-0-9-0

https://flask-wtf.readthedocs.org/en/latest/upgrade.html#version-0-9-0

回答by Doobeh

jbub's right, but is addressing a problem you will run into, rather then the problem you're currently having-- the tutorial is dated, and doesn't mention what versions of the library the user should be using, so if you do as the tutorial says you are going to run into problems unless you change your import lines to match what jbub says.

jbub 是对的,但是正在解决您将遇到的问题,而不是您当前遇到的问题——教程已经过时,并且没有提到用户应该使用的库版本,所以如果你这样做该教程说除非您更改导入行以匹配 jbub 所说的内容,否则您将遇到问题。

But-- the problem you're currently having isn't that-- if it were, your error would be No module named flask.ext.wtf.Textfieldat the moment it can't see the library at all.

但是——你目前遇到的问题不是——如果是的话,你的错误将是No module named flask.ext.wtf.Textfield它根本看不到库。

That means you haven't installed the Flask-WTF library to the correct location, in this case, it looks like it should be installed in your virtual environment, which you seem to have created under a directory called flask, to make things a little clearer, I'm going to refer to that directory as venv.

这意味着您还没有将 Flask-WTF 库安装到正确的位置,在这种情况下,它看起来应该安装在您的虚拟环境中,您似乎在名为 的目录下创建了该库flask,以使事情更清楚一些,我打算将该目录称为venv.

The virtual environment is a little room, you need to step into it, then put things on the shelf (install your libraries). So, first you need to activate your virtual environment, so launch a terminal and navigate to the virtual environment you created (the venvdir), and then activate it (walk into the room):

虚拟环境是一个小房间,你需要进入它,然后把东西放在架子上(安装你的库)。所以,首先你需要激活你的虚拟环境,所以启动一个终端并导航到你创建的虚拟环境(venv目录),然后激活它(走进房间):

source bin/activate

source bin/activate

Your console prompt will change to show you've activated it for this terminal-- now your terminal is standing in the room, so you can install your libraries as required, you don't even need sudoany more, because your little room is safe for your user to play in, it doesn't effect the world outside it's door. So lets install flask-wtf (and any other libraries you may need by asking pip):

您的控制台提示将更改为显示您已为此终端激活它——现在您的终端站在房间里,因此您可以根据需要安装您的库,您甚至不需要sudo更多,因为您的小房间很安全对于您的用户来说,它不会影响门外的世界。所以让我们安装flask-wtf(以及通过询问pip您可能需要的任何其他库):

pip install flask-wtfetc.

pip install flask-wtf等等。

Now, because you're in the virutal environment, it will launch the version of python that exists there and see all the libraries you've set up, which is what you want. So while the terminal prompt shows your virtual environment is still active, you can launch your run.pyfile by running python run.pyand it should solve your issue.

现在,因为您处于虚拟环境中,它将启动存在于那里的 python 版本并查看您设置的所有库,这正是您想要的。因此,当终端提示显示您的虚拟环境仍处于活动状态时,您可以run.py通过运行来启动您的文件python run.py,它应该可以解决您的问题。

回答by olleh

I had the same problem. I read steps 1again to check where I slipped.

我有同样的问题。我再次阅读步骤 1以检查我滑倒的位置。

Remember to install the necessary modules for your project.

请记住为您的项目安装必要的模块。

In this case the following:

在这种情况下:

flask/bin/pip install flask-login
flask/bin/pip install flask-openid
flask/bin/pip install flask-mail
flask/bin/pip install sqlalchemy
flask/bin/pip install flask-sqlalchemy
flask/bin/pip install sqlalchemy-migrate
flask/bin/pip install flask-whooshalchemy==0.55a
flask/bin/pip install flask-wtf
flask/bin/pip install pytz
flask/bin/pip install flask-babel
flask/bin/pip install flup

Since you've created this virtual environment, "flask", all the modules installed are available only in your virtual environment(flask).

由于您已经创建了这个虚拟环境“flask”,因此安装的所有模块仅在您的虚拟环境(flask)中可用。

Remember to run "flask/Scripts/python run.py" and not "python run.py"

记住运行“flask/Scripts/python run.py”而不是“python run.py”

回答by moovon

Even after changing different import styles and re-installing flask, flask-wtf, if it still does not works : then in the config.py("config.py may be of different name eg app_config.py etc)

即使在更改不同的导入样式并重新安装烧瓶、flask-wtf 之后,如果它仍然不起作用:然后在 config.py("config.py 可能具有不同的名称,例如 app_config.py 等)

insert the line

插入行

sys.path.insert(0, os.path.join(os.path.dirname(__file__), 
'venv/Lib/site-packages'))

set the lib path of your own app , in my case it was "venv/Lib/site-packages"

设置你自己的应用程序的 lib 路径,在我的例子中是“venv/Lib/site-packages”

回答by xiu

you can run the command:pip install -U Flask-WTF ,and change to from flask.ext.wtf import Form from wtforms import TextField, BooleanField from wtforms.validators import Required

您可以运行命令:pip install -U Flask-WTF ,并更改为 from flask.ext.wtf import Form from wtforms import TextField, BooleanField from wtforms.validators import Required

回答by Pavel Chernikov

The API has changed from:

API 已更改为:

from flask.ext.wtf import Form

to:

到:

from flask_wtf import Form

See the docs here

请参阅此处的文档

回答by Théo T. Carranza

Do this: . flask/bin/activate

做这个: 。烧瓶/垃圾箱/激活

Then it'll work properly.

然后它会正常工作。

Cheers.

干杯。