python的第三方模块越来越丰富,涉及的领域也非常广,如科学计算、图片处理、web应用、GUI开发等。当然也可以将自己写的模块进行打包或发布。一简单的方法是将你的类包直接copy到python的lib目录,但此方式不便于管理与维护,存在多个python版本时会非常混乱。现介绍如何编写setup.py来对一个简单的python模块进行打包。
一、编写模块
进入项目目录
#cd /home/pysetup
#vi foo.py
二、编写setup.py
#vi setup.py
更多参数说明见表:
三、setup.py参数说明
#python setup.py build # 编译
#python setup.py install #安装
#python setup.py sdist #生成压缩包(zip/tar.gz)
#python setup.py bdist_wininst #生成NT平台安装包(.exe)
#python setup.py bdist_rpm #生成rpm包
或者直接"bdist 包格式",格式如下:
#python setup.py bdist --help-formats
--formats=rpm RPM distribution
--formats=gztar gzip'ed tar file
--formats=bztar bzip2'ed tar file
--formats=ztar compressed tar file
--formats=tar tar file
--formats=wininst Windows executable installer
--formats=zip ZIP file
四、打包
#python setup.py sdist
running sdist
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 Myblog-1.0
making hard links in Myblog-1.0...
hard linking foo.py -> Myblog-1.0
hard linking setup.py -> Myblog-1.0
creating dist
tar -cf dist/Myblog-1.0.tar Myblog-1.0
gzip -f9 dist/Myblog-1.0.tar
removing 'Myblog-1.0' (and everything under it)
提示两条warning可以忽略,不影响打包,当然一个完善的项目必须有README及MANIFEST.in(项目文件清单)文件。
#ls dist
Myblog-1.0.tar.gz
五、安装
#tar -zxvf Myblog-1.0.tar.gz
#cd Myblog-1.0.tar.gz
#python setup.py install (此命令大家再熟悉不过了)
running install
running build
running build_py
creating build/lib.linux-x86_64-2.6
copying foo.py -> build/lib.linux-x86_64-2.6
running install_lib
copying build/lib.linux-x86_64-2.6/foo.py -> /usr/local/lib/python2.6/dist-packages
byte-compiling /usr/local/lib/python2.6/dist-packages/foo.py to foo.pyc
running install_egg_info
Writing /usr/local/lib/python2.6/dist-packages/Myblog-1.0.egg-info
六、测试
>>> from foo import MyClass
>>> app=MyClass()
>>> app.print printblog()
>>> app.printblog()
http://blog.liuts.com
>>> app.printBblog()
HTTP://BLOG.LIUTS.COM
>>>
参考文献:
http://docs.python.org/distutils/setupscript.html
如大家有什么疑问或感兴趣的话题可以通过weibo与我交流:http://t.qq.com/yorkoliu
一、编写模块
进入项目目录
#cd /home/pysetup
#vi foo.py
二、编写setup.py
#vi setup.py
更多参数说明见表:
三、setup.py参数说明
引用
#python setup.py build # 编译
#python setup.py install #安装
#python setup.py sdist #生成压缩包(zip/tar.gz)
#python setup.py bdist_wininst #生成NT平台安装包(.exe)
#python setup.py bdist_rpm #生成rpm包
或者直接"bdist 包格式",格式如下:
引用
#python setup.py bdist --help-formats
--formats=rpm RPM distribution
--formats=gztar gzip'ed tar file
--formats=bztar bzip2'ed tar file
--formats=ztar compressed tar file
--formats=tar tar file
--formats=wininst Windows executable installer
--formats=zip ZIP file
四、打包
#python setup.py sdist
引用
running sdist
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 Myblog-1.0
making hard links in Myblog-1.0...
hard linking foo.py -> Myblog-1.0
hard linking setup.py -> Myblog-1.0
creating dist
tar -cf dist/Myblog-1.0.tar Myblog-1.0
gzip -f9 dist/Myblog-1.0.tar
removing 'Myblog-1.0' (and everything under it)
提示两条warning可以忽略,不影响打包,当然一个完善的项目必须有README及MANIFEST.in(项目文件清单)文件。
#ls dist
Myblog-1.0.tar.gz
五、安装
#tar -zxvf Myblog-1.0.tar.gz
#cd Myblog-1.0.tar.gz
#python setup.py install (此命令大家再熟悉不过了)
引用
running install
running build
running build_py
creating build/lib.linux-x86_64-2.6
copying foo.py -> build/lib.linux-x86_64-2.6
running install_lib
copying build/lib.linux-x86_64-2.6/foo.py -> /usr/local/lib/python2.6/dist-packages
byte-compiling /usr/local/lib/python2.6/dist-packages/foo.py to foo.pyc
running install_egg_info
Writing /usr/local/lib/python2.6/dist-packages/Myblog-1.0.egg-info
六、测试
引用
>>> from foo import MyClass
>>> app=MyClass()
>>> app.print printblog()
>>> app.printblog()
http://blog.liuts.com
>>> app.printBblog()
HTTP://BLOG.LIUTS.COM
>>>
参考文献:
http://docs.python.org/distutils/setupscript.html
如大家有什么疑问或感兴趣的话题可以通过weibo与我交流:http://t.qq.com/yorkoliu
卡卡
2015/08/18 14:04
我想知道 在运行python setup.py install如果机器存在多个python版本,,系统会用哪个python:如:Writing /usr/local/lib/python2.6/dist-packages/Myblog-1.0.egg-info系统怎么指定应该写入python2.6下的dist-packages
fh.cn
2010/08/27 15:45
呵呵,来友情支持天师一下。
分页: 1/1 1