安装¶
Python 版本¶
我们建议使用最新版本的 Python 3. Flask 支持 Python 3.4 及更新版本,Python 2.7 和 PyPy。
依赖¶
安装 Flask 时将自动安装这些发行版。
- Werkzeug实现了 WSGI,它是应用程序和服务器之间的标准 Python 接口。
- Jinja是一种模板语言,用于呈现应用程序所服务的页面。
- MarkupSafe伴随着 Jinja。它在渲染模板时逃避不受信任的输入以避免注入攻击。
- ItsDangerous安全地签署数据以确保其完整性。这用于保护 Flask 的会话 cookie。
- Click是一个用于编写命令行应用程序的框架。 它提供
flask
命令并允许添加自定义管理命令。
可选的依赖项¶
这些发行版不会自动安装。 如果安装它们,Flask 将检测并使用它们。
- Blinker为[signals]提供支持。
- SimpleJSON是一个快速的 JSON 实现,它与 Python 的
json
模块兼容。如果安装了 JSON 操作,则首选它。 - python-dotenv在运行[flask]命令时支持[dotenv]。
- Watchdog为开发服务器提供了更快,更高效的重新加载器。
虚拟环境¶
使用虚拟环境在开发和生产中管理项目的依赖关系。
What problem does a virtual environment solve? The more Python projects you have, the more likely it is that you need to work with different versions of Python libraries, or even Python itself. Newer versions of libraries for one project can break compatibility in another project.
Virtual environments are independent groups of Python libraries, one for each project. Packages installed for one project will not affect other projects or the operating system's packages.
Python 3 comes bundled with the :mod:venv
module to create virtual environments.
If you're using a modern version of Python, you can continue on to the next section.
If you're using Python 2, see :ref:install-install-virtualenv
first.
创建一个环境¶
Create a project folder and a :file:venv
folder within:
$ mkdir myproject
$ cd myproject
$ python3 -m venv venv
On Windows:
$ py -3 -m venv venv
If you needed to install virtualenv because you are using Python 2, use the following command instead:
$ python2 -m virtualenv venv
On Windows:
> \Python27\Scripts\virtualenv.exe venv
激活环境¶
Before you work on your project, activate the corresponding environment:
$ . venv/bin/activate
On Windows:
> venv\Scripts\activate
Your shell prompt will change to show the name of the activated environment.
安装烧瓶¶
Within the activated environment, use the following command to install Flask:
$ pip install Flask
Flask is now installed. Check out the :doc:/quickstart
or go to the
:doc:Documentation Overview </index>
.
生活在边缘¶
If you want to work with the latest Flask code before it's released, install or update the code from the master branch:
$ pip install -U https://github.com/pallets/flask/archive/master.tar.gz
.. install-install-virtualenv:
安装 virtualenv¶
If you are using Python 2, the venv module is not available. Instead,
install virtualenv
.
On Linux, virtualenv is provided by your package manager:
# Debian, Ubuntu $ sudo apt-get install python-virtualenv # CentOS, Fedora $ sudo yum install python-virtualenv # Arch $ sudo pacman -S python-virtualenv
If you are on Mac OS X or Windows, download get-pip.py
, then:
$ sudo python2 Downloads/get-pip.py $ sudo python2 -m pip install virtualenv
On Windows, as an administrator:
> \Python27\python.exe Downloads\get-pip.py > \Python27\python.exe -m pip install virtualenv
Now you can return above and :ref:install-create-env
.