简体   繁体   English

没有使用 virtualenv 的名为烧瓶的模块

[英]No module named flask using virtualenv

I am following these steps to learn flask http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world/page/0#comments我正在按照这些步骤学习烧瓶http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world/page/0#comments

I ran this command to create the virtual env:我运行这个命令来创建虚拟环境:

python virtualenv.py flask

When I try to start flask using the python.exe file in my project scripts directory, it says当我尝试使用项目脚本目录中的 python.exe 文件启动烧瓶时,它说

No module named flask

My PATH is set to the python directory that virtualenv installed.我的 PATH 设置为 virtualenv 安装的 python 目录。 Why is it not able to find flask?为什么找不到flask?

I initially started with the official Flask quickstart guide and was able to get the webserver to run, but with this virtual env install it is not working.我最初从官方 Flask 快速入门指南开始,并且能够让网络服务器运行,但是使用这个虚拟 env 安装它不起作用。

Make sure your virtualenv is activated.确保您的 virtualenv 已激活。 Then You check on the PYTHONPATH of that virtualenv.然后你检查那个 virtualenv 的PYTHONPATH Is there a flask package (folder) installed in that directory.该目录中是否安装了烧瓶包(文件夹)。

If you unsure whether you have installed flask, just run the following command to see all the packages you have installed pip list or pip show flask .如果不确定是否安装了flask,只需运行以下命令即可查看已安装的所有包pip listpip show flask Do you see flask there?你看到那里的烧瓶了吗? If not you can run pip install flask如果没有,您可以运行pip install flask

This error can also appear if you start your Flask python server using ./run.py or similarly use file associations to start your server.如果您使用./run.py启动 Flask python 服务器或类似地使用文件关联来启动服务器,也会出现此错误。 Then the python command in the association will be used instead of your virtual environment python command.然后将使用关联中的 python 命令而不是你的虚拟环境 python 命令。 Use python run.py instead.改用python run.py See how my run.py innocently assumes /usr/bin/python?看看我的 run.py 是如何无辜地假设 /usr/bin/python 的?

#!/usr/bin/python
# run.py
from app import app
app.run(debug=True,host='0.0.0.0',port=5000)

I had this same problem on three Raspberry Pi units at the same time;我同时在三个 Raspberry Pi 单元上遇到了同样的问题; beat my head against the wall trying to fix it for several hours (reinstall flask via pip, apt and aptitude - no joy).把我的头撞在墙上试图修复它几个小时(通过 pip、apt 和 aptitude 重新安装烧瓶 - 没有乐趣)。

Instead of:而不是:

pip install flask

I finally tried:我终于试过了:

pip install Flask

Worked like a charm.像魅力一样工作。

Make sure you run your script after you've activated your virtualenv.确保在激活 virtualenv运行脚本。 On OS X you'd see (virtual_env_name) at the start of each terminal line.在 OS X 上,您会在每个终端行的开头看到(virtual_env_name) To do this:去做这个:

cd to your virtualenv's directory and type . bin/activate cd到您的 virtualenv 目录并键入. bin/activate . bin/activate

cd to the directory containing the .py file you want to run at app launch in the browser cd到包含要在浏览器中启动应用程序时运行的.py文件的目录

Now enter python file_name.py , for me the file name was routes.py following this example现在输入python file_name.py ,对我来说,文件名是routes.py下面这个例子

This problem can also arise if the port is not available.如果端口不可用,也会出现此问题。 Try running on different port.尝试在不同的端口上运行。

Activate your virtual environment first with首先激活您的虚拟环境

source bin/activate envName

Then try to run your command again然后尝试再次运行您的命令

If nothing else helps, check that in your code it is:如果没有其他帮助,请检查您的代码是否为:

from flask import Flask

I've tried many things before I've noticed my mistake.在我注意到我的错误之前,我已经尝试了很多事情。 I had this in my code:我的代码中有这个:

from Flask import Flask

When I have changed capitalized letter for the module name, ie flask then everything worked.当我更改了模块名称的大写字母时,即flask然后一切正常。

On Windows even if you see (virtual_env_name) in the cmd line it is possible that the virtual environment is not completely activated.在 Windows 上,即使您在 cmd 行中看到 (virtual_env_name),也可能虚拟环境未完全激活。 Deactivate/reactivate and try again.停用/重新激活并重试。

I am running Python on windows 7. I had same problem No module named flask.我在 Windows 7 上运行 Python。我遇到了同样的问题 没有名为flask 的模块。

I tried reinstalling python, venv but it did not work.我尝试重新安装 python、venv,但没有用。

Finally i run it like this最后我像这样运行它

  1. Install venv the usual way以通常的方式安装 venv
  2. go to scripts directory and activate转到脚本目录并激活
  3. C:\\Python34\\microb>c:\\Python34\\microb\\fla\\scripts\\python run.py C:\\Python34\\microb>c:\\Python34\\microb\\fla\\scripts\\python run.py
  4. here microb is my project and fla is venv这里 microb 是我的项目,fla 是 venv

In Python 3.x在 Python 3.x 中

pip3 install flask

Worked fine for me.对我来说效果很好。

Thanks & Regards感谢和问候

For those of you on Windows who are running into this problem, have activated your venv and flask is installed in the right directory.对于那些在 Windows 上遇到此问题的人,请激活您的 venv 并将烧瓶安装在正确的目录中。 For me I realized it was looking for flask but the file was called flask.exe .对我来说,我意识到它正在寻找烧瓶,但该文件名为flask.exe I renamed it and it worked perfectly.我重命名了它,它工作得很好。

Try below lines :尝试以下几行:

$ python3.7 -m venv env 

$ source env/bin/activate

(env)$ pip install yourpackages

(env)$ python app.py

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

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