Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 610. 파이썬 - PyPI 패키지 만들기 [링크 복사], [링크+제목 복사],
조회: 14731
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 4개 있습니다.)
개발 환경 구성: 610. 파이썬 - PyPI 패키지 만들기
; https://www.sysnet.pe.kr/2/0/12863

개발 환경 구성: 611. 파이썬 - PyPI 패키지 만들기 (2) long_description, cmdclass 옵션
; https://www.sysnet.pe.kr/2/0/12865

개발 환경 구성: 612. 파이썬 - PyPI 패키지 만들기 (3) entry_points 옵션
; https://www.sysnet.pe.kr/2/0/12867

개발 환경 구성: 614. 파이썬 - PyPI 패키지 만들기 (4) package_data 옵션
; https://www.sysnet.pe.kr/2/0/12870




파이썬 - PyPI 패키지 만들기

닷넷의 NuGet처럼, 파이썬에는 PyPI 저장소가 있는데요, 바로 그곳에 파이썬으로 만든 패키지(흔한 말로 라이브러리)를 올리는 실습을 해보겠습니다.

Find, install and publish Python packages with the Python Package Index
; https://pypi.org/

등록할 패키지는 테스트 삼아 간단하게 다음의 코드로 합니다.

# iputil.py

import requests


def get():
    """
    Get my Public IP in Python
    ; https://pytutorial.com/python-get-public-ip
    :return:
    """
    endpoint = 'https://ipinfo.io/json'
    response = requests.get(endpoint, verify=True)

    if response.status_code != 200:
        return 'Status:', response.status_code, 'Problem with the request. Exiting.'
        exit()

    data = response.json()

    return data['ip']


if __name__ == '__main__':
    print(get())

그리고 편의상 위의 코드를 담은 파일을 프로젝트 루트의 하위에 디렉터리를 하나 만들어 그 안에 패키지로 묶일 파일들을 담아 둘 텐데요, 가령 "netutil" 이름의 디렉터리를 만들고 그 안에 위의 코드를 담은 "iputil.py" 파일과, 패키지임을 알리는 __init__.py를 위치시킵니다.

# __init__.py

from netutil import iputil

__version__ = '1.0.0.1'
__author__ = 'techshare'

이와 함께 setup.py를 프로젝트의 루트 디렉터리에 생성하는데요, 따라서 대략 다음과 같은 구조가 됩니다.

.
├── netutil
│   ├── __init__.py
│   └── iputil.py
└── setup.py

그리고 setup.py는 다음의 코드를 담습니다.

from setuptools import setup, find_packages
import netutil


setup(
    name="net-util",
    description="utility functions for networking",
    version=netutil.__version__,
    author=netutil.__author__,
    author_email="techsharer@outlook.com",
    url="https://www.sysnet.pe.kr",
    license="Ms-PL",
    packages=find_packages(exclude=[]),
    install_requires=["requests>=2.22.0"],
)

자, 그럼 여기까지 해서 오류가 없음을 다음의 명령어로 테스트해 줍니다.

c:\temp> python setup.py
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

error: no commands supplied

setup.py에 문제가 없다면 위와 같은 식의 출력이 나와야 합니다.




"python setup.py" 명령어의 출력에도 나오지만, 필요한 cmd 인자가 하나 더 있습니다. 이것은 "--help-commands"를 주면 알 수 있는데, 여러 가지 명령어 중에 여기서는 bdist_wheel을 사용합니다.

c:\temp> python setup.py bdist_wheel
running bdist_wheel
running build
running build_py
creating build
creating build\lib
creating build\lib\netutil
copying netutil\iputil.py -> build\lib\netutil
copying netutil\__init__.py -> build\lib\netutil
installing to build\bdist.win-amd64\wheel
running install
running install_lib
creating build\bdist.win-amd64
creating build\bdist.win-amd64\wheel
creating build\bdist.win-amd64\wheel\netutil
copying build\lib\netutil\iputil.py -> build\bdist.win-amd64\wheel\.\netutil
copying build\lib\netutil\__init__.py -> build\bdist.win-amd64\wheel\.\netutil
running install_egg_info
running egg_info
creating net_util.egg-info
writing net_util.egg-info\PKG-INFO
writing dependency_links to net_util.egg-info\dependency_links.txt
writing top-level names to net_util.egg-info\top_level.txt
writing manifest file 'net_util.egg-info\SOURCES.txt'
reading manifest file 'net_util.egg-info\SOURCES.txt'
writing manifest file 'net_util.egg-info\SOURCES.txt'
Copying net_util.egg-info to build\bdist.win-amd64\wheel\.\net_util-1.0.0.0-py3.7.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\net_util-1.0.0.0.dist-info\WHEEL
creating 'dist\net_util-1.0.0.0-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'netutil/__init__.py'
adding 'netutil/iputil.py'
adding 'net_util-1.0.0.0.dist-info/METADATA'
adding 'net_util-1.0.0.0.dist-info/WHEEL'
adding 'net_util-1.0.0.0.dist-info/top_level.txt'
adding 'net_util-1.0.0.0.dist-info/RECORD'
removing build\bdist.win-amd64\wheel

