接上一篇帖子 https://bbs.icxbk.com/group-topic-id-57159.html
Distutils是每个Python程序员工具包内的基础工具。而且事实上Distutils能做的比基于脚本的Python库安装程序还要多:它可以用来建立简单的Windows安装程序,再加上扩展程序py2exe,就能建立独立的Windows可执行程序了。
Setup.py脚本:
from distutils.core import setup
setup(name=\'Hello\'.
version=\'1.0\'
description=\'A simple example\',
author=\'Magnus Lie Hetland\',
py_modules=)
将脚本存储为setup . py (Distutils安装脚本的惯例),确保在同一个目录下存在名为hello.py的模块文件。
运行
python setup.py命令输出:
usage: setup.py cmdl ...]
or: setup.py一help
or: setup.py一help-commands
or: setup.py cmd一help
error: no commands supplied
使用build命令:python setup.py build
输出:
running build
running build_py
creating build
creating build/lib
copying hello.py->build/lib
Distutils建了build的子目录,其中包含名为lib的子目录,并且把hello.py的一个副本放置在build/lib内。bui 1 d子目录是Distutils组装包(以及编译扩展库等)的工作区。在安装的时候不需要运行build命令—如果需要的话,在运行install命令的时候它就自动运行了。
running install
running build
running build_Py
running install_ lib
copying build/lib/hello.py->/path/to/python/lib/python2.5/site-packages
byte-compiling /path/to/python/lib/python2.5/site-packages/hello.py to hello.pyc
这就是安装Python模块、包和扩展的标准机制。
使用sdist命令(用于“源代码发布”) :python setup.py sdist存档文件,输出如下
writing manifest file \'MANIFEST\'
creating Hello-1.0
making hard links in Hello-1.0...
hard linking hello.py -> Hello-1.0
hard linking setup.py ->Hello-1.0
tar -cf dist/Hello-1.0.tar Hello-1.0
gzip一f9 dist/Hello-1.0.tar
removing \'Hello-1.0\' (and everything under it)
现在除了build子目录外,应该还有一个dist子目录。可以在它里面找到一个叫做Hello-1.0.tar.gz的gzip格式的tar存档文件。
假设已经在当前(空) 目录中放置了源文件palindrome2.c,下面的setup.py脚本可以用于编译(和安装):
from distutils.core import setup,Extension
setup(name=\'palindrome\',
version=\'1.0\',
ext_modules=<> <> <> <> )
])
如果使用install命令运行这个setup.py脚本,palindrome扩展模块应该会在安装前自动编译。不是指定模块名字的列表,而是将Extension实例的列表提供给ext_mdules参数。构造函数会获取一个名称和相关文件的列表—例如,这将是指定头文件(.h)的地方。
Py2exe包可以创建拥有GUI的可执行文件。
print \'Hello, world!’
raw_input(\'Press
再找个只包含这个名为hello.py的文件的空目录,像下面这样创建setup.py;
from distutils.core import setup
import py2exe
setup(console=)
可以像下面这样运行脚本:
python setup.py py2exe
创建控制台应用程序(hello.exe)以及位于dist子目录中的其他一些文件。可以从命令行运行,或者直接双击运行。