花一天时间随便找了几篇文章入门Python,并且写了几个Python小程序想和朋友分享一下顺便推荐这个很棒的编程语言,但是遗憾的是朋友的电脑没有安装Python,而且还是用Windows系统,所以最好的办法是把Python程序转换为Windows上的可执行文件。
首先要下载“适当版本“的Python:
http://www.python.org/getit/
还需要一个叫py2exe的程序:
http://www.py2exe.org/
把他们分别运行安装。
以一个简单的程序为例: print "Hello World!" 如果要运行这个程序,可以: D:/test>python helloworld.py Hello World! D:/test> 接下来创建安装脚本,在脚本中告诉安装程序你要在安装过程做什么事还有需要安装哪些文件,将下列内容写入setup.py: from distutils.core import setup import py2exe setup(console=['helloworld.py']) py2exe使用一个新命令来扩展Distutils如果你安装第三方模块,那么使用下面的命令,它可以安装你需要的Python模块或包: D:/test>python setup.py install running install running build running install_egg_info Removing c:/Python27/Lib/site-packages/UNKNOWN-0.0.0-py2.7.egg-info Writing c:/Python27/Lib/site-packages/UNKNOWN-0.0.0-py2.7.egg-info D:/test> 然后运行你的安装脚本: D:/test>python setup.py py2exe running py2exe *** searching for required modules *** *** parsing results *** creating python loader for extension 'unicodedata' (c:/Python27/DLLs/unicodedata .pyd -> unicodedata.pyd) creating python loader for extension 'select' (c:/Python27/DLLs/select.pyd -> se lect.pyd) creating python loader for extension '_hashlib' (c:/Python27/DLLs/_hashlib.pyd - > _hashlib.pyd) creating python loader for extension 'bz2' (c:/Python27/DLLs/bz2.pyd -> bz2.pyd) *** finding dlls needed *** *** create binaries *** *** byte compile python files *** byte-compiling D:/test/build/bdist.win32/winexe/temp/_hashlib.py to _hashlib.pyc # ***省略下面的输出内容*** D:/test> 现在你已经生成了Python程序对应的可执行文件,测试一下是否正确运行: D:/test>cd dist D:/test/dist>helloworld.exe Hello, World! D:/test/dist> Python的运行需要一个叫MSVCR90.dll,也就是VS2008带有的DLL,如果对方的系统没有带有这个文件,你可能需要在你的安装包里加上这个文件。从微软的网站可以下载到带有这个文件的安装包 Microsoft Visual C++ 2008 Redistributable Package。 最后,你可能需要构造一个安装程序,让这些文件的安装过程更直观简便, NSIS (Nullsoft Scriptable Install System)就是一个开源免费而且功能强大的安装程序制作软件: