การสร้างแพกเกจและอัพโหลดขึ้น pypi เพื่อให้ติดตั้งได้ด้วย pip

จาก Morange Wiki

ในการเขียนโปรแกรมด้วยภาษาไพธอน นิยมใช้ pip ซึ่งเป็นเครื่องมือติดตั้งแพกเกจ ที่ได้ลงทะเบียนไว้ที่ที่เก็บ[1]

ในขั้นตอนการสร้างนั้น ขอยกตัวอย่างแพกเกจชื่อ bahttext [2] ซึ่งเริ่มต้นจากการสร้างไฟล์ setup.py

#!/usr/bin/env python

from distutils.core import setup

setup(name='bahttext',
      version='1.0',
      description='Tool for convert currency number to Thai text',
      author='Seksan Poltree',
      author_email='seksan@morange.co.th',
      url='https://github.com/morangesolution/bahttext',
      download_url='https://github.com/morangesolution/bahttext/tarball/master',
      packages=['bahttext'],
      license="BSD",
     )

จากนั้น ทำการสร้างไฟล์สำหรับอัพโหลด โดยใช้คำสั่ง python setup.py sdist

(bahttext)Seksans-MacBook-Air:bahttext seksan$ python setup.py sdist
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt

writing manifest file 'MANIFEST'
creating bahttext-1.0
creating bahttext-1.0/bahttext
making hard links in bahttext-1.0...
hard linking setup.py -> bahttext-1.0
hard linking bahttext/__init__.py -> bahttext-1.0/bahttext
creating dist
Creating tar archive
removing 'bahttext-1.0' (and everything under it)

เมื่อสร้างแพกเกจแล้ว ทำการอัลโหลด โดยใช้คำสั่ง python setup.py register เพื่อลงทะเบียนแพกเกจที่ได้สร้างขึ้น ซึ่งขั้นตอนนี้ อาจต้องลงทำเบียนผู้ใช้และรหัสผ่านสำหรับเว็บ pypi ก่อน ซึ่งสามารถลงทะเบียนผ่านสคริปได้เช่นเดียวกัน

(bahttext)Seksans-MacBook-Air:bahttext seksan$ python setup.py register
running register
running check
We need to know who you are, so please choose either:
 1. use your existing login,
 2. register as a new user,
 3. have the server generate a new password for you (and email it to you), or
 4. quit
Your selection [default 1]: 

Username: sekz
Password: 
Registering bahttext to http://pypi.python.org/pypi
Server response (200): OK
I can store your PyPI login so future submissions will be faster.
(the login will be stored in /Users/seksan/.pypirc)
Save your login (y/N)?

เมื่อลงทะเบียนแล้ว สามารถอัพโหลดไฟล์แพกเกจที่สร้างขึ้นไปยังเซิร์ฟเวอร์ได้ โดยใช้คำสั่ง python setup.py sdist upload

(bahttext)Seksans-MacBook-Air:bahttext seksan$ python setup.py sdist upload
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt

writing manifest file 'MANIFEST'
creating bahttext-1.0
creating bahttext-1.0/bahttext
making hard links in bahttext-1.0...
hard linking setup.py -> bahttext-1.0
hard linking bahttext/__init__.py -> bahttext-1.0/bahttext
Creating tar archive
removing 'bahttext-1.0' (and everything under it)
running upload
Submitting dist/bahttext-1.0.tar.gz to http://pypi.python.org/pypi
Server response (200): OK
(bahttext)Seksans-MacBook-Air:bahttext seksan$

จากนั้นแพกเกจจะขึ้นไปอยู่บน pypi [3] เราสามารถใช้คำสั่ง pip install bahttext เพื่อติดตั้งได้ง่ายๆ

(bahttext)Seksans-MacBook-Air:~ seksan$ pip install bahttext
Downloading/unpacking bahttext
  Downloading bahttext-1.0.tar.gz
  Running setup.py (path:/Users/seksan/Workspace/bahttext/build/bahttext/setup.py) egg_info for package bahttext
    
Installing collected packages: bahttext
  Running setup.py install for bahttext
    
Successfully installed bahttext
Cleaning up...
(bahttext)Seksans-MacBook-Air:~ seksan$ pip list
bahttext (1.0)
pip (1.5.4)
setuptools (2.2)
wsgiref (0.1.2)
(bahttext)Seksans-MacBook-Air:~ seksan$


อ้างอิง