pypi 패키지 등록 방법

2024. 8. 6. 14:43Python

pypi에 패키지를 업로드 하는 방법을 정리한 글입니다. pypi 가입이 되어 있다는 전제하에 작성되었습니다.

 

준비물

  • pypi 계정 및  API token

 

프로젝트 생성

1. pypid에 등록할 패키지 명을 최상위 디렉토리로 만든다.

mothod-versioning 패키지 예시


2. setup.py 작성

  • pypi 패키지에 대한 정보 기입
  • Development Status 종류
    • 1 - Planning: 초기 개발 단계로, 아직 개발이 시작되지 않은 상태.
    • 2 - Pre-Alpha: 개발이 시작되었지만, 기능이 거의 없는 상태.
    • 3 - Alpha: 주요 기능이 구현되었으나, 테스트가 충분히 이루어지지 않은 상태.
    • 4 - Beta: 대부분의 기능이 구현되고, 테스트가 어느 정도 이루어진 상태지만, 여전히 버그가 있을 수 있는 상태.
    • 5 - Production/Stable: 기능이 완전히 구현되고, 충분히 테스트되어 실제 사용이 가능한 상태.
    • 6 - Mature: 안정된 버전으로, 오랜 기간 동안 사용되며, 추가적인 개발이 거의 없는 상태.
    • 7 - Inactive: 더 이상 유지보수되지 않는 상태.
  •  주의점
    • python은 import 시에 하이픈(-) 사용이 불편하기 때문에 최상위 디렉토리에만 하이픈(-)을 사용하고 하위 디텍토리는 언더스코어(_)를 사용하는게 좋다.
    • 최상위 디렉토리와 바로 하위 디렉토리 명이 다를 경우 pakages 옵션 설정이 필요하다.
from setuptools import setup, find_packages

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name="method-versioning",
    version="0.0.1",
    author="Sanghun Lee",
    author_email="nrhys2005@gmail.com",
    description="Method versioning package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/nrhys2005/method-versioning",
    packages=find_packages(include=['method_versioning', 'method_versioning.*']),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Topic :: Software Development :: Libraries",
    ],
    python_requires='>=3.6',
)

3. LICENCE 작성

MIT License

Copyright (c) 2024 Sanghun Lee

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

4. 패키지 빌드

1. 모듈 설치

pip install setuptools wheel

2. 패키지 빌드

python setup.py sdist bdist_wheel

5. 패키지 업로드

1. 모듈 설치

pip install twine

2. 패키지 업로드

  • 첫 업로드 시에는 api token이 필요
python -m twine upload dist/*

6. 새로운 버전 업로드

1. 소스 수정 및 버전 수정

2. dist, build, *.egg-info 폴더를 삭제한 후 다시 빌드

rm -rf dist build *.egg-info
python setup.py sdist bdist_wheel

3. 재업로드

twine upload dist/*

 

참고