Python学习笔记_第十八章:程序打包

用于发布Python包的工具包Distutils能让程序员轻松地用Python编写安装脚本。这些脚本可以用来发布存档文件等。

Distutils基础

Distutils
如下为一个简单的安装脚本(setup.py)

from distutils.core import setup
setup(name = 'hello',
    version = '1.0',
    description = 'A simple example',
    author = 'hcs',
    py_modules = ['hello'])

setup函数中不一定得提供所有信息(可以不提供任何参数),也可以提供更多(author_email或者url)的参数
setuptools是建立在Distutils基础上的,包含一些增强功能,例如可以生成Python蛋,即用于发布Python包的可携带】单文件捆绑版本。还提供很多与Python package Index自动交互的功能。
安装Python模块、包和扩展的标准机制:

  • 准备hello.py(包、扩展)
  • 在同一目录下编写安装脚本(setup.py)
  • 执行:python setup.py build,输出如下:


    13298870-17bebf02de7c95df.png
    result
  • 执行:python setup.py install,输出如下:
    13298870-4e4809c26f30634a.png
    result

    上例中我们只使用了py_module指定,如果想要安装整个包的话,可以使用类似的方式执行packages指令。还能为Distutils创建配置文件,以设置各种属性。详情
    提供选项可以指定安装什么程序以及在哪安装,方法有:命令行选项开关、setup函数中的关键字参数以及Distutils的配置文件。

打包

写完setup.py脚本以后,就可以用它来建立存档文件,windows安装程序和RPM包。

建立存档文件

使用sdist命令
执行Python setup.py sdist命令,结果如下:


13298870-b46cb9a43bcc376a.png
result

此时根目录下的结构是这样的:


13298870-2d546e73050a2176.png
根目录结构

现在就可以将dist下的hello-1.0.zip发布给其他人,利用内置的setup.py解包并安装。dist中的压缩包格式可以设置,通过--formats设定。Python2.5内置的格式有:bztar、gztar、tar、zip(windows中默认的)和ztar。

创建Windows安装程序和RPM包

使用bdist命令可以创建单一的Windows安装程序和Linux RPM文件,bdist的可用格式有rpm和wininst。
执行 python setup.py bdist --formats=wininst 结果如下:


13298870-5c3c379dd52c30a6.png
result

有意思的是在非windows OS也可以为程序包建立windows安装程序,前提是没有任何需要编译的扩展。
使用真正的安装程序
如果想要更加专业的安装界面(尤其用py2exe生成可执行程序时)可以考虑使用一些标准的安装程序,例如:Inno Setup它和py2exe所创建的可执行文件配合相当好,并且提供一般的安装体验,有卸载功能。
更Python化,使用McMillan installer,可以代替py2exe。其他的选择有:InstallShieldWiseinstallerInstaller VISENullsoft Scriptable Install System、Youseful Windows Installer和Ghost Installer

编译扩展

from distutils.core import setup, Extension
setup(name = 'palindrome',
      version = '1.0',
      ext_modules = [Extension('palindrome', ['palindrome2.c'])])

执行:python setup.py build_ext --inplace,结果如下:


13298870-53a9a88dbcd892dc.png
result

如果安装了SWIG,Distutils可以直接调用它,安装脚本如下:

from distutils.core import setup, Extension
setup(name = 'palindrome',
      version = '1.0',
      ext_modules = [Extension('palindrome', ['palindrome.c', 'palindrome.i'])])

执行:python setup.py build_ext --inplace,结果如下:

13298870-bcbfb4302d53e646.png
result

上面结果中报:fatal error LNK1120: 1 unresolved externals错误,未解决。
如果报:''error: Unable to find vcvarsall.bat''错误,解决方案如下:
https://blog.csdn.net/yuer158462008/article/details/68957646

使用py2exe创建可执行程序

py3exe作为Distutils的扩展(可在http://www.py2exe.org上获得)。可以用来生成Windows的可执行程序(.exe文件,Distutils的sdist命令也生成了.exe文件),如果不想让用户单独安装Python解释器的话,它就大显神威了。
生成可执行文件后可能还需要一个安装程序,如Inno Setup(http://jrsoftware.org/isinfo.php),来发布可执行程序及py2exe生成的附加文件。
一个例子
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,结果如下:

C:\Users\hcs\Desktop\py2exe>python setup.py py2exe
running py2exe
creating C:\Users\hcs\Desktop\py2exe\build
creating C:\Users\hcs\Desktop\py2exe\build\bdist.win-amd64
creating C:\Users\hcs\Desktop\py2exe\build\bdist.win-amd64\winexe
creating C:\Users\hcs\Desktop\py2exe\build\bdist.win-amd64\winexe\collect-2.7
creating C:\Users\hcs\Desktop\py2exe\build\bdist.win-amd64\winexe\bundle-2.7
creating C:\Users\hcs\Desktop\py2exe\build\bdist.win-amd64\winexe\temp
creating C:\Users\hcs\Desktop\py2exe\dist
*** searching for required modules ***
*** parsing results ***
creating python loader for extension 'unicodedata' (D:\Program Files\Python2.7\DLLs\unicodedata.pyd -> unicodedata.pyd)
creating python loader for extension 'select' (D:\Program Files\Python2.7\DLLs\select.pyd -> select.pyd)
creating python loader for extension '_hashlib' (D:\Program Files\Python2.7\DLLs\_hashlib.pyd -> _hashlib.pyd)
creating python loader for extension 'bz2' (D:\Program Files\Python2.7\DLLs\bz2.pyd -> bz2.pyd)
*** finding dlls needed ***
*** create binaries ***
......
*** copy dlls ***
copying C:\WINDOWS\system32\python27.dll -> C:\Users\hcs\Desktop\py2exe\dist
setting sys.winver for 'C:\Users\hcs\Desktop\py2exe\dist\python27.dll' to 'py2exe'
copying D:\Program Files\Python2.7\lib\site-packages\py2exe\run.exe -> C:\Users\hcs\Desktop\py2exe\dist\hello.exe

*** binary dependencies ***
Your executable(s) also depend on these dlls which are not included,
you may or may not need to distribute them.

Make sure you have the license if you distribute any of them, and
make sure you don't distribute files belonging 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

编译后根目录为:

13298870-9b2a81248ae54162.png
root

dist目录为:
13298870-fa2a3441760b7a5b.png
dist

双击或在命令行都可以运行hello.exe,命令行结果如下:
13298870-63789cedc0cfbae5.png
result

关于py2exe如何工作以及如何以更高级的方法使用它的信息,都可以在py2exe的网站( http://www.py2exe.org)上找到。
就想也怕巷子深
新的软件可以在很多网站上声明,例如Freshmeat( http://freshmeat.net)。但还有一个叫做Python Package Index(PyPi)的Python包集中主页可供选择。
Distutils命令:可以使用如:build、build_ext、install、sdist和bdist。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值