Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38197
  • 博文数量: 22
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 237
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-18 20:02
个人简介

人生最大的悲哀莫过于迷失自我而无法自拔!

文章分类

全部博文(22)

文章存档

2013年(11)

2012年(11)

我的朋友

分类: LINUX

2013-02-01 22:57:12

python程序打包
Distutils用于发布Python包的工具包,能让程序员轻松地用Python编写安装脚本。



1.示例:
[root@web-db distutils]# cat setup.py
#!/usr/local/bin/python


from distutils.core import setup


setup(name='Hello',
      version='1.0',
      description='A Simple Example',
      author='Magnus Lie Hetland',
      author_email='python@python.org',
      py_modules=['hello'])




[root@web-db distutils]# cat hello.py
#!/usr/local/bin/python


print "Hello world!"
[root@web-db distutils]# python setup.py install
running install
running build
running build_py
creating build
creating build/lib
copying hello.py -> build/lib
running install_lib
copying build/lib/hello.py -> /usr/local/lib/python2.7/site-packages
byte-compiling /usr/local/lib/python2.7/site-packages/hello.py to hello.pyc
running install_egg_info
Writing /usr/local/lib/python2.7/site-packages/Hello-1.0-py2.7.egg-info
[root@web-db distutils]# python
Python 2.7.3 (default, Jan  3 2013, 20:50:15)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
Hello world!
>>> exit()
经过测试hello模块正常安装并且正常工作。


2.打包:
用完供用户安装模块使用的setup.py脚本以后,就可以用他来建立存档文件,Windows安装程序
或者RPM包。
1)建立存档文件:
使用sdist命令(用于“源代码发布”)
[root@web-db distutils]# python setup.py sdist


.......
File "/usr/local/lib/python2.7/tarfile.py", line 425, in __init__
    raise CompressionError("zlib module is not available")
tarfile.CompressionError: zlib module is not available
.......
下载最新版的zlib包然后安装即可解决问题。
[root@web-db distutils]# ll dist/
total 4
-rw-r--r-- 1 root root 509 Jan 15 23:16 Hello-1.0.tar.gz
[root@web-db distutils]# cd dist/
[root@web-db dist]# ls
Hello-1.0.tar.gz
[root@web-db dist]# tar zxvf Hello-1.0.tar.gz
Hello-1.0/
Hello-1.0/setup.py
Hello-1.0/PKG-INFO
Hello-1.0/hello.py
[root@web-db dist]# cat Hello-1.0/setup.py
#!/usr/local/bin/python


from distutils.core import setup


setup(name='Hello',
      url='',
      version='1.0',
      description='A Simple Example',
      author='Magnus Lie Hetland',
      author_email='python@python.org',
      py_modules=['hello'])


[root@web-db dist]# cat Hello-1.0/hello.py
#!/usr/local/bin/python


print "Hello world!"


在创建源代码发布程序时,程序会同时创建叫做MANIFEST的文件,其中包括所有文件的列表。
MANIFEST.in文件时清单(manifest)的模板。在指明安装内容时用到,可以使用如下命令来制定想要
包含的文件。如果Distutils自己没有指明要安装的文件,可以使用setup.py脚本(以及默认包含的文件,
比如README)。
如果重构了包并且想要重新打包,那么请删除MANIFEST文件,以便于重新开始打包。


3.创建Windows安装程序或RPM包:
使用bdist命令可以创建单一的Windows安装程序和Linux RPM文件(一般来说可以用它来创建
二进制发布程序,其中的扩展已经由于特定结构而被编译。bdist可用的格式(除了sdist可用的
格式之外)有rpm(针对RPM包)和wininst(针对Windows可执行安装程序)。


在非Windows操作系统内也可以为程序包建立Windows安装程序,前提是没有任何需要编译的扩展。
[root@web-db distutils]# python setup.py bdist --formats=wininst
running bdist
running bdist_wininst
running build
running build_py
installing to build/bdist.linux-x86_64/wininst
running install_lib
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/wininst
creating build/bdist.linux-x86_64/wininst/PURELIB
copying build/lib/hello.py -> build/bdist.linux-x86_64/wininst/PURELIB
running install_egg_info
Writing build/bdist.linux-x86_64/wininst/PURELIB/Hello-1.0-py2.7.egg-info
creating '/tmp/tmpelQrJy.zip' and adding '.' to it
adding 'PURELIB/hello.py'
adding 'PURELIB/Hello-1.0-py2.7.egg-info'
Warning: Can't read registry to find the necessary compiler setting
Make sure that Python modules _winreg, win32api or win32con are installed.
removing 'build/bdist.linux-x86_64/wininst' (and everything under it)

阅读(1065) | 评论(0) | 转发(0) |
0

上一篇:python网络编程

下一篇:python图形用户界面

给主人留下些什么吧!~~