그럼, 현재 디렉터리 기준으로 하위에 "build", "dist", "[setup.py에 지정한 name].egg-info" 디렉터리가 생성이 되는데 최종 결과물은 "dist" 하위에 생성되고, 예제의 경우 "net_util-1.0.0.0-py3-none-any.whl"이 됩니다. (그동안, 패키지 관리자를 사용해 보셨다면 whl 파일도 결국 zip 파일임을 예상할 수 있을 것입니다. ^^)




whl 파일(PEP 427 The Wheel Binary Package Format 1.0)은 최종 패키징 파일이기 때문에 이것 자체로 pip을 이용한 설치가 가능합니다.

c:\temp\dist> pip install net_util-1.0.0.0-py3-none-any.whl
Processing c:\temp\dist\net_util-1.0.0.0-py3-none-any.whl
Requirement already satisfied: requests>=2.22.0 in e:\python37\lib\site-packages (from net-util==1.0.0.0) (2.26.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in e:\python37\lib\site-packages (from requests>=2.22.0->net-util==1.0.0.0) (1.26.6)
Requirement already satisfied: charset-normalizer~=2.0.0 in e:\python37\lib\site-packages (from requests>=2.22.0->net-util==1.0.0.0) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in e:\python37\lib\site-packages (from requests>=2.22.0->net-util==1.0.0.0) (2021.5.30)
Requirement already satisfied: idna<4,>=2.5 in e:\python37\lib\site-packages (from requests>=2.22.0->net-util==1.0.0.0) (3.2)
Installing collected packages: net-util
Successfully installed net-util-1.0.0.0

이후, 다른 파이썬 프로젝트에서 다음과 같이 import시켜 사용할 수 있습니다.

import netutil

print(netutil.iputil.get_public_ip())

사실 로컬에서 사용할 거라면 (develop mode를 지원하는) "pip install -e ." 등의 방법을 사용하는 것이 더 편리하지만, 그래도 복잡한 setup.py를 구성한 경우 정상적으로 동작하는지 저런 식으로 테스트하는 것도 좋겠습니다.

자, 그럼 제대로 setup.py가 동작했다면 이제 결과물을 PyPI에 올려야 할 텐데요, 재미있는 것은 PyPI의 경우 직접 패키징 파일을 업로드하는 기능이 사이트에서 제공하지 않습니다. (제가 못 찾은 걸 수도 있습니다. ^^;)

대신, twine과 같은 식의 도구를 사용해 다음과 같이 등록할 수 있습니다.

c:\temp> pip install twine

C:\temp> twine upload dist/net_util-1.0.0.0-py3-none-any.whl
Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: techshare
Enter your password:
Uploading net_util-1.0.0.0-py3-none-any.whl
100%|███████████████████████████████████████████████| 4.51k/4.51k [00:01<00:00, 2.46kB/s]

View at:
https://pypi.org/project/net-util/1.0.0.0/

그런데, 저렇게 실행 시 계정 정보를 입력받는 것이 영 못마땅한데요, 다행히 별도 설정 파일을 이용하는 방법도 있습니다.

[윈도우]
%USERPROFILE%\.pypirc

[리눅스]
$HOME/.pypirc

환경에 따라 위의 경로로 ".pypirc" 파일을 만들고 그 안에 계정 정보를 넣어두면,

[pypi]
  username = ...사용자 계정...
  password = ...사용자 암호...

twine 실행 시 계정 정보를 묻지 않습니다. 하지만, 그래도 여전히 "사용자 암호"가 노출되는데, 이러한 문제는 PyPI 사이트로부터 "API 토큰"을 생성하는 것으로 해결할 수 있습니다.

pypi_api_token_1.png

토큰 값은 한 번만 출력되므로 페이지를 벗어나기 전에 반드시 복사해 두어야 합니다. 그래서 여러분들의 .pypirc 파일에 다음과 같이 값을 채워주면 됩니다.

[pypi]
  username = __token__
  password = pypi-AgE...[생략]...bHMWc

혹은 여러 계정을 관리해야 한다면?

[distutils]
index-servers=
    mypypi
    testpypi

[mypypi]
  username = __token__
  password = pypi-AgE...[생략]...bHMWc

[testpypi]
  username = __token__
  password = pypi-AgE...[생략]...bHMWc

(다중 설정의 경우 twine 명령행에 "--repository testpypi"와 같은 식으로 선택할 수 있습니다.)

이 정도면 기본적인 사용법은 대충 마무리된 것 같습니다. 이후에는 낯선 옵션과 용어들을 좀 더 넓혀가는 것만 남았군요. ^^




개인적으로, 처음 setup.py를 봤을 때 직관적으로 잘 이해할 수 없었습니다. 보통 그동안의 경험상 setup이라고 하면 설치 파일(setup.exe)을 연상하게 되는데 이번 경우에는 "make_package"에 해당하기 때문에 혼란이 온 것입니다. (어쨌든, "set up a package"라는 의미였겠지만... ^^;)

참고로, 아래의 글도 읽어보시길 권장합니다.

Deep Dive into pip - 1
; https://suhwan.dev/2018/10/24/deep-dive-into-pip-1/

Deep Dive into pip - 2
; https://suhwan.dev/2018/10/30/deep-dive-into-pip-2/

The Pip Python Package Manager
; https://www.datacamp.com/community/tutorials/pip-python-package-manager

그나저나, setup.py를 이용한 방식도 이젠 legacy라고... ^^;

setup.py 멈춰!
; https://tech.buzzvil.com/blog/setup.py-%EB%A9%88%EC%B6%B0/




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 12/7/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  [62]  63  64  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12387정성태10/28/202018505.NET Framework: 957. C# - static 필드의 정보가 GC Heap에 저장될까요? [3]파일 다운로드1
12386정성태10/28/202019242Linux: 34. 사용자 정보를 함께 출력하는 리눅스의 ps 명령어 사용 방법
12385정성태10/28/202016590오류 유형: 673. openssl - req: No value provided for Subject Attribute CN, skipped
12384정성태10/27/202018912오류 유형: 672. AllowPartiallyTrustedCallers 특성이 적용된 어셈블리의 struct 멤버 메서드를 재정의하면 System.Security.VerificationException 예외 발생
12383정성태10/27/202018982.NET Framework: 956. C# 9.0 - (7) 패턴 일치 개선 사항(Pattern matching enhancements) [3]파일 다운로드1
12382정성태10/26/202016725오류 유형: 671. dotnet build - The local source '...' doesn't exist
12381정성태10/26/202018743VC++: 137. C++ stl map의 사용자 정의 타입을 key로 사용하는 방법 [1]파일 다운로드1
12380정성태10/26/202014714오류 유형: 670. Visual Studio - Squash_FailureCommitsReset
12379정성태10/21/202020287.NET Framework: 955. .NET 메서드의 Signature 바이트 코드 분석 [1]파일 다운로드2
12378정성태10/15/202019231.NET Framework: 954. C# - x86/x64 환경에 따라 달라지는 P/Invoke 함수의 export 이름 [1]파일 다운로드1
12377정성태10/15/202019650디버깅 기술: 172. windbg - 파일 열기 시점에 bp를 걸어 파일명 알아내는 방법(Managed/Unmanaged)
12376정성태10/15/202015747오류 유형: 669. windbg - sos의 name2ee 명령어 실행 시 "Failed to request module list." 오류
12375정성태10/15/202017273Windows: 177. 윈도우 탐색기에서 띄우는 cmd.exe 창의 디렉터리 구분 문자가 'Yen(&#0165;)' 기호로 나오는 경우 [1]
12374정성태10/14/202023173.NET Framework: 953. C# 9.0 - (6) 함수 포인터(Function pointers) [1]파일 다운로드2
12373정성태10/14/202016944.NET Framework: 952. OpCodes.Box와 관련해 IL 형식으로 직접 코딩 시 유의할 점
12372정성태10/13/202019337.NET Framework: 951. C# 9.0 - (5) 로컬 함수에 특성 지정 가능(Attributes on local functions)파일 다운로드1
12371정성태10/13/202018252개발 환경 구성: 519. Visual Studio의 Ctrl+Shift+U (Edit.MakeUppercase) 단축키가 동작하지 않는 경우
12370정성태10/13/202018468Linux: 33. Linux - nmcli를 이용한 고정 IP 설정
12369정성태10/12/202022114Windows: 176. Raymond Chen이 한글날에 밝히는 윈도우의 한글 자모 분리 현상 [3]
12368정성태10/12/202018205오류 유형: 668. VSIX 확장 빌드 - The "GetDeploymentPathFromVsixManifest" task failed unexpectedly.
12367정성태10/12/202030701오류 유형: 667. Ubuntu - Temporary failure resolving 'kr.archive.ubuntu.com' [2]
12366정성태10/12/202020051.NET Framework: 950. C# 9.0 - (4) 원시 크기 정수(Native ints) [1]파일 다운로드1
12365정성태10/12/202018391.NET Framework: 949. C# 9.0 - (3) 람다 메서드의 매개 변수 무시(Lambda discard parameters)파일 다운로드1
12364정성태10/11/202019389.NET Framework: 948. C# 9.0 - (2) localsinit 플래그 내보내기 무시(Suppress emitting localsinit flag)파일 다운로드1
12363정성태10/11/202021102.NET Framework: 947. C# 9.0 - (1) 대상으로 형식화된 new 식(Target-typed new expressions) [2]파일 다운로드1
12362정성태10/11/202017651VS.NET IDE: 151. Visual Studio 2019에 .NET 5 rc/preview 적용하는 방법
... 61  [62]  63  64  65  66  67  68  69  70  71  72  73  74  75  ...