简体   繁体   English

导入错误:没有名为 flask_wtf 的模块

[英]ImportError: No module named flask_wtf

I am getting the error, ImportError: No module named flask_wtf .我收到错误ImportError: No module named flask_wtf I tried every possible solution.我尝试了所有可能的解决方案。 I installed flask-wrf using -我安装了 Flask-wrf 使用 -

$ flask/bin/pip install flask-wtf

Requirement already satisfied: flask-wtf in ./flask/lib/python2.7/site-packages
Requirement already satisfied: WTForms in ./flask/lib/python2.7/site-packages.

Please help me out with this issue.请帮我解决这个问题。

I am sharing my code which shows error:我正在共享显示错误的代码:

from flask_wtf import Form 
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired
    
class LoginForm(Form):
    openid = StringField('openid', validators=[DataRequired()])
    remember_me = BooleanField('remember_me', default=False)

I also have same issue.我也有同样的问题。 But installing Flask-WTF但是安装 Flask-WTF

  sudo pip install flask-wtf

I was able to resolve this issue.我能够解决这个问题。

Ensure you run the virtual environment's python rather than global python.确保您运行虚拟环境的 python 而不是全局 python。 I resolved this in my windows env through executing:我通过执行在我的 Windows 环境中解决了这个问题:

\inetpub\projectFolder\scripts\python.exe run.py

rather than just而不仅仅是

python run.py

Also, make sure that you have installed the modules to the virtual environment rather than globally.此外,请确保您已将模块安装到虚拟环境中,而不是全局安装。 Check that the modules exist appropriately - again under windows I checked for the file:检查模块是否正确存在 - 再次在 windows 下我检查了文件:

\inetpub\projectFolder\lib\site-packages\wtforms 

This is certainly a problem with virtual environment.这肯定是虚拟环境的问题。 I had the same problem and managed to get rid of errors with sudo pip install Flask-WTF while my virtual environment was active.我遇到了同样的问题,并在我的虚拟环境处于活动状态时设法摆脱了sudo pip install Flask-WTF的错误。

This solution, however, has got me into another package (email_validator) importing errors, though I was sure everything is installed as it should be.然而,这个解决方案让我进入了另一个包 (email_validator) 导入错误,尽管我确信一切都按原样安装。 Now, instead of running flask run from terminal, I decided to run the whole application as it, by typing python3 application.py in my app directory, where application.py is my flask app.现在,我决定不再从终端运行flask run ,而是通过在我的应用程序目录中键入python3 application.py来运行整个应用程序,其中application.py 是我的flask 应用程序。

In this way of running application I haven't come into any errors at all.以这种运行应用程序的方式,我根本没有遇到任何错误。 If you want to run your app like me, be sure to set following conditional at the bottom of your application:如果您想像我一样运行您的应用程序,请务必在应用程序底部设置以下条件:

if __name__ == '__main__':
    app.run(debug=True)

(This will run your application in debug mode.) (这将在调试模式下运行您的应用程序。)

I'm beginner level developer.我是初级开发人员。 It would be really nice for someone more experienced to explain logic behind this.让更有经验的人来解释这背后的逻辑会非常好。

Edit: Once I ran my application like this, suddenly I can run it with flask run again, without any errors.编辑:一旦我像这样运行我的应用程序,突然间我可以再次使用flask run运行它,没有任何错误。 Interesting.有趣。

In my case it worked after I installed it using pip3 instead of just pip.在我的情况下,它我使用 pip3 而不仅仅是 pip 安装它工作。 I didn't look into it enough to find out what was different but I'm seriously thinking of linking pip to pip3 in my venv.我没有足够研究它以找出不同之处,但我正在认真考虑在我的 venv 中将 pip 链接到 pip3。

I've had the same problem.我遇到了同样的问题。 The problem in my case was that Flask was installed globally , so flask run called the global package that didn't recognise the local package (Probably because the global call to python called python2, and my env was set to python3) .在我的例子中的问题是 Flask 是全局安装的,所以flask run调用了无法识别本地包的全局包(可能是因为对 python 的全局调用称为 python2,而我的 env 设置为 python3)。 I solved it by uninstalling Flask globally and installing it only in the virtual environment.我通过全局卸载 Flask 并仅在虚拟环境中安装它来解决它。

