在虚拟环境中安装flask_如何在虚拟环境中安装Flask

本文介绍了如何在Python 3的虚拟环境中安装Flask。首先确保不在现有虚拟环境中,并创建新的虚拟环境。激活环境后,使用`python3 -m venv env`命令检查。接着,通过`pip install flask`安装Flask,确保Django不在环境中。为了方便管理,可以创建`requirements.txt`文件记录依赖,并使用`pip install -r requirements.txt`安装所有包。该教程适用于Linux、Mac OS或使用PowerShell的Windows环境。
摘要由CSDN通过智能技术生成

在虚拟环境中安装flask

If you wish to use Flask, you are in the right place! This guide will teach you how to install Flask if you want to explore web development with it.

如果您想使用Flask,那么您来对地方了! 本指南将教您如何使用Flask安装Web开发。

Just keep in mind that Flask might not always be the best choice – it gets difficult to build large web-applications with it if you are new to web development in Python. Perhaps check out Django as another option.

请记住,Flask可能并不总是最好的选择–如果您不熟悉Python Web开发,将很难用它来构建大型Web应用程序。 也许签出Django作为另一种选择。

Flask is a micro-framework and you can pick the functionality you wish to have over the basic barebones functionality you already have from a standard web-framework.

Flask是一个微框架,您可以从标准Web框架中选择希望拥有的功能,而不是已经拥有的基本准系统功能。

First make sure you've installed Python 3 and are using it inside a virtual environment.

首先,请确保您已安装Python 3并在虚拟环境中使用它。

Also, make sure that you are not inside a virtual environment already. Then create a new virtual environment, named py3-flask

另外,请确保您尚未位于虚拟环境中。 然后创建一个新的虚拟环境,名为py3-flask

$ mkvirtualenv py3-flask --python=/usr/bin/python3

Now, execute the workon command to see a list of virtual environments in your machine. This should list py3-flask in a line.

现在,执行workon命令以查看计算机中虚拟环境的列表。 这应该在一行py3-flask列出py3-flask

After this, activate this environment:

之后,激活此环境:

$ workon py3-flask

Your virtual environment will be activated with a copy of Python interpreter, with Python 3 properties. You should run

您的虚拟环境将通过具有Python 3属性的Python解释器的副本激活。 你应该跑

$ python --version

to ensure that you are indeed inside a Python 3 environment.

以确保您确实在Python 3环境中。

Just to be clear, if you have already installed Django or some other framework, it should not be in this environment. We are using a virtual environment to keep our installation of different frameworks separated.

只是要清楚,如果你已经安装的Django或一些其他的框架,它应该是在这样的环境。 我们正在使用虚拟环境来保持不同框架的安装分离。

To be sure, run

可以肯定的是,运行

pip freeze

Make sure Django is not listed in the output list generated by above command.

确保Django未在上述命令生成的输出列表中列出。

Now, let’s install Flask. If you want to learn more, here’s the official installation guide. However, a lot of developers prefer installing some extra packages with Flask for more functionality.

现在,让我们安装Flask。 如果您想了解更多信息,请参阅官方安装指南 。 但是,许多开发人员更喜欢在Flask上安装一些额外的软件包以获取更多功能。

To install just Flask, execute

要仅安装Flask,请执行

$ pip install flask

When you run pip freeze again, it should show you Flask in listed packages.

当您再次运行pip freeze时,它会在列出的软件包中显示Flask

It is cumbersome running long commands like this. Fortunately, there is something like package.json in the Python domain as well - a list of dependencies, which the package manager can use to duplicate the environment by downloading them with the proper version from the central repo.

运行这样的长命令很麻烦。 幸运的是,在Python域中也有类似package.json东西-依赖项列表,程序包管理器可通过从中央存储库下载正确版本的依赖项来复制环境。

The standard is to use pip freeze and log the output to a local file, which can be source controlled.

标准是使用pip freeze并将输出记录到本地文件中,该文件可以由源代码控制。

$ pip freeze > requirements.txt

Here’s the content of requirements.txt from my environment, after installing those Flask packages. You may add or remove more packages as your application grows. But for now, just copy and paste the content of the following in a text file in the same directory as you are in.

这是安装这些Flask软件包后来自我的环境的requirements.txt的内容。 您可以随着应用程序的增长添加或删除更多软件包。 但是现在,只需将以下内容复制并粘贴到与您所在目录相同的文本文件中即可。

Babel==2.2.0
Flask==0.10.1
Flask-Babel==0.9
Flask-Login==0.3.2
Flask-Mail==0.9.1
Flask-OpenID==1.2.5
Flask-SQLAlchemy==2.1
Flask-WTF==0.12
Flask-WhooshAlchemy==0.56
Jinja2==2.8
MarkupSafe==0.23
SQLAlchemy==1.0.12
Tempita==0.5.2
WTForms==2.1
Werkzeug==0.11.4
Whoosh==2.7.2
blinker==1.4
coverage==4.0.3
decorator==4.0.9
defusedxml==0.4.1
flipflop==1.0
guess-language==0.2
itsdangerous==0.24
pbr==1.8.1
python3-openid==3.0.9
pytz==2015.7
six==1.10.0
speaklater==1.3
sqlalchemy-migrate==0.10.0
sqlparse==0.1.18

This list of packages are taken from here.

此软件包列表从此处获取

Once you have saved the file, just run

保存文件后,只需运行

$ pip install -r requirements.txt

The package manager will take care of installing the missing packages for you! And you should commit this file with your source control system.

软件包管理器将为您安装缺少的软件包! 并且您应该将此文件提交给源代码控制系统。

The above set of commands assumes that you have a Linux machine or Mac OSX machine. Or that you are using a cloud-hosted box on cloud9 or Nitrous, or maybe you are using a Vagrant box.

上面的命令集假定您拥有Linux机器或Mac OSX机器。 或者您正在cloud9或Nitrous上使用云托管的框,或者您正在使用“无业游民”框。

But, if you have to use a Windows machine, do consider using Windows Powershell, instead of Windows CMD. Most of the commands will be same. In case you need any assistance, you might want to check out this Stack Overflow discussion.

但是,如果必须使用Windows计算机,请考虑使用Windows Powershell而不是Windows CMD。 大多数命令将是相同的。 如果您需要任何帮助,则可能需要查看此Stack Overflow讨论

翻译自: https://www.freecodecamp.org/news/how-to-install-flask-in-a-virtual-environment/

在虚拟环境中安装flask

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值