1. 패키지 폴더 준비

package/
    mypack/
        __init__.py
    dist/
        mypack-0.1-py3-none-any.whl
        mypack-0.1.tar.gz
    tests/
        test_func1.py
        test_func2.py
    README.md
    LICENSE
    requirements.txt
    setup.py

2. .gitignore 준비

/Include
/Lib
/Scripts
/bin
/lib
/lib64
/include
/share
/man
/.eggs
__pycache__
pyvenv.cfg

3. requirements.txt 준비

requests==2.22.0
pandas==0.25.3
nose==1.3.7
wheel==0.33.6

4. setup.py 준비

from setuptools import setup, find_packages

setup(
    name = 'mypack',
    version  = '0.1',
    description = 'mypack',
    author = 'Taewoo Lee',
    author_email = 'oiehot@gmail.com',
    license = 'MIT',
    packages = ['mypack'],
    # packages = setuptools.find_packages()
    install_requires = [],
    keywords = ['mypack'],
    package_data = {},
    zip_safe = False,
    classifiers = [
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires = '>=3.6',
)

5. 테스트 하기

  1. 테스트 함수 준비
# tests/test_core.py
from unittest import TestCase

class TestFunc(TestCase):
    def test_is_string(self):
        self.assertTrue(isinstance("Hello World",str))
  1. 테스트에 필요한 모듈 설치
$ pip install nose
or pip install -r requirements.txt # nose 가 기입된
  1. 패키지 설정
# setup.py
setup(
    ...
    test_suite='nose.collector',
    tests_require=['nose'],
)
  1. 테스트 실행
$ python setup.py test
#!/bin/bash
# /test (700)
source $PWD/bin/activate
python setup.py test
deactivate

6. 빌드하기

$ cd package/
$ python setup.py sdist
$ python setup.py bdist
$ python setup.py bdist_wheel
  • dist/ 에서 결과물 확인
  • sdist
    • source distribution
    • 소스 배포
    • tar.gz 로 압축됨
    • 경로: package/dist/mypack-0.1.tar.gz
  • bdist
    • built distribution
    • 빌드 결과물 배포
    • 경로: package/dist/mypack-0.1.win-amd64.zip
  • bdist_wheel
    • $ pip install wheel 이 선행되어야 함
    • 경로: package/dist/mypack-0.1-py3-none-any.whl

7. 빌드된 패키지 사용하기

$ pip install mypack

# 로컬(ssh, git)을 통한 설치
$ pip install git+ssh://192.168.0.10/oiehot/project/python/mypack.git

# 패키지 목록을 통한 설치
$ cat requirements.txt
sanic==19.9.0
requests==2.9.1
pandas==0.24.2
pymongo==3.8.0
git+ssh://192.168.0.10/oiehot/project/python/mypack.git
$ pip install -r requirements.txt

참고자료