2016/06/09

Day XXX: setuptools の使い方

python パッケージの作成、および他のパッケージの依存関係などを管理するには、{setuptools} を使えば簡単に実現できる。
{setuptools} は Homebrew で python をインストールすると自動的にインストールされているパッケージである。
設定方法は、パッケージの root directory に setup.py を作成するだけである。
練習用に arith4 という sandbox package を作成した。
setup.py#!/usr/bin/env python
# -*- coding:utf-8 -*-

from setuptools import setup, find_packages
import arith4

def setup_package():
 metadata = dict()
 metadata['name'] = arith4.__package__
 metadata['version'] = arith4.__version__
 metadata['description'] = arith4.description_
 metadata['long_description'] = arith4.long_description_
 metadata['author'] =  arith4.author_
 metadata['url'] = arith4.url_
 metadata['license'] = arith4.license_
 metadata['entry_points'] = {
  'console_scripts': [
   'arith4_command = arith4.arith4_command:arith4_command',
  ],
  'gui_scripts': [
  ],
 }
 metadata['packages'] = find_packages(exclude=["tests"])
 metadata['include_package_data'] = True
 metadata['package_data'] = {
  '': ['*.rst'],
 }
 metadata['install_requires'] = [
  'setuptools >= 22.0.5',
 ]
 setup(**metadata)

if __name__ == "__main__":
 setup_package()
基本的に {setuptools} の setup() を呼ぶだけである。
setup() に渡す引数(上記の metadata)として何があるかは、setuptools のドキュメントを見るのが一番良いが、使用したものについて簡単にまとめる。

  • name :
    パッケージの名前
  • version :
    バージョン
  • description, long_description :
    説明
  • author :
    作者
  • author_email :
    作者 e-mail アドレス
  • url :
    ホームページの URL
  • license :
    パッケージの利用ライセンス
  • entry_points :
    ここに 名前=関数 の形で登録しておくと、パスの通った場所に、その関数が実行されるスクリプトの形でインストールされる
    console_script と gui_script はその名の通り
  • packages :
    setuptools.find_package() を呼ぶと、root directory 以下の有効な(python ソースコードの入った)ディレクトリをパッケージと認識して、build, install を行う
    上記ではさらに tests フォルダを除外する設定にしてある
  • include_package_data :
    ソースコード以外のものもパッケージに含めるか
  • packages_data :
    パッケージ名と、パッケージに入れるファイルの形式を指定する
    上記ではすべてのパッケージにおいて .rst 拡張子のファイルを含める
  • install_requires :
    インストールの際に必要な他パッケージの依存関係
    パッケージ名 (評価記号) バージョン番号という形で登録する
    評価記号には == と >= がある
主な設定項目は arith4/__init__.py に記述し、様々な箇所から参照できるようにした。

設定が終了したら、setup.py builld でビルドされ、 setup.py install でインストールされる。

0 件のコメント:

コメントを投稿