Setuptoolsを使った展開(Deploying with Setuptools) Deploying with Setuptools

Setuptoolsは、Pythonのライブラリや拡張を配布するとき一般的に使われる拡張ライブラリです。Setuptoolsは、Pythonと一緒に出荷(ship)されていてモジュールのインストールする仕組みの基本であるdistutilsを、大規模なアプリケーションをより容易に配布できるように、様々なより複雑な構成要素もサポートするよう拡張しています: `Setuptools`_, is an extension library that is commonly used to distribute Python libraries and extensions. It extends distutils, a basic module installation system shipped with Python to also support various more complex constructs that make larger applications easier to distribute:

  • 依存対象のサポート: ライブラリまたはアプリケーションは、それが依存する他のライブラリを、自動的にインストールされるように宣言できます。 **support for dependencies**: a library or application can declare a list of other libraries it depends on which will be installed automatically for you.

  • パッケージの登録: setuptoolsはパッケージをインストールされたPythonに登録します。これはあるパッケージから提供される情報を他のパッケージから問い合わせることを可能にします。最もよく知られているこの仕組みの目玉機能は、あるパッケージが「entry point」を宣言し、他のパッケージが別のパッケージを拡張するためにフックできるようにする仕組みのサポートです。 **package registry**: setuptools registers your package with your Python installation. This makes it possible to query information provided by one package from another package. The best known feature of this system is the entry point support which allows one package to declare an "entry point" that another package can hook into to extend the other package.

  • インストール管理ツール: pipで他のライブラリをインストールできます。 **installation manager**: :command:`pip` can install other libraries for you.

もしPython 2(>=2.7.9)またはPython 3(>=3.4)をpython.orgからインストールしている場合、システムにはpipとsetuptoolsが既にインストールされているでしょう。そうでない場合、それらを自分でインストールする必要があります。 If you have Python 2 (>=2.7.9) or Python 3 (>=3.4) installed from python.org, you will already have pip and setuptools on your system. Otherwise, you will need to install them yourself.

Flask自身、そしてPyPIで見つけることができるすべてのライブラリはsetuptoolsまたはdistutilsを使って配布されています。 Flask itself, and all the libraries you can find on PyPI are distributed with either setuptools or distutils.

この章の例では、自分のアプリケーションをyourapplication.pyと呼び、モジュールではなくより大きなアプリケーションを使っていると仮定します。もし自分のアプリケーションをパッケージへまだ変換していない場合は、そのようにすればできるか見るためにより大きなアプリケーションパターンを調べてください。 In this case we assume your application is called :file:`yourapplication.py` and you are not using a module, but a :ref:`package <larger-applications>`. If you have not yet converted your application into a package, head over to the :ref:`larger-applications` pattern to see how this can be done.

setuptoolsを使ったデプロイ作業は、より複雑でより自動化されたデプロイのシナリオへの最初のステップです。もしプロセスをすべて自動化したい場合、Deploying with Fabric章も読んでください。 A working deployment with setuptools is the first step into more complex and more automated deployment scenarios. If you want to fully automate the process, also read the :ref:`fabric-deployment` chapter.

基本になるSetupスクリプト Basic Setup Script

Flaskを既にインストールしているので、setuptoolsはシステム上で利用可能です。Flaskがすでにsetuptoolsに依存しています。 Because you have Flask installed, you have setuptools available on your system. Flask already depends upon setuptools.

標準的な免責事項が適用されます: virtualenvを使うべきです Standard disclaimer applies: :ref:`you better use a virtualenv <virtualenv>`.

setupのコードは常に自分のアプリケーションの側にあるsetup.pyという名前のファイルの中で進めます。ファイル名が唯一の規約ですが、誰もがその名前のファイルを探そうとするので、(場所などを)変更すべきではありません。 Your setup code always goes into a file named :file:`setup.py` next to your application. The name of the file is only convention, but because everybody will look for a file with that name, you better not change it.

Flaskアプリケーションの基本となるsetup.pyファイルは以下のようになります: A basic :file:`setup.py` file for a Flask application looks like this::

from setuptools import setup

setup(
    name='Your Application',
    version='1.0',
    long_description=__doc__,
    packages=['yourapplication'],
    include_package_data=True,
    zip_safe=False,
    install_requires=['Flask']
)

サブパッケージを明示的にリストにする必要があることを覚えておいてください。もしsetuptoolsが自動的にパッケージを探し出すようにしたい場合、find_packages関数を使用できます: Please keep in mind that you have to list subpackages explicitly. If you want setuptools to lookup the packages for you automatically, you can use the ``find_packages`` function::

from setuptools import setup, find_packages

setup(
    ...
    packages=find_packages()
)

setup関数へのほとんどのパラメータは自己説明的なはずですが、include_package_datazip_safeはそうではない(何を意図するものかわかりづらい)かもしれません。include_package_dataは、MANIFEST.inファイルを探して、一致する全てエントリーをパッケージのデータとしてインストールするよう、setuptoolsに伝えます。これは、Pythonモジュールと一緒に静的ファイルとテンプレートを配布するために使用します(リソースの配布を見てください)。zip_safeフラグは、zipアーカイブの作成を強要もしくは回避するために使用できます。概して、zipファイルとしてインストールされるパッケージは、ツールの中にはサポートしないものがあり、デバッグが大幅に難しくなるため、おそらくパッケージをzipファイルとしてインストールしたくはないでしょう。 Most parameters to the ``setup`` function should be self explanatory, ``include_package_data`` and ``zip_safe`` might not be. ``include_package_data`` tells setuptools to look for a :file:`MANIFEST.in` file and install all the entries that match as package data. We will use this to distribute the static files and templates along with the Python module (see :ref:`distributing-resources`). The ``zip_safe`` flag can be used to force or prevent zip Archive creation. In general you probably don't want your packages to be installed as zip files because some tools do not support them and they make debugging a lot harder.

ビルドのタグ付け Tagging Builds

リリースと開発でビルドを区別すると便利です。それらのオプションを設定するために、setup.cfgファイルを追加します。: It is useful to distinguish between release and development builds. Add a :file:`setup.cfg` file to configure these options. ::

[egg_info]
tag_build = .dev
tag_date = 1

[aliases]
release = egg_info -Db ''

python setup.py sdistを実行すると「.dev」にそのときの日付を追加したタグを使って開発パッケージを作成します: flaskr-.1.0.dev20160314.tar.gzpython setup.py release sdistを実行するとバージョンだけを使ってリリースパッケージを作成します: flaskr-1.0.tar.gz Running ``python setup.py sdist`` will create a development package with ".dev" and the current date appended: ``flaskr-1.0.dev20160314.tar.gz``. Running ``python setup.py release sdist`` will create a release package with only the version: ``flaskr-1.0.tar.gz``.

リソースの配布 Distributing Resources

もしちょうど作成したパッケージをインストールしようとする場合、statictemplatesのようなフォルダはインストールされないことに気づくでしょう。その理由は、setuptoolsはどのファイルを加えればよいか知らないためです。このときするべきことは、MANIFEST.inファイルをsetup.pyファイルの側に作成することです。MANIFEST.inファイルはあなたのtarball(訳注: 配布用のファイル一式をまとめたアーカイブファイル、よく使われるファイル形式の一つにtar形式があるため、tarballと呼ばれることがある)に加えるべきすべてのファイルをリストします: If you try to install the package you just created, you will notice that folders like :file:`static` or :file:`templates` are not installed for you. The reason for this is that setuptools does not know which files to add for you. What you should do, is to create a :file:`MANIFEST.in` file next to your :file:`setup.py` file. This file lists all the files that should be added to your tarball::

recursive-include yourapplication/templates *
recursive-include yourapplication/static *

MANIFEST.inファイルにそれらを入れたとしても、setup関数のinclude_package_dataパラメータをTrueに設定しない限りはインストールされないことを忘れないでください。 Don't forget that even if you enlist them in your :file:`MANIFEST.in` file, they won't be installed for you unless you set the `include_package_data` parameter of the ``setup`` function to ``True``!

依存対象の宣言 Declaring Dependencies

依存対象はinstall_requiresパラメータの中でリストとして宣言されます。リスト内の各項目は、インストール時にPyPIから引き出されるはずのパッケージの名前です。標準設定では常に最新バージョンを使用しますが、最小及び最大バージョンの要件を提供することもできます。以下はいくつかの例です: Dependencies are declared in the ``install_requires`` parameter as a list. Each item in that list is the name of a package that should be pulled from PyPI on installation. By default it will always use the most recent version, but you can also provide minimum and maximum version requirements. Here some examples::

install_requires=[
    'Flask>=0.2',
    'SQLAlchemy>=0.6',
    'BrokenPackage>=0.7,<=1.0'
]

先に言及したように、依存対象はPyPIから引き出されます。もし誰とも共有したくない内部用パッケージであるためにPyPIでは見つからず見つかるようにするつもりもないパッケージに依存させたい場合はどうするのでしょうか。あたかもPyPIエントリーがあるようにして、setuptoolsがtarballsを探すべき代わりの場所のリストを提供します: As mentioned earlier, dependencies are pulled from PyPI. What if you want to depend on a package that cannot be found on PyPI and won't be because it is an internal package you don't want to share with anyone? Just do it as if there was a PyPI entry and provide a list of alternative locations where setuptools should look for tarballs::

dependency_links=['http://example.com/yourfiles']

そのページはディレクトリ内のファイルのリストであり、setuptoolsが検索するためのファイル名と同じような、適切なファイル名を使った実際のtarballsをページ中のリンクが指すように、確実にしてください。もしパッケージを含んだ社内サーバを持っている場合、そのサーバのURLを提供します。 Make sure that page has a directory listing and the links on the page are pointing to the actual tarballs with their correct filenames as this is how setuptools will find the files. If you have an internal company server that contains the packages, provide the URL to that server.

インストール/開発 Installing / Developing

自分のアプリケーションを(理想的にはvirtualenvの中へ)インストールするには、ただsetup.pyスクリプトをinstallパラメータを使って実行します。そのコマンドはアプリケーションをvirtualenvのsite-packagesフォルダ内へインストールし、すべての依存対象のダウンロードとインストールも行います: To install your application (ideally into a virtualenv) just run the :file:`setup.py` script with the ``install`` parameter. It will install your application into the virtualenv's site-packages folder and also download and install all dependencies::

$ python setup.py install

もしパッケージを開発していて要求されるものもインストールしたいときは、代わりにdevelopコマンドを使用できます: If you are developing on the package and also want the requirements to be installed, you can use the ``develop`` command instead::

$ python setup.py develop

developコマンドはデータをコピーする代わりに、単にリンクをsite-packagesへインストールするという利点があります。その後は、変更の後でinstallを再び実行する必要なくコード上で作業を続けられます。 This has the advantage of just installing a link to the site-packages folder instead of copying the data over. You can then continue to work on the code without having to run ``install`` again after each change.