简体   繁体   English

ModuleNotFoundError:没有名为“ flask_wtf”的模块

[英]ModuleNotFoundError: No module named 'flask_wtf'

I've found already similar questions here, but could not find specific solution. 我已经在这里找到了类似的问题,但是找不到具体的解决方案。 I've got virtual environment, which is activated and running, and within it I've installed flask-wtf module, as: 我有一个已激活并正在运行的虚拟环境,并且在其中安装了flask-wtf模块,如下所示:

pip install flask-wtf

Then, there is script called "test.py", which contiains from flask_wtf import FlaskForm -> and when it is started from terminal as 然后,有一个名为“ test.py”的脚本,该脚本从flask_wtf导入Containins FlaskForm- >,并从终端启动时以

python test.py

Error is raised, as 引发错误,因为

    from flask_wtf import FlaskForm
ModuleNotFoundError: No module named 'flask_wtf'

Command 命令

which flask_wtf

returns nothing. 什么也不返回。 But, I could see flask_wtf folder in flaskEnv/lib/python3.6/site-packages folder (where flaskEnv - is directory for virtual environment). 但是,我可以在flaskEnv / lib / python3.6 / site-packages文件夹中看到flask_wtf文件夹(其中flaskEnv-是虚拟环境的目录)。

Following the advise, which I've found here on website, running script as 按照我在网站上找到的建议,以

/home/kosist/Documents/Environments/flaskEnv/bin/python test.py

works just fine - script is executed without error. 效果很好-脚本执行无误。 But - why it happens like this? 但是-为什么会这样呢? I don't want to enter all the time full path to python, and moreover, command 我不想一直输入python的完整路径,而且命令

which python

returns the following: 返回以下内容:

/home/kosist/Documents/Environments/flaskEnv/bin/python

so calling python already refers to that path! 因此,调用python已经指向该路径!

Also, script works, if flask-wtf module is installed globally, via 另外,如果通过以下方式全局安装flask-wtf模块,脚本也可以工作

sudo pip install flask-wtf

Then I could run script as 然后我可以运行脚本

python test.py

and everything works. 一切正常。

Could someone, please, help and explain, what is going on, and how to fix it? 有人可以帮忙解释一下,怎么回事,以及如何解决? Because I'm afraid, that I'm missing some simple key point about calling modules from virtualenv, and similar situation could happen to any of the possible installed modules. 因为担心,我缺少从virtualenv调用模块的一些简单要点,并且任何可能安装的模块都可能发生类似情况。

You can verify which packages are installed by using: 您可以使用以下方法验证安装了哪些软件包:

pip freeze

You can also verify that pip is running in a virtual environment by using: 您还可以使用以下方法验证pip是否在虚拟环境中运行:

pip -V

This last command will output the path to the current virtual environment, what I would recommend you to do is first verify that you're actually using the virtual environment, install flask-wtf and then verify that flask-wtf was installed using pip freeze. 最后一条命令将输出到当前虚拟环境的路径,我建议您首先验证您是否正在实际使用虚拟环境,安装flask-wtf,然后验证是否使用pip Frozen安装了flask-wtf。

Finally, the reason was found - I've forgotten, that I had specified alias to python3.6 in ~/.bash_aliases file... So in this case command 最后,找到了原因-我忘记了,我已经在〜/ .bash_aliases文件中为python3.6指定了别名...因此在这种情况下,命令

which python

returned path to virtual environment python, but calling of 返回虚拟环境python的路径,但调用

python

called python3.6, which is installed globally... After ~/.bash_aliases file was removed, everything works. 名为python3.6,已全局安装...删除〜/ .bash_aliases文件后,一切正常。

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

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