前段时间在玩 OAuth2,尝试了很多个 Python 封装的 OAuth2 第三方库,感觉都不是很对自己胃口(最无语的是那个叫作 oauth2 的库,居然是 OAuth 1.0 的实现??!!),于是自己写了一个叫作 py-oauth2 的“库”。之所以取这个名字是因为之前用过一个叫 py-trello 的库,以为叫 py-xxx 会很洋气,现在想想真是土死了…

制作打包一个 Python Egg 并部署整个过程还蛮有意思的,下面小教程(这里以制作一个叫作 bee 的 Egg 为例)~

制作

先创建一个项目文件夹:

$mkdir bee-egg # 这个名字可以随便取$cdbee-egg

在里边建一个叫 bee 的文件夹(逻辑比较简单的话可以直接建一个叫作 bee.py 的文件),放业务逻辑代码:

mkdir beetouch bee/__init__.py

其中 bee/init.py 写几行代码:

# -*- coding: utf-8 -*-def hoot():return 'hum hum'def add(x, y):return x + y

在 bee-egg 目录中创建 Egg 的配置文件 setup.py :

#!/usr/bin/env pythonfrom setuptools import setup, find_packagessetup(name = 'bee',version = '0.0.1',keywords = ('bee', 'egg'),description = 'a simple egg',license = 'MIT License',url = 'http://liluo.org',author = 'liluo',author_email = 'i@liluo.org',packages = find_packages(),include_package_data = True,platforms = 'any',install_requires = [],)

当前目录文件结构如下:

bee-egg/bee/__init__.pysetup.py

打包

打包是最重要却又最简单的一步,执行:

python setup.py xxxx

即可。比较主流的2种打包格式:

# 以下所有生成文件将在当前路径下 dist 目录中python setup.py bdist_egg # 生成类似 bee-0.0.1-py2.7.egg,支持 easy_installpython setup.py sdist # 生成类似 bee-0.0.1.tar.gz,支持 pip

当然还有其他非主流格式或者其他选项,可以通过下面这个命令查看:

python setup.py --help-commands

部署到 PyPI

注册 Egg

可以通过 SSH 或者 web form ,SSH 步骤:

$python setup.py registerrunning registerrunning egg_infowriting bee.egg-info/PKG-INFOwriting top-level names to bee.egg-info/top_level.txtwriting dependency_links to bee.egg-info/dependency_links.txtreading manifest file 'bee.egg-info/SOURCES.txt'writing manifest file 'bee.egg-info/SOURCES.txt'running checkWe 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 foryou (and email it to you), or4. quitYour selection [default 1]:Username: liluoPassword:Registering bee to http://pypi.python.org/pypiServer response (200): OKI can store your PyPI login so future submissions will be faster.(the login will be stored in /Users/luo/.pypirc)Save your login (y/N)?y

上传到 PyPI

上传文件也是有 SSH 和 web form 2种方法。

web form: 在浏览器登陆到 PyPI 点击 Your packages 中 Egg 的项目,然后选择某个版本的 files 即可看到上传界面。

SSH:

$python setup.py sdist bdist_egg upload# 这里上传的是 .tar.gz 压缩包和 .egg 文件# 屏幕上大片信息输出,最后会看到running uploadSubmitting dist/bee-0.0.1.tar.gz to http://pypi.python.org/pypiServer response (200): OKSubmitting dist/bee-0.0.1-py2.7.egg to http://pypi.python.org/pypiServer response (200): OK

安装测试

先用 pip 安装:

$pip install beeDownloading/unpacking beeDownloading bee-0.0.1.tar.gzRunning setup.py egg_info forpackage beeInstalling collected packages: beeRunning setup.py install forbeeSuccessfully installed beeCleaning up...

测试:

$python>>> import bee>>> dir(bee)['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'add', 'hoot']>>> bee.add(1,3)4>>> from bee import hoot>>> hoot()'hum hum'

Yeah!现在世界人民都可以用 [bee]http://pypi.python.org/pypi/bee/ 这个 Python Egg 了~

卸载掉用 easy_install 试下:

$pip uninstall beeUninstalling bee:/Library/Python/2.7/site-packages/bee/Library/Python/2.7/site-packages/bee-0.0.1-py2.7.egg-infoProceed (y/n)? ySuccessfully uninstalled bee$easy_install beeSearching forbeeReading http://pypi.python.org/simple/bee/Reading http://liluo.orgBest match: bee 0.0.1Downloading http://pypi.python.org/packages/2.7/b/bee/bee-0.0.1-py2.7.egg#md5=6b5a719f1ca42367fb05d1e3eb45fbe5Processing bee-0.0.1-py2.7.eggMoving bee-0.0.1-py2.7.egg to /Library/Python/2.7/site-packagesAdding bee 0.0.1 to easy-install.pth fileInstalled /Library/Python/2.7/site-packages/bee-0.0.1-py2.7.eggProcessing dependencies forbeeFinished processing dependencies forbee

可以再重做一下上面的测试,验证一下。

Tips

pip install xxxx 时默认只会找对应的压缩包文件

当时我用 easy_install 安装没问题,但是 pip 每次都说找不到资源。我盯着 @hongqn 在 PyPI 上的 CaoE 足足2分钟才反应过来,原来它是需要一个压缩包格式的文件。

setup.py 中调用当前目录的文件一定要加 MANIFEST.in 并将调用文件 include 进来

使用 python setup.py sdist 打包时,如果 setup.py 调用了当前目录中的文件(如README.rst):

long_description = open('README.rst').read()

一定要在增加 MANIFEST.in 文件并将调用文件 include 进来,否则将导致用户在 pip install 时报文件找不到的错误,示例:

$cat MANIFEST.ininclude README.rst

偶尔去看一下自己的 Egg 在 PyPI 上的下载数也挺欢乐的

Version: 0.0.3py-oauth2-0.0.3.tar.gz(251)py_oauth2-0.0.3-py2.7.egg(218)Version: 0.0.2py-oauth2-0.0.2.tar.gz(376)py_oauth2-0.0.2-py2.6.egg(435)py_oauth2-0.0.2-py2.7.egg(304)Version: 0.0.1py_oauth2-0.0.1-py2.6.egg(478)

END