インストール可能なプロジェクト(Make the Project Installable) Make the Project Installable

プロジェクトをインストール可能にするということは、配布用(distribution)ファイルを作成でき、このプロジェクトの環境へFlaskをインストールしたときのように、その配布用ファイルを他の環境へインストールできることを意味します。これは、自分のプロジェクトのデプロイを、他のどのライブラリのインストールとも同じような扱いにし、従ってあなたは(このプロジェクトでは)何を管理するときにも全て標準的なPythonのツールを使っています。(訳注: パッケージ管理に関していくつかあるPythonでの標準的なツール・やり方を使って、インストールやアップグレードなどをしています)。 Making your project installable means that you can build a *distribution* file and install that in another environment, just like you installed Flask in your project's environment. This makes deploying your project the same as installing any other library, so you're using all the standard Python tools to manage everything.

インストールするということは、このチュートリアルからは明白ではないかもしれず、新規のPythonユーザにとっても明白ではないかもしれない、以下のような、別の利点もあります。 Installing also comes with other benefits that might not be obvious from the tutorial or as a new Python user, including:

  • この時点では、PythonとFlaskがどうやってflaskrパッケージを使用するか理解している唯一の理由は、プロジェクトのディレクトリから(flask runコマンドなどを)実行しているということだけです。インストールするということは、どこから実行してもそれ(flaskr)をimportできることを意味します。 Currently, Python and Flask understand how to use the ``flaskr`` package only because you're running from your project's directory. Installing means you can import it no matter where you run from.

  • プロジェクトの依存対象を、他のパッケージと同じようなやり方で管理可能になり、従ってpip install yourproject.whlで依存対象をインストールできるようになります。 You can manage your project's dependencies just like other packages do, so ``pip install yourproject.whl`` installs them.

  • テストツールがテスト用の環境を開発用の環境から隔離できるようになります。 Test tools can isolate your test environment from your development environment.

注釈

テスト用の環境は、このチュートリアルでは後の方で紹介されていますが、自分の将来のプロジェクトでは常にテスト用の環境と一緒に開始するべきです。 This is being introduced late in the tutorial, but in your future projects you should always start with this.

プロジェクトの記述(Describe the Project) Describe the Project

setup.pyファイルはプロジェクトと、プロジェクトに所属するファイルを記述します。 The ``setup.py`` file describes your project and the files that belong to it.

setup.py
from setuptools import find_packages, setup

setup(
    name='flaskr',
    version='1.0.0',
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        'flask',
    ],
)

packagesは、どのpackageディレクトリ(およびそれが含んでいるPythonファイル)を含めるべきかをPythonに伝えます。find_packages()は、packageディレクトリを全て手入力せずに済むように、packageディレクトリを自動的に見つけ出します。staticやtemplatesディレクトリのような、(Pythonのpackageではない)その他のファイルを含めるには、include_package_dataを設定します。そのようなその他のデータが何かを伝えるためには、PythonはMANIFEST.inというもう一つのファイルを必要とします。 ``packages`` tells Python what package directories (and the Python files they contain) to include. ``find_packages()`` finds these directories automatically so you don't have to type them out. To include other files, such as the static and templates directories, ``include_package_data`` is set. Python needs another file named ``MANIFEST.in`` to tell what this other data is.

MANIFEST.in
include flaskr/schema.sql
graft flaskr/static
graft flaskr/templates
global-exclude *.pyc

これはPythonに、statictemplatesディレクトリにある全てと、schema.sqlファイルはコピーし、バイトコードのファイル(訳注:Pythonが実行時に作成・使用する中間ファイル)は除外するように伝えます。 This tells Python to copy everything in the ``static`` and ``templates`` directories, and the ``schema.sql`` file, but to exclude all bytecode files.

使用されているファイルとオプションの別の説明については、official packaging guideを見てください。 See the `official packaging guide`_ for another explanation of the files and options used.

プロジェクトのインストール Install the Project

pipを使って自分のプロジェクトを仮想環境へインストールします。 Use ``pip`` to install your project in the virtual environment.

$ pip install -e .

これはpipに、いまのディレクトリからsetup.pyを見つけ出し、それを編集可能(editable)または開発(development)モードでインストールするように伝えます。編集可能モードは、ローカルのコードを変更したときに、プロジェクトの依存対象のような、プロジェクトに関するメタデータを変更した場合だけ再インストールが必要なようにします。 This tells pip to find ``setup.py`` in the current directory and install it in *editable* or *development* mode. Editable mode means that as you make changes to your local code, you'll only need to re-install if you change the metadata about the project, such as its dependencies.

この時点でプロジェクトがインストールされたことを、pip listを使って確かめることができます。 You can observe that the project is now installed with ``pip list``.

$ pip list

Package        Version   Location
-------------- --------- ----------------------------------
click          6.7
Flask          1.0
flaskr         1.0.0     /home/user/Projects/flask-tutorial
itsdangerous   0.24
Jinja2         2.10
MarkupSafe     1.0
pip            9.0.3
setuptools     39.0.1
Werkzeug       0.14.1
wheel          0.30.0

プロジェクトの実行のしかたは今までから変化はありません。FLASK_APPはまだflaskrに設定されており、flask runはまだアプリケーションを実行させますが、flask-tutorialディレクトリからだけではなく、どこからでも呼び出せるようになっています。 Nothing changes from how you've been running your project so far. ``FLASK_APP`` is still set to ``flaskr`` and ``flask run`` still runs the application, but you can call it from anywhere, not just the ``flask-tutorial`` directory.

テストの網羅率(Test Coverage)へ続きます。 Continue to :doc:`tests`.