記錄制作 Python Egg 到 PyPI


編寫 setup.py 檔案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages

__version__ = '0.1.0'
__author__ = 'Zeuxis Lo'
__email__ = '[email protected]'

setup(
name='ProjectName',
version=__version__,
url='https://github.com/zeuxisoo/project-name/',
license='BSD',
author=__author__,
author_email=__email__,
description='The short of descriptions',
packages=find_packages(exclude=['tests', 'tests.*']),
include_package_data=True,
zip_safe=False,
platforms='any',
install_requires=open('requirements.txt').read().splitlines(),
)

編寫 requirements.txt 檔案

1
2
3
flask
flask-babel
flask-wtforms

編寫 MANIFEST.in 檔案

  • 當 setup.py 用到的文件都要在這記錄
1
2
3
4
5
include Makefile
include requirements.txt
recursive-include tests *
recursive-exclude tests *.pyc
recursive-exclude tests *.pyo

編寫簡單的 Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
.PHONY: clean clean-pyc

clean: clean-build clean-pyc

clean-build:
@rm -fr build/
@rm -fr dist/
@rm -fr *.egg-info

clean-pyc:
@find . -name '*.pyc' -exec rm -f {} +
@find . -name '*.pyo' -exec rm -f {} +
@find . -name '*~' -exec rm -f {} +

註冊 pypi 的帳號

1
2
3
4
1. 執行 python setup.py register
2. 選擇 2
3. 填入 帳號 密碼 電郵地址
4. 接收電郵並確認

登入 pypi 帳號

1
2
3
4
1. 執行 python setup.py register
2. 選擇 1
3. 之後填入 帳號 密碼
4. 最後提示已儲存到 /home/[user]/.pypirc

提交到 pypi

1
2
1. 執行 python setup.py sdist upload
2. 如無意外 *.tar.gz 就提交成功

打包出 tar.gz

1
2
1. 執行 python setup.py sdist
2. 之後 tar.gz 會出現在 dist 目錄

測試 tar.gz

1
2
3
4
5
6
7
1. 根據上面的方法先打包出 tar.gz
2. 建立測試目錄 (mkdir ~/Desktop/py && cd $_)
3. 建立測試環境 (virtualenv-2.7 --no-site-package venv)
4. 激活測試環境 (source venv/bin/activate)
5. 解壓縮 tar.gz 到 ~/Desktop/py 並進入到裡面
6. 嘗試安裝 python setup.py install
7. 如無意外就可以看見成功提示