PyPIにパッケージを登録する必要最低限の流れ
  • Date:2015-11-12 木 (午前 06:49:40)
  • Updated:2015-11-13 金 (午前 09:42:48)
  • Tags:Python
  • Category:Programming

目次


PyPIにパッケージを登録してみたので流れをまとめました、詳しい説明は抜きで必要最低限のことだけをまとめてあります。

主にPyPAの公式sampleprojectを参考にしています。

embed.ly

1. 必要なファイル

.
├── MANIFEST.in
├── README.rst
├── setup.cfg
├── setup.py
├── XXX
│   ├── __init__.py
│   └── XXX.py
└── tests
    ├── __init__.py
    └── test_XXX.py

※XXXは適宜置き換えます

setup.cfgは以下のように書いておきます。

[bdist_wheel]
universal=1

MANIFEST.inはPyPIにアップロードするファイル書きます。

include README.rst
include setup.cfg
recursive-include tests *

README.rstとなっているのでreStructuredTextです。PyPIでMarkdownは使えません。

2. setup.pyの内容

import sys

from setuptools.command.test import test as TestCommand
from setuptools import setup, find_packages
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()


class PyTest(TestCommand):
    user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]

    def initialize_options(self):
        TestCommand.initialize_options(self)
        self.pytest_args = []

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        #import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(self.pytest_args)
        sys.exit(errno)


setup(
    name='sample',
    version='0.1.0',
    description='Description',
    long_description=long_description,
    url='https://github.com/foo/sample',
    author='foo',
    author_email='foo.bar@example.com',
    license='MIT',
    py_modules=['sample'],
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',
        # Indicate who your project is intended for
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
    ],
    keywords='Sample',
    packages=find_packages(exclude=['contrib', 'docs', 'tests']),
    install_requires=['peppercorn'],
    extras_require={
        'dev': ['check-manifest'],
        'test': ['coverage'],
    },
    tests_require=['pytest'],
    cmdclass={'test': PyTest},
    entry_points={
        'console_scripts': [
            'sample=sample:main',
        ],
    },
)

※sampleというパッケージになっているので適宜置き換えます

特筆すべき内容

  1. README.rstをPyPIのトップに表示
  2. "python setup.py test"でテストが走る
  3. モジュール名の指定(py_modules)
  4. コンソールアプリのコマンド追加(entry_points)

他は基本的なsetup.pyの内容です。

setupの引数の、name,description,url,authorなどの内容は適宜書き換えて使います。

3. PyPIへの登録

(1) PyPI - the Python Package Index : Python Package Indexでユーザーを作ります。

(2) ~/.pypircを作成します。

[distutils]
index-servers =
   pypi

[pypi]
username: {Username}
password: {Password}

(3) setup.pyの内容を確認します。

$ python setup.py check -r -s

(4) PyPIに登録します。

$ python setup.py register

(5) パッケージをアップロードします。

$ python setup.py sdist bdist_wheel upload

コマンドを実行するにあたって必要なパッケージがいくつかあるので適宜インストールが必要です。

まとめ

必要最低限の情報で説明はないので、PyPIについて詳しいことは他のブログエントリや公式のドキュメントを参照するといいです。

参考記事

PyPIについて多くのことはこちらのスライドに学びました。わかりやすかったです。

embed.ly

Show comments

Adsense

Share

  • このエントリーをはてなブックマークに追加

About

どこにでもいる平凡なプログラムを書く人間のログ。

Recently

Tags

Pages

-->