安装

Flask依赖两个外部库:WerkzeugJinja2Werkzeug is a toolkit for WSGI, the standard Python interface between web applications and a variety of servers for both development and deployment. Jinja2 renders templates.

So how do you get all that on your computer quickly? There are many ways you could do that, but the most kick-ass method is virtualenv, so let’s have a look at that first.

You will need Python 2.6 or newer to get started, so be sure to have an up-to-date Python 2.x installation. For using Flask with Python 3 have a look at Python 3 Support.

virtualenv

Virtualenv is probably what you want to use during development, and if you have shell access to your production machines, you’ll probably want to use it there, too.

What problem does virtualenv solve? If you like Python as much as I do, chances are you want to use it for other projects besides Flask-based web applications. But the more projects you have, the more likely it is that you will be working with different versions of Python itself, or at least different versions of Python libraries. 悲惨的现实是:常常会有库破坏向后兼容性,然而正常的应用不采用外部库的可能微乎其微。当在你的项目中,出现两个或更多依赖性冲突时,你会怎么做?

virtualenv 拯救世界!Virtualenv enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated. Let’s see how virtualenv works.

If you are on Mac OS X or Linux, chances are that the following command will work for you:

$ sudo pip install virtualenv

It will probably install virtualenv on your system. 它甚至可能会存在于包管理器中。如果你用的是 Ubuntu,可以尝试:

$ sudo apt-get install python-virtualenv

如果你用的是 Windows ,而且没有 easy_install命令,那么你必须先安装这个命令。Check the pip and setuptools on Windows section for more information about how to do that. 之后,运行上述的命令,但是要去掉 sudo前缀。

Once you have virtualenv installed, just fire up a shell and create your own environment. 我通常创建一个项目文件夹,并在其下创建一个venv文件夹:

$ mkdir myproject
$ cd myproject
$ virtualenv venv
New python executable in venv/bin/python
Installing setuptools, pip............done.

Now, whenever you want to work on a project, you only have to activate the corresponding environment. On OS X and Linux, do the following:

$ . venv/bin/activate

If you are a Windows user, the following command is for you:

$ venv\Scripts\activate

Either way, you should now be using your virtualenv (notice how the prompt of your shell has changed to show the active environment).

如果想回到真实的世界,请使用下面的命令:

$ deactivate

之后,shell提示符应该回到和以前一样。

现在,让我们继续。键入以下命令来激活virtualenv中的Flask:

$ pip install Flask

A few seconds later and you are good to go.

全局安装

This is possible as well, though I do not recommend it. 只需要以root权限运行pip

$ sudo pip install Flask

(在 Windows 上,在管理员权限的命令提示符中去掉sudo运行这条命令。)

活在边缘

如果你需要最新版本的Flask,有两种方法:你可以使用pip拉取开发版本, 或让它操作一个git checkout。Either way, virtualenv is recommended.

Get the git checkout in a new virtualenv and run in development mode:

$ git clone http://github.com/pallets/flask.git
Initialized empty Git repository in ~/dev/flask/.git/
$ cd flask
$ virtualenv venv
New python executable in venv/bin/python
Installing setuptools, pip............done.
$ . venv/bin/activate
$ python setup.py develop
...
Finished processing dependencies for Flask

This will pull in the dependencies and activate the git head as the current version inside the virtualenv. Then all you have to do is run git pull origin to update to the latest version.

Windows下的pipsetuptools

有时候,获取标准的“Python包工具”如pip, setuptoolsvirtualenv可能有点麻烦,但一点也不难。关键的包是pip,其它的包(如virtualenv)都需要它安装。幸运的是,有个“安装脚本”可以直接运行来安装。

如果你当前没有pip,那么 get-pip.py将会为你安装一个。

get-pip.py

它下载下来后应该可以双击。如果你已经安装pip,可以通过执行以下命名来升级它们:

> pip install --upgrade pip setuptools

在大部分时候,一旦打开命令行你就能够输入pippython来运行,但是在Windows上可能不行,因为它不知道这些可执行文件的位置(可以试一下!).

若要修掉这个问题,你应该能够浏览到Python的安装目录(如C:Python27),然后是Tools,再然后是Scripts,最后找到win_add2path.py文件并运行它。打开一个新的命令行,检查一下可以只用输入python就可以调用解释器。

最后,你可以简单地运行来安装virtualenv

> pip install virtualenv

然后,你就可以结束上面的安装指南了。