インストールについて

Flaskは、 WerkzeugJinja2 の2つの外部ライブラリに依存しています。Werkzeugは、WSGIのツールキットで、各種Webアプリサーバや 開発サーバーの両方で動作します。Jinja2は、レンダーテンプレートです。

早速PCに環境構築したいですか? いろんなインストール方法がありますが、以下でVirtualEnvを用いて 環境構築する方法を紹介します。

virtualenv

Virtualenvは、Shell上でプロジェクト毎に個別の環境が構築出来るナイスなツールです。

virtualenvの環境構築を見てみましょう!

お使いのOSが、Mac OSX か Linuxな方は、以下の2つのコマンドのどちらかでインストール出来ます

$ sudo easy_install virtualenv

こちらも同様以上の事が可能です:

$ sudo pip install virtualenv

Ubuntuであれば以下のように指定インストール出来ます

$ sudo apt-get install python-virtualenv

Windowsをお使いの方は、まず easy_install をインストールしてください。 easy_install on Windows が参考になるかも知れません。インストールが 完了したら上記コマンドを実行することが可能です。( sudo の記述は省いてください)

以上で、Virtualenvはインストール出来ました。早速環境を作っていきます。プロジェクト フォルダー myproject 内にenvという環境を作ります:

$ mkdir myproject
$ cd myproject
$ virtualenv env
New python executable in env/bin/python
Installing setuptools............done.

この環境を使用するようにアクティブ化する必要があります。OS XとLinuxでは以下のコマンドを実行します

$ . env/bin/activate

(Note the space between the dot and the script name. The dot means that this script should run in the context of the current shell. If this command does not work in your shell, try replacing the dot with source)

Windows環境であれば、以下のコマンドを実行します:

$ env\scripts\activate

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

Now you can just enter the following command to get Flask activated in your virtualenv:

$ easy_install Flask

A few seconds later you are good to go.

System Wide Installation

This is possible as well, but I do not recommend it. Just run easy_install with root rights:

$ sudo easy_install Flask

(Run it in an Admin shell on Windows systems and without sudo).

Living on the Edge

If you want to work with the latest version of Flask, there are two ways: you can either let easy_install pull in the development version, or tell it to operate on a 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/mitsuhiko/flask.git
Initialized empty Git repository in ~/dev/flask/.git/
$ cd flask
$ virtualenv env
$ . env/bin/activate
New python executable in env/bin/python
Installing setuptools............done.
$ 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 you just have to git pull origin to get the latest version.

To just get the development version without git, do this instead:

$ mkdir flask
$ cd flask
$ virtualenv env
$ . env/bin/activate
New python executable in env/bin/python
Installing setuptools............done.
$ easy_install Flask==dev
...
Finished processing dependencies for Flask==dev

easy_install on Windows

On Windows, installation of easy_install is a little bit tricker because slightly different rules apply on Windows than on Unix-like systems, but it’s not difficult. The easiest way to do it is to download the ez_setup.py file and run it. The easiest way to run the file is to open your downloads folder and double-click on the file.

Next, add the easy_install command and other Python scripts to the command search path, by adding your Python installation’s Scripts folder to the PATH environment variable. To do that, right-click on the “Computer” icon on the Desktop or in the Start menu, and choose “Properties”. Then, on Windows Vista and Windows 7 click on “Advanced System settings”; on Windows XP, click on the “Advanced” tab instead. Then click on the “Environment variables” button and double click on the “Path” variable in the “System variables” section. There append the path of your Python interpreter’s Scripts folder; make sure you delimit it from existing values with a semicolon. Assuming you are using Python 2.6 on the default path, add the following value:

;C:\Python26\Scripts

Then you are done. To check that it worked, open the Command Prompt and execute easy_install. If you have User Account Control enabled on Windows Vista or Windows 7, it should prompt you for admin privileges.

Table Of Contents

Related Topics

This Page