31. Python脚本学习笔记三十一程序打包
本篇名言:“问候是一只船能把情送远。祝福是一把伞。伴你走过阴雨天,思念是一支箭。能让心相连;拼搏的时候,给你一声问候,所有的坎坷,都化作云烟!”
最近,Python程序员开始习惯于程序打包发布。
Distutils是能让程序员发布Python包的工具。
1. Distutils基础
首先编写简单编写脚本,如下
Setup.py
from distutils.core import setup
setup(name='Hello',
version='1.0',
description='A simpleexample',
author='Magnus LieHetland',
py_modules=['hello'])
然后在setup.py的当前目录下,创建hello.py脚本,内容如下:
print "hello,world!"
就是一行
然后执行
Python setup.py build
输出如下:
>python setup.py build
running build
running build_py
creating build
creating build\lib
copying hello.py -> build\lib
会创建build的目录,其中包含lib的子目录。会把hello.py放到build/lib内。
Build目录是distutils组装包的工作区。
然后可以执行python setup.py install进行安装。
1.1 安装
执行:
Python setup.py install
输出如下:
running install
running build
running build_py
running install_lib
copying build\lib\hello.py ->D:\python27\Lib\site-packages
byte-compilingD:\python27\Lib\site-packages\hello.py to hello.pyc
running install_egg_info
WritingD:\python27\Lib\site-packages\Hello-1.0-py2.7.egg-info
成功将hello.py复制到了python安装路径下的lib目录。
同时将hello.py编译成hello.pyc。
这就是安装Python模块、包和扩展的标准机制。
其他可以参见“Installing Python Modules”。
2. 打包
可以执行python setup.py sdist进行打包:
E:\l>python setup.py sdist
running sdist
running check
warning: check: missing required meta-data:url
warning: check: missing meta-data: if'author' supplied, 'author_email' must be
supplied too
warning: sdist: manifest template'MANIFEST.in' does not exist (using default fi
le list)
warning: sdist: standard file not found:should have one of README, README.txt
writing manifest file 'MANIFEST'
creating Hello-1.0
copying files to Hello-1.0...
copying hello.py -> Hello-1.0
copying setup.py -> Hello-1.0
creating dist
creating 'dist\Hello-1.0.zip' and adding'Hello-1.0' to it
adding 'Hello-1.0\hello.py'
adding 'Hello-1.0\PKG-INFO'
adding 'Hello-1.0\setup.py'
removing 'Hello-1.0' (and everything underit)
命令执行完毕后
创建dist文件夹,同时会生成一个MANIFEST文件,MANIFEST文件包括所有文件的列表。
在dist文件夹中会有一个Hello-1.0.zip文件夹,该文件夹包含setup.py和相关的py文件,将zip文件发布给其他人可以利用内置的setup.py脚本解包和安装。
1.2 创建WINDOWS安装程序
通过bdist命令可以创建单一的windows或LINUX 的二进制安装程序。
执行如下命令:
E:\>python setup.py bdist--formats=wininst
running bdist
running bdist_wininst
running build
running build_py
creating build
creating build\lib
copying hello.py -> build\lib
installing to build\bdist.win32\wininst
running install_lib
creating build\bdist.win32
creating build\bdist.win32\wininst
creating build\bdist.win32\wininst\PURELIB
copying build\lib\hello.py ->build\bdist.win32\wininst\PURELIB
running install_egg_info
Writing build\bdist.win32\wininst\PURELIB\Hello-1.0-py2.7.egg-info
creating'c:\users\majusaka\appdata\local\temp\tmphltcul.zip' and adding '.' to
it
adding 'PURELIB\Hello-1.0-py2.7.egg-info'
adding 'PURELIB\hello.py'
removing 'build\bdist.win32\wininst' (andeverything under it)
然后会在dist目录下生成如下可安装的二进制文件
Hello-1.0.win32.exe(注意:这个EXE安装时需要系统已经安装了python的)
此外还可以考虑使用一些标准的安装程序
Inno Setup,InstallShield,
Wise installer ,
Installer VISE,
Nullsoft Scriptable Install System,
Youseful Windows Installer
Ghost Installer 等。
3. 编译扩展
在一个空目录中建立一个C文件,内容还是之前的回文字检查如下:
#include <Python.h>
static PyObject *is_palindrome(PyObject *self, PyObject *args) {
int i, n;
const char *text;
int result;
/* "s" means asingle string: */
if (!PyArg_ParseTuple(args,"s", &text)) {
return NULL;
}
/* The old code, more orless: */
n=strlen(text);
result = 1;
for (i=0; i<=n/2; ++i) {
if (text[i] !=text[n-i-1]) {
result = 0;
break;
}
}
/* "i" means asingle integer: */
returnPy_BuildValue("i", result);
}
/* A listing of our methods/functions: */
static PyMethodDef PalindromeMethods[] = {
/* name, function, argumenttype, docstring */
{"is_palindrome",is_palindrome, METH_VARARGS, "Detect palindromes"},
/* An end-of-listingsentinel: */
{NULL, NULL, 0, NULL}
};
/* An initialization function for the module (the name is
significant): */
PyMODINIT_FUNC initpalindrome() {
Py_InitModule("palindrome",PalindromeMethods);
}
命名为palindrome2.c
然后建立setup.py脚本,内容如下:
from distutils.core import setup,Exception
setup(name='palindrome',
version='2.0',
ext_modules=[
Extension('palindrome',['palindrome2.c'])
])
在该目录下执行
Python setup.py install
(蛤蟆这里以Linux为例,Linux原生态的GCC使用起来更加方便)
root@master python_zhizuo]# python setup.pyinstall
running install
running build
running build_ext
building 'palindrome' extension
creating build
creating build/temp.linux-x86_64-2.6
gcc -pthread -fno-strict-aliasing -O2 -g-pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector--param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv-DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions-fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE-fPIC -fwrapv -fPIC -I/usr/include/python2.6 -c palindrome2.c -obuild/temp.linux-x86_64-2.6/palindrome2.o
creating build/lib.linux-x86_64-2.6
gcc -pthread -sharedbuild/temp.linux-x86_64-2.6/palindrome2.o -L/usr/lib64 -lpython2.6 -obuild/lib.linux-x86_64-2.6/palindrome.so
running install_lib
copyingbuild/lib.linux-x86_64-2.6/palindrome.so ->/usr/lib64/python2.6/site-packages
running install_egg_info
Writing/usr/lib64/python2.6/site-packages/palindrome-2.0-py2.6.egg-info
会自动在安装前进行编译,同时会在build文件中得到palindrome.so文件。
如果要在当前目录中生成名为palindrome.so文件,可以直接执行如下命令:
python setup.py build_ext –inplace
是不是比上一章我们手动执行要方便很多?
4. 使用py2exe创建可执行程序
Py2exe是Distutils的扩展,可用来创建可执行的Windows程序。这样的exe是不需要再安装python解析器的。
下载时候注意版本,然后直接双击安装即可。
还是来看看具体如何使用:
创建一个hello.py,内容如下:
print "hello world!"
raw_input('Press <enter>')
创建setup.py如下:
from distutils.core import setup
import py2exe
setup(console=['hello.py'])
然后执行如下
Python setup.py py2exe
输出太多,蛤蟆截去后面一部分内容,如下:
copyingD:\python27\lib\site-packages\py2exe\run.exe -> E:\python_zhizuo\py2exe
dist\hello.exe
*** binary dependencies ***
Your executable(s) also depend on thesedlls which are not included,
you may or may not need to distribute them.
Make sure you have the license if youdistribute any of them, and
make sure you don't distribute filesbelonging to the operating system.
USER32.dll - C:\Windows\system32\USER32.dll
SHELL32.dll - C:\Windows\system32\SHELL32.dll
ADVAPI32.dll - C:\Windows\system32\ADVAPI32.dll
WS2_32.dll - C:\Windows\system32\WS2_32.dll
GDI32.dll - C:\Windows\system32\GDI32.dll
KERNEL32.dll - C:\Windows\system32\KERNEL32.dll
这样就得到独立于解析器的的可执行文件了。可以直接双击就能运行。
大家可以访问 Python Package Index (PyPI)的 Python包击中主页。