파이썬 PIP Install 패키지 만들어보기

Jay
15 min readFeb 12, 2019

--

Link 참고하여 PIP Install 패키지 만들어보기 시작하겠다.

Circadian Rhythm 측정 매트랩기반 프로그램 만들었는데, 파이썬으로 만들어서 사용하면 어떨까 해서 시작한다. ccpy = circa python 이랄까.

진짜 패키지를 만들기 위해 일단 텅빈 pip install package를 만들어봄.

일단 내 깃허브 ccpy를pip package로만들어보자

0. 먼저 할 일, 깃 리포랑 가상환경만들기

  1. 깃허브에서 https://github.com/jeakwon/ccpy 퍼블릭 리포짓을 만들었다.
    (20190214부터는 https://github.com/jeakwon/ypcc 로 주소 변경)
  2. 아나콘다에서 conda create -n ccpy python=3.6 가상환경을 만들었음

1. Setup.py를 만든다

setup.py에 들어갈 내용들

  • name
  • version
  • description
  • author
  • author_email
  • url
  • download_url
  • install_requires
  • packages
  • keywords
  • python_requires
  • package_data
  • zip_safe
  • classifiers

등등등 아래 setup.py코드를 작성해 보았다.

2. 필요한 라이브러리 학인

pip install setuptools
pip install wheel

아나콘다로 가상환경을 만들면 애초에 포함되어 만드러지기 때문에 걱정 없음

The following NEW packages will be INSTALLED:certifi:        2018.11.29-py36_0
pip: 19.0.1-py36_0
python: 3.6.8-h9f7ef89_1
setuptools: 40.7.3-py36_0
sqlite: 3.26.0-he774522_0
vc: 14.1-h0510ff6_4
vs2015_runtime: 14.15.26706-h3a45250_0
wheel: 0.32.3-py36_0
wincertstore: 0.2-py36h7fe50ca_0
Proceed ([y]/n)? y

3. setup.cfg 설정

대부분의 경우 REAdME.md 가 마크다운 형태이므로 아래와 같이 파일 만들기 setup.cfg

4. 빌드

python setup.py bdist_wheel 를 setup.py가 있는 위치에서 실행.

아나콘다 가상환경을 activate하고 했음.(필수인지는 모름)

(ccpy) C:\Users\jeakwon\Desktop\git\ccpy>python setup.py bdist_wheel
running bdist_wheel
running build
installing to build\bdist.win-amd64\wheel
running install
running install_egg_info
running egg_info
creating ccpy.egg-info
writing ccpy.egg-info\PKG-INFO
writing dependency_links to ccpy.egg-info\dependency_links.txt
writing top-level names to ccpy.egg-info\top_level.txt
writing manifest file 'ccpy.egg-info\SOURCES.txt'
reading manifest file 'ccpy.egg-info\SOURCES.txt'
writing manifest file 'ccpy.egg-info\SOURCES.txt'
Copying ccpy.egg-info to build\bdist.win-amd64\wheel\.\ccpy-0.1-py3.6.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\ccpy-0.1.dist-info\WHEEL
creating 'dist\ccpy-0.1-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'ccpy-0.1.dist-info/METADATA'
adding 'ccpy-0.1.dist-info/WHEEL'
adding 'ccpy-0.1.dist-info/top_level.txt'
adding 'ccpy-0.1.dist-info/RECORD'
removing build\bdist.win-amd64\wheel

이렇게 나오고,

dist 폴더에 ccpy-0.1-py3-none-any.whl 라는 빌드파일이 생성된다.

5. 배포

python setup.py upload 로 배포시 http를 사용하여 아이디 패스워드가 노출 될 수 있닥 한다. TLS 라는 것을 이용하는 twine 을 이용하여 안전하게 배포하자. 덤으로 twine을 사용하면 PyPI 계정 없는경우에 가이드까지 해준다고함.

pip insall twine 또는 conda install twine

그리고 나서 dist 폴더에 ccpy-0.1-py3-none-any.whl 라는 빌드파일을

twine upload dist/ccpy-0.1-py3-none-any.whl 이렇게 커맨드에 입력해주면

(ccpy) C:\Users\jeakwon\Desktop\git\ccpy>twine upload dist/ccpy-0.1-py3-none-any.whl
Enter your username: jeakwon
Enter your password:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading ccpy-0.1-py3-none-any.whl
100%|█████████████████████████████████████████████████████████████████████████████| 4.62k/4.62k [00:00<00:00, 6.69kB/s]
NOTE: Try --verbose to see response content.
HTTPError: 403 Client Error: Invalid or non-existent authentication information. for url: https://upload.pypi.org/legacy/

이렇게 뜨는데, 위에 보면 authentication이 없다고 한다 = 아이디 비밀번호만들어라. https://upload.pypi.org/legacy/ 접속후 아이디 비번을 만들어라

참고로 비밀번호 안정성이 높아야 하는듯 하다

다시 도전

(ccpy) C:\Users\jeakwon\Desktop\git\ccpy>twine upload dist/ccpy-0.1-py3-none-any.whl
Enter your username: jeakwon
Enter your password:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading ccpy-0.1-py3-none-any.whl
100%|█████████████████████████████████████████████████████████████████████████████| 4.62k/4.62k [00:00<00:00, 6.18kB/s]
NOTE: Try --verbose to see response content.
HTTPError: 400 Client Error: User 'jeakwon' does not have a verified primary email address. Please add a verified primary email before attempting to upload to PyPI. See https://pypi.org/help/#verified-email for more information.for more information. for url: https://upload.pypi.org/legacy/

이번에도 무언가 애러가 떴다. email address를 확인하라는데…

확인해보니 이런 메일이 와 있다.

PyPI <noreply@pypi.org>
7:27 PM (9 minutes ago)
to me

Someone, perhaps you, has added this email address (onlytojay@gmail.com) to their PyPI account.

If you wish to proceed with this request, click this link to verify your email address

This link will expire in 72 hours.

If you did not make this request, you can safely ignore this email.

위의 링크를 클릭해주고 또 진행하였으나

(ccpy) C:\Users\jeakwon\Desktop\git\ccpy>twine upload dist/ccpy-0.1-py3-none-any.whl
Enter your username: jeakwon
Enter your password:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading ccpy-0.1-py3-none-any.whl
100%|█████████████████████████████████████████████████████████████████████████████| 4.62k/4.62k [00:00<00:00, 6.13kB/s]
NOTE: Try --verbose to see response content.
HTTPError: 403 Client Error: The user 'jeakwon' isn't allowed to upload to project 'ccpy'. See https://pypi.org/help/#project-name for more information. for url: https://upload.pypi.org/legacy/

이번엔… The user ‘jeakwon’ isn’t allowed to upload to project ‘ccpy’.

무슨 문제인지 학인하려 https://pypi.org/help/#project-name 접속

Why isn’t my desired project name available?
Your publishing tool may return an error that your new project can’t be created with your desired name, despite no evidence of a project or release of the same name on PyPI. Currently, there are three primary reasons this may occur:

* The project name conflicts with a Python Standard Library module from any major version from 2.5 to present.

* The project name has been explicitly prohibited by the PyPI administrators. For example, pip install requirements.txt is a common typo for pip install -r

* requirements.txt, and should not surprise the user with a malicious package.
The project name has been registered by another user, but no releases have been created.

느낌이 내 이름으로 뭔가 존재하는가보다 라는걸 느껴서

pip install ccpy 를 검색하니 아니나 다를까 https://pypi.org/project/ccpy/가 존재한다.

6. 대안

그래서 setup.pyname = ‘ccpy’name = ‘ypcc’ 로 바꾸고 저장.

일단 폴더안의 build, dist, ypcc.egg-info 는 모두 삭제 후 python setup.py bdist_wheel

(ccpy) C:\Users\jeakwon\Desktop\git\ccpy>python setup.py bdist_wheel
running bdist_wheel
running build
installing to build\bdist.win-amd64\wheel
running install
running install_egg_info
running egg_info
creating ypcc.egg-info
writing ypcc.egg-info\PKG-INFO
writing dependency_links to ypcc.egg-info\dependency_links.txt
writing top-level names to ypcc.egg-info\top_level.txt
writing manifest file 'ypcc.egg-info\SOURCES.txt'
reading manifest file 'ypcc.egg-info\SOURCES.txt'
writing manifest file 'ypcc.egg-info\SOURCES.txt'
Copying ypcc.egg-info to build\bdist.win-amd64\wheel\.\ypcc-0.1-py3.6.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\ypcc-0.1.dist-info\WHEEL
creating 'dist\ypcc-0.1-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'ypcc-0.1.dist-info/METADATA'
adding 'ypcc-0.1.dist-info/WHEEL'
adding 'ypcc-0.1.dist-info/top_level.txt'
adding 'ypcc-0.1.dist-info/RECORD'
removing build\bdist.win-amd64\wheel

이어서 twine upload dist/ypcc-0.1-py3-none-any.whl

(ccpy) C:\Users\jeakwon\Desktop\git\ccpy>twine upload dist/ypcc-0.1-py3-none-any.whl
Enter your username: jeakwon
Enter your password:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading ypcc-0.1-py3-none-any.whl
100%|█████████████████████████████████████████████████████████████████████████████| 4.62k/4.62k [00:00<00:00, 4.80kB/s]

예스! 에러가 더 뜨지않는다! 이걸로 얻은 교훈.

https://pypi.org/project/ 에서 프로젝트 이름 검색후 이름을 설정할것

그래서 내 깃허브에는 위의 코드 스니펫이랑은 다르게name = ‘ypcc’ 로 되어 있다.

7. Pip install ypcc

진짜 될것인가. 시도해보자

pip install ypcc

결과는

(ccpy) C:\Users\jeakwon\Desktop\git\ccpy>pip install ypcc
Collecting ypcc
Downloading https://files.pythonhosted.org/packages/63/1e/f1643fc007ac5a1dd1c850d215114e4c254a0f92b1e2405025fffd374a39/ypcc-0.1-py3-none-any.whl
Installing collected packages: ypcc
Successfully installed ypcc-0.1

키야~~~~ 맨날 사용만 해보던 라이브러리가 실제로 생성되었다.

(에따라 https://github.com/jeakwon/ypcc 깃허브 주소도 변경, 20190214)

이 방법으로 내 circa analysis용

유용한 라이브러리를 만들 수 있으면 좋겠다.

앞으로 더 공부해야 할 부분은

라이브러리 업데이트 방법/ 삭제 방법.

이상으로 아무런 쓸모가 없는 파이선 패키지 배포기였습니다.

링크에서 더 자세한 패키징 방법을 확인 가능

--

--

Jay
Jay

Written by Jay

Brain Neural Network : Where neuroscience meets machine learning

No responses yet