使用 pip 安装 Flask-WTF 很简单:

pip install Flask-WTF

i did simple installation from flask_wtf and it worked the command is following我从flask_wtf做了简单的安装,它的工作命令如下

pip install Flask-WTF pip 安装 Flask-WTF

please go to this link for more info https://flask-wtf.readthedocs.io/en/stable/install.html请转到此链接以获取更多信息https://flask-wtf.readthedocs.io/en/stable/install.html

我遇到了同样的问题 使用以下方法解决了它

conda install -c wakari flask-wtf

Note that if you are using Anaconda's python to run your app then you have to pip install flask-wtf from the Anaconda prompt and not from general command prompt.请注意,如果您使用 Anaconda 的 python 来运行您的应用程序,那么您必须从 Anaconda 提示符而不是从一般命令提示符pip install flask-wtf This helped me when I encountered this problem.当我遇到这个问题时,这对我有帮助。 Hope this helps!希望这可以帮助!

You could install flask_wtf in conda using this command您可以使用此命令在 conda 中安装 flask_wtf

conda install -c anaconda flask-wtf

Since your problem is due to virtualenv, installing this on conda will solve the problem.由于您的问题是由于 virtualenv 造成的,因此在 conda 上安装它可以解决问题。 It did work for me.它确实对我有用。

就我而言,它是 python 版本,我尝试了上述所有命题​​,我将 python 版本从 3 更改为 2,并且它运行良好。

python2 app.py

Extending the @pawelduzy's answer adding a app.run(debug=True) resolved the same issue in my virtual environement where Flask-WTF extension was already installed via pip install flask-wtf .扩展@pawelduzy 的答案添加app.run(debug=True)解决了我的虚拟环境中的相同问题,其中 Flask-WTF 扩展已经通过pip install flask-wtf

This is the content of my '__init__.py' file这是我的'__init__.py'文件的内容

from flask import Flask
from config import Config

app = Flask(__name__)
app.config.from_object(Config)
app.run(debug=True)

from app import routes

Alternatively you may insert the app.run(debug=True) statement into a .py -file that launches the main application或者,您可以将app.run(debug=True)语句插入到启动主应用程序的.py文件中

from app import app
app.run(debug=True)

But be aware of what is mentioned in the Flask's Docs, namely "Debugging Application Errors"但请注意 Flask 文档中提到的内容,即“调试应用程序错误”

Having problems getting your application configured for production?在为生产配置应用程序时遇到问题? If you have shell access to your host, verify that you can run your application manually from the shell in the deployment environment.如果您拥有对主机的 shell 访问权限,请验证您是否可以从部署环境中的 shell 手动运行您的应用程序。 Be sure to run under the same user account as the configured deployment to troubleshoot permission issues.确保在与配置的部署相同的用户帐户下运行以解决权限问题。 You can use Flask's builtin development server with debug=True on your production host, which is helpful in catching configuration issues, but be sure to do this temporarily in a controlled environment.您可以在生产主机上使用带有debug=True Flask 内置开发服务器,这有助于捕获配置问题,但请务必在受控环境中临时执行此操作。 Do not run in production with debug=True .不要在生产环境中使用debug=True

Had the same error "ModuleNotFoundError: No module named 'flask_wtf'".有同样的错误“ModuleNotFoundError:没有名为‘flask_wtf’的模块”。 This worked for me:这对我有用:

  1. Open Anaconda - install flask_wtf in using this command in the Anaconda terminal:打开 Anaconda - 在 Anaconda 终端中使用以下命令安装 flask_wtf:

     conda install -c anaconda flask-wtf
  2. In Pycharm go to preferences/project/python interpreter.在 Pycharm 中,转到首选项/项目/python 解释器。 Click the "+" button on the lower left then search for "flask-wtf. Then install the package.点击左下角的“+”按钮,然后搜索“flask-wtf。然后安装包。

The second step was not possible without the first, since flask-wtf didn't show up in the list of available packages.没有第一步,第二步是不可能的,因为flask-wtf没有出现在可用包列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM