Python打包成可执行的exe文件

上一篇 / 下一篇  2010-09-17 17:39:32 / 个人分类:Python

    网上找了很多信息,就一个合适我的python3.0final版本。
 
    先下载cx_freeze:http://sourceforge.net/projects/cx-freeze/ 合适我的pathon的是cx_Freeze-4.1.win32-py3.0.msi
    然后找相关资料,按照步骤来做。
    听说linux下是这样安装的:rmp -i InstallerName  (没试过,应该是正确的吧,对linux不熟)
    window下只需要下一步下一步就行
 
    听说有两种编译方式:
    1) cxfreeze hello.py [存放编译结果的目录路径] dist  (这个我试过,不成功,暂时放弃)
    2) 先写一个setup.py文件:  (我用的就是这个方法)
    import sys
    sys.path[0] = 'usr/lib/site-packages'  #根据site-packages实际的相对路径写
    from cx_Freeze import setup, Executable
    setup(name = "hello", version = "0.1"s description = "hello test", executables = [Executable("hello.py")])
    然后运行这个命令python setup.py build,这就OK了啦 (这个要在CMD里面执行)
 
   如果要在python程序里面输入中文字符,最好的办法是在文件头部包含中文字符库:
   from encodings import gbk
==============================================================
上面这部分是我2010年9月17号下午五点多完成的。
四年之后的今天2014年9月25号,我找到这篇文章,开始给我的TestResultToTestLink项目打包。
这个项目有以下几个文件:
1. 需要import一个第三方的Lib,Lib放在C:\Python30\Lib\xmlrpc
2. 项目有3个py文件,然后需要读取两个txt文件.
 
打包工具:
cx_Freeze-4.1.win32-py3.0
 
方法一:
创建个setup.py文件,写入以下内容:
import sys
sys.path[0] = "/Lib/site-packages"
from cx_Freeze import setup
from cx_Freeze import Executable
setup(name="ReportTestResultToTestLink", version="1.0", description="the first release", executables=[Executable("C:\workspace\ReportTestResultToTestLink\ReportTestResultToTestLink.py")])
然后启动CMD,进入Python30:
运行以下命令:python.exe setup.py build
得到的结果是:
running build
running build_exe
coyping C:\Python30\lib\site-packages\cx_Freeze\bases\Console.exe -> build\exe.w
in32-3.0\ReportTestResultToTestLink.exe
coyping C:\Windows\system32\python30.dll -> build\exe.win32-3.0\python30.dll
writing zip file build\exe.win32-3.0\library.zip
  Name                      File
  ----                      ----
m BUILD_CONSTANTS
m __future__
m __main__
(....中间一段省略......)
m unittest
m warnings
m zipimport
m zlib
Missing modules:
? ServerInfo imported from reporttestresulttotestlink__main__
? TestLinkAPIClient imported from reporttestresulttotestlink__main__
coyping C:\Python30\DLLs\select.pyd -> build\exe.win32-3.0\select.pyd
coyping C:\Python30\DLLs\unicodedata.pyd -> build\exe.win32-3.0\unicodedata.pyd
然后我在C:\Python30\build\exe.win32-3.0中看到有4个文件生成,其中一个是ReportTestResultToTestLink.exe
运行这个EXE,得到错误提示:
Traceback (most recent call last):
  File "C:\Python30\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 2
7, in <module>
    exec(code, m.__dict__)
  File "C:\workspace\ReportTestResultToTestLink\ReportTestResultToTestLink.py",
line 1, in <module>
    import ServerInfo
ImportError: No module named ServerInfo
"ServerInfo"是我一个py文件。然后我又把相关的文件拷贝到exe所在的文件夹。最后得到的提示,说是我import的那个第三方module找不到。于是我在setup里面加入这样一行:
sys.path[1] = "/Lib/xmlrpc"
再试过还是不行!唉!!!
 
方法二:
启动CMD,进入C:\Python30\Scripts
执行命令:
cxfreeze.bat "C:\workspace\ReportTestResultToTestLink\Report
TestResultToTestLink.py" --target-dir dist
最后在C:\Python30\Scripts\dist中看到有几个文件生成,然后我再用CMD进入这个地址,执行ReportTestResultToTestLink.exe,结果完全正确!!!
 
当然这第二个方法,我也走了不少弯路,尝试过以下这些,统统失败:
===============================================
C:\Python30\Scripts>cxfreeze.bat --include-path="C:\Python30\Lib\xmlrpc" --init-
script="C:\workspace\ReportTestResultToTestLink\ReportTestResultToTestLink.py"
Usage: cxfreeze [options] [SCRIPT]
Freeze a Python script. and all of its referenced modules to a base
executable which can then be distributed without requiring a Python
installation.
cxfreeze: error: script. or a list of modules must be specified
===============================================
C:\Python30\Scripts>cxfreeze.bat --include-path="C:\Python30\Lib\xmlrpc" --init-
script="C:\workspace\ReportTestResultToTestLink\ReportTestResultToTestLink.py" -
-include-modules="C:\workspace\ReportTestResultToTestLink\TestConfig.py"
Usage: cxfreeze [options] [SCRIPT]
Freeze a Python script. and all of its referenced modules to a base
executable which can then be distributed without requiring a Python
installation.
cxfreeze: error: script. or a target name must be specified
===============================================
C:\Python30\Scripts>cxfreeze.bat --include-path="C:\Python30\Lib\xmlrpc" --init-
script="C:\workspace\ReportTestResultToTestLink\ReportTestResultToTestLink.py" -
-include-modules="C:\workspace\ReportTestResultToTestLink\ServerInfo.py" --targe
t-name=1
Traceback (most recent call last):
  File "c:\python30\Scripts\cxfreeze", line 5, in <module>
    main()
  File "C:\Python30\lib\site-packages\cx_Freeze\main.py", line 179, in main
    freezer.Freeze()
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 418, in Freeze
    self._FreezeExecutable(executable)
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 119, in _Freez
eExecutable
    finder = self._GetModuleFinder(exe)
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 261, in _GetMo
duleFinder
    finder.IncludeModule(name)
  File "C:\Python30\lib\site-packages\cx_Freeze\finder.py", line 435, in Include
Module
    module = self._ImportModule(name, deferredImports)
  File "C:\Python30\lib\site-packages\cx_Freeze\finder.py", line 231, in _Import
Module
    raise ImportError("No module named %s" % name)
ImportError: No module named C:\workspace\ReportTestResultToTestLink\ServerInfo.
py
 
C:\Python30\Scripts>cxfreeze.bat --include-path="C:\Python30\Lib\xmlrpc" --init-
script="C:\workspace\ReportTestResultToTestLink\ReportTestResultToTestLink.py" -
-include-modules="ServerInfo.py" --target-name=1
Traceback (most recent call last):
  File "c:\python30\Scripts\cxfreeze", line 5, in <module>
    main()
  File "C:\Python30\lib\site-packages\cx_Freeze\main.py", line 179, in main
    freezer.Freeze()
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 418, in Freeze
    self._FreezeExecutable(executable)
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 119, in _Freez
eExecutable
    finder = self._GetModuleFinder(exe)
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 261, in _GetMo
duleFinder
    finder.IncludeModule(name)
  File "C:\Python30\lib\site-packages\cx_Freeze\finder.py", line 435, in Include
Module
    module = self._ImportModule(name, deferredImports)
  File "C:\Python30\lib\site-packages\cx_Freeze\finder.py", line 231, in _Import
Module
    raise ImportError("No module named %s" % name)
ImportError: No module named ServerInfo.py
==============================================
C:\Python30\Scripts>cxfreeze.bat --include-path="C:\Python30\Lib\xmlrpc" --init-
script="C:\workspace\ReportTestResultToTestLink\ReportTestResultToTestLink.py" -
-include-modules="ServerInfo" --target-name=1
Traceback (most recent call last):
  File "c:\python30\Scripts\cxfreeze", line 5, in <module>
    main()
  File "C:\Python30\lib\site-packages\cx_Freeze\main.py", line 179, in main
    freezer.Freeze()
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 418, in Freeze
    self._FreezeExecutable(executable)
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 119, in _Freez
eExecutable
    finder = self._GetModuleFinder(exe)
  File "C:\Python30\lib\site-packages\cx_Freeze\freezer.py", line 261, in _GetMo
duleFinder
    finder.IncludeModule(name)
  File "C:\Python30\lib\site-packages\cx_Freeze\finder.py", line 435, in Include
Module
    module = self._ImportModule(name, deferredImports)
  File "C:\Python30\lib\site-packages\cx_Freeze\finder.py", line 231, in _Import
Module
    raise ImportError("No module named %s" % name)
ImportError: No module named ServerInfo
竟然统统失败!!!!!我表示很无语。以后我会专门找时间来解决这些历史遗留问题的,现在就用这个成功的方法来完成我的项目算了。

TAG: Python python

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 52604
  • 日志数: 17
  • 建立时间: 2009-06-29
  • 更新时间: 2015-04-24

RSS订阅

Open Toolbar