【Python】python打包~~
06 Nov 2013刚折腾了一下python打包本地代码并发布到pypi上,一股狂拽炫酷吊霸天的感觉。
我拿之前写的一个链接检测器来做实验,现在已经上传的了pypi上,no picture you say ge jb
占位
ok, 开工~
1. 创建setup.py
在需打包的目录下新建一个setup.py
那么里边写什么内容呢,有模板~~~
#! /usr/bin/env python #coding=utf-8 from distutils.coreimport setup setup( name='MyLib', version='1.0', description='My Lib disribution Utility', author='flowerowl', author_email='lazynightz@gmail.com', url='http://lazynight.me', py_modules=['mylib'], )
每次打包都要这样?太费劲呢,ok有一个自动生成setup.py的工具:paster
pip install paster
搞定,现在输入 paster create xxx
╭─@Flowerowl.local ~/z/pj/pylink ╰─paster create Selected and implied templates: PasteScript#basic_package A basic setuptools-enabled package Enter project name: pylinktester Variables: egg: pylinktester package: pylinktester project: pylinktester Enter version (Version (like 0.1)) ['']: 0.1 Enter description (One-line description of the package) ['']: a link tester written in python Enter long_description (Multi-line description (in reST)) ['']: Enter keywords (Space-separated keywords/tags) ['']: python tester Enter author (Author name) ['']: yuzhe Enter author_email (Author email) ['']: lazynight@gmail.com Enter url (URL of homepage) ['']: http://lazynight.me Enter license_name (License name) ['']: Enter zip_safe (True/False: if the package can be distributed as a .zip file) [False]: Creating template basic_package Creating directory ./pylinktester Recursing into +package+ Creating ./pylinktester/pylinktester/ Copying __init__.py to ./pylinktester/pylinktester/__init__.py Copying setup.cfg to ./pylinktester/setup.cfg Copying setup.py_tmpl to ./pylinktester/setup.py Running /usr/bin/python setup.py egg_info
好了,现在目录下生成一个新的pylinktester目录,里边有setup.py文件,copy过来就可以。
不放心? 可以检查一下:
python setup.py check
有错误会提示,没错误就过去了。
2. 打包
然后我们开始打包。
╭─@Flowerowl.local ~/z/pj/pylink ╰─ python setup.py sdist running sdist running egg_info writing pylinktester.egg-info/PKG-INFO writing top-level names to pylinktester.egg-info/top_level.txt writing dependency_links to pylinktester.egg-info/dependency_links.txt reading manifest file 'pylinktester.egg-info/SOURCES.txt' writing manifest file 'pylinktester.egg-info/SOURCES.txt' warning: sdist: standard file not found: should have one of README, README.rst, README.txt running check creating pylinktester-0.1 creating pylinktester-0.1/core creating pylinktester-0.1/model creating pylinktester-0.1/pylinktester.egg-info creating pylinktester-0.1/threads creating pylinktester-0.1/utils making hard links in pylinktester-0.1... hard linking setup.py -> pylinktester-0.1 hard linking core/__init__.py -> pylinktester-0.1/core hard linking core/bfs.py -> pylinktester-0.1/core hard linking core/demo.py -> pylinktester-0.1/core hard linking core/test.py -> pylinktester-0.1/core hard linking model/__init__.py -> pylinktester-0.1/model hard linking model/urlQuence.py -> pylinktester-0.1/model hard linking model/urlSync.py -> pylinktester-0.1/model hard linking pylinktester.egg-info/PKG-INFO -> pylinktester-0.1/pylinktester.egg-info hard linking pylinktester.egg-info/SOURCES.txt -> pylinktester-0.1/pylinktester.egg-info hard linking pylinktester.egg-info/dependency_links.txt -> pylinktester-0.1/pylinktester.egg-info hard linking pylinktester.egg-info/not-zip-safe -> pylinktester-0.1/pylinktester.egg-info hard linking pylinktester.egg-info/top_level.txt -> pylinktester-0.1/pylinktester.egg-info hard linking threads/__init__.py -> pylinktester-0.1/threads hard linking threads/spliderThread.py -> pylinktester-0.1/threads hard linking threads/testThread.py -> pylinktester-0.1/threads hard linking threads/threadController.py -> pylinktester-0.1/threads hard linking utils/__init__.py -> pylinktester-0.1/utils hard linking utils/getRequest.py -> pylinktester-0.1/utils hard linking utils/getUrls.py -> pylinktester-0.1/utils hard linking utils/testUrls.py -> pylinktester-0.1/utils Writing pylinktester-0.1/setup.cfg Creating tar archive removing 'pylinktester-0.1' (and everything under it)
搞定,现在目录下生成一个新目录sdist,里边有你的源码包
但是你,观察一下pylinktester.egg-info里的SOURCE.TXT
里边没有main.py(这是我的程序里的启动文件)
在SOURCES.txt里加入main.py,搞定。
现在呢,你想把它发布到pypi源上,多么上档次。
上传软件到pypi需要三个步骤。
- 注册你自己
- 注册你的软件
- 上传你通过
setup.py sdist
和setup.py bdist_*
创建的包。
要注册自己,访问 PyPI用户注册页面。输入你想要的用户名和密码,提供一个有效的电子邮件地址,然后点击 Register
按钮。(如果你有一个 PGP 或 GPG 密钥,你也可以提供。如果你没有或者不知道这是什么意思,不用担心。)检查你的电子邮件,在几分钟之内,你应该会收到一封来自 PyPI 的包含验证链接的邮件。点击链接以完成注册过程。
然后去$HOME 目录下新建一个 .pypirc文件,在里边写入:
[pypirc]
servers = pypi
[server-login]
username:flowerowl
password:xxxxxxxx
搞定,现在我们去上传~!
╭─@Flowerowl.local ~/z/pj/pylink ╰─ python setup.py register sdist upload running register running egg_info writing pylinktester.egg-info/PKG-INFO writing top-level names to pylinktester.egg-info/top_level.txt writing dependency_links to pylinktester.egg-info/dependency_links.txt reading manifest file 'pylinktester.egg-info/SOURCES.txt' writing manifest file 'pylinktester.egg-info/SOURCES.txt' running check Registering pylinktester to http://pypi.python.org/pypi Server response (200): OK running sdist warning: sdist: standard file not found: should have one of README, README.rst, README.txt creating pylinktester-0.1 creating pylinktester-0.1/core creating pylinktester-0.1/model creating pylinktester-0.1/pylinktester.egg-info creating pylinktester-0.1/threads creating pylinktester-0.1/utils making hard links in pylinktester-0.1... hard linking setup.py -> pylinktester-0.1 hard linking core/__init__.py -> pylinktester-0.1/core hard linking core/bfs.py -> pylinktester-0.1/core hard linking core/demo.py -> pylinktester-0.1/core hard linking core/test.py -> pylinktester-0.1/core hard linking model/__init__.py -> pylinktester-0.1/model hard linking model/urlQuence.py -> pylinktester-0.1/model hard linking model/urlSync.py -> pylinktester-0.1/model hard linking pylinktester.egg-info/PKG-INFO -> pylinktester-0.1/pylinktester.egg-info hard linking pylinktester.egg-info/SOURCES.txt -> pylinktester-0.1/pylinktester.egg-info hard linking pylinktester.egg-info/dependency_links.txt -> pylinktester-0.1/pylinktester.egg-info hard linking pylinktester.egg-info/not-zip-safe -> pylinktester-0.1/pylinktester.egg-info hard linking pylinktester.egg-info/top_level.txt -> pylinktester-0.1/pylinktester.egg-info hard linking threads/__init__.py -> pylinktester-0.1/threads hard linking threads/spliderThread.py -> pylinktester-0.1/threads hard linking threads/testThread.py -> pylinktester-0.1/threads hard linking threads/threadController.py -> pylinktester-0.1/threads hard linking utils/__init__.py -> pylinktester-0.1/utils hard linking utils/getRequest.py -> pylinktester-0.1/utils hard linking utils/getUrls.py -> pylinktester-0.1/utils hard linking utils/testUrls.py -> pylinktester-0.1/utils Writing pylinktester-0.1/setup.cfg Creating tar archive removing 'pylinktester-0.1' (and everything under it) running upload Submitting dist/pylinktester-0.1.tar.gz to http://pypi.python.org/pypi Server response (200): OK
大功告成~!
感受一下吧~
转载请注明:于哲的博客 » 【Python】python打包~~