LoginSignup
3
9

More than 5 years have passed since last update.

Python を setup.pyでパッケージ化してみる

Last updated at Posted at 2018-06-18

Pythonで作成したプログラムをパッケージ化して配布したいときがありますよね。
例えば単純に「hello world」を表示するプログラムを作ったとします。

hello.py

hello.py
str1 = "hello world"

def main():
    print(str1)

if __name__ == '__main__':
    main()

こんな感じにコマンドを叩けば表示されますね。

admin$ python ./hello.py
hello world

コマンドを打つのが面倒い場合はパッケージ化することで多少は楽になります。
パッケージ化するにはsetup.pyが必要となります。
パッケージの名前や依存関係などを含むファイルを定義してあげます。

setup.py
from setuptools import setup

setup(
    name='hello',
    entry_points={
        'console_scripts': [
            'hello = hello:main',
        ],
    }
)

setup.pyの定義が終わったら python setup.py develop を打つことでdevelop用にインストールされます。

admin$ python setup.py develop
running develop
running egg_info
省略
Installed /Users/admin/Desktop
Processing dependencies for hello==0.0.0
Finished processing dependencies for hello==0.0.0

あとはhelloを打つだけで、「hello world」が表示されます。

admin$ hello
hello world

それでは、Enjoy your pypy and Python life!

3
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
9