简体   繁体   English

python virtualenv和flask安装。 没有名为flask的模块

[英]python virtualenv and flask installation. No module named flask

I keep getting this error when running the basic program. 运行基本程序时,我不断收到此错误。

ImportError: No module named flask

Here's the basic prog: 这是基本的编:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

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

when I run which Flask on the folder it doesn't return the location. 当我在文件夹上运行哪个Flask时,它不返回位置。 But if I run the pip install from there it says it's already installed. 但是,如果我从那里运行pip安装,它说它已经安装了。

If your want to create a project with Flask and VirtualENV, you should follow the steps below, I sure that you will not meet the error above. 如果要使用Flask和VirtualENV创建项目,则应遵循以下步骤,我确定您不会遇到上述错误。

Step 1: Create project directory and initial virtualenv directory 步骤1:创建项目目录和初始virtualenv目录

mkdir project 
cd project
virtualenv -p /usr/bin/python env

Step 2: Activate your virtual enviroment 步骤2:激活您的虚拟环境

source env/bin/activate

Step 3: Create requirements.txt file and add the content below: 第3步:创建requirements.txt文件并添加以下内容:

Flask

Step 4: Install the packets with PIP 步骤4:使用PIP安装数据包

pip install -r requirements.txt

Step 5: Create your project file. 步骤5:创建您的项目文件。 Eg: run.py 例如: run.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

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

Step 6: Finally, run your app 步骤6:最后,运行您的应用

python run.py

You will have some little works before publishing your code to repositories: init git, create gitignore ... 在将代码发布到存储库之前,您将需要做一些工作:初始化git,创建gitignore ...

I hope you had installed virtualenv and if you have create the virtual environment (virtualenv), you have to use . 我希望您已经安装了virtualenv,并且如果您创建了虚拟环境(virtualenv),则必须使用。 . venv/bin/activate . venv/bin/activate command to activate the enviroment in unix or OSx. . venv/bin/activate命令以在UNIX或. venv/bin/activate中激活环境。 Hope you will get information from this source 希望您能从此来源获得信息

http://flask.pocoo.org/docs/installation/#installation

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

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