python32 cx_Freeze 生成python exe文件

来源:互联网 发布:去痘印 知乎 编辑:程序博客网 时间:2024/05/29 08:22

** 遇到问题“No module named re”,参照step 5

方法一: cxfreeze hello.py --target-dir dist --base-name=win32gui
               

              在用方法二的时候,在运行生成的exe的时候,遇到了“ImportError: cannot import name _ChainMap”问题,暂时没有解决。所以用方法一  


方法二:

step1 : download cx_Freeze-4.2.3.win32-py3.2.msi  in sourceforge website and install it

step2 : add the c:\python32\Scripts加到环境变量

step3: 写setup.py,放到目标py(rock.py)同一目录下。

 setyp.py的内容如下:

# -*- coding: utf-8 -*-  '''description:python32 to exethe step:cxfreeze hello.py --target-dir dist --base-name=win32gui base-name=win32gui  作用是=生成win32gui窗口,不生成dos窗口  '''import sys  import refrom cx_Freeze import setup, Executablebase = Noneif sys.platform == "win32":    base = "Win32GUI"setup(name = "rock",  version = "1.0",description = "rock game",executables = [Executable("rock.py",base = "Win32GUI",icon = "app.ico")])


setup的写法可参考:http://cx-freeze.sourceforge.net/cx_Freeze.html

step4:in cmd dos window,you can run into the rock.py dir,and input  "cxfreeze setup.py --target-dir install  ",此时生成的exe及相关文件会放到当前目录的子目录dist下

stept5: double click the exe just generated,i will run the rock.py smoothly. if it disapper at once,nothing show.you can drag the exe to cmd window,and see what error it report. in my first time ,it repot “No module named re”,so i add "import re" in my dist file (rock.py) and regenerate the exe again,it works.



三): 生成exe时候遇到 的一个奇怪问题:

做了一个监控主动出图的程序(StartPeriodical.py)是否正常的程序,如下:

#StartPeriodical.py# -*- conding:utf-8 -*-import subprocessimport reimport randomfrom datetime import datetimeimport sysimport time#p=subprocess.call(["python", "InitiativePic_singleThread.py"])p=subprocess.Popen(["python", "InitiativePic_singleThread.py"], shell=True,stdout=True)while 1:    time.sleep(60)    ret=subprocess.Popen.poll(p)    if ret is None:        print("           *                              ")        print("                                          ")        print("       *       *                          ")        print("                                          ")        print("           *                              ")        print(datetime.now(),"the Initiative Programer is running!")    else:        print(datetime.now(),"it has stoped,so it will restart")        p=subprocess.Popen(["python", "InitiativePic_singleThread.py"], shell=True,stdout=True) 


执行  cxfreeze hello.py --target-dir dist --base-name=win32gui后,在cmd中运行生成的exe,提示tuple has no attribute "get"

我以为是import -pixSubprocess时候的警告造成的问题,于是我就用下面这段代码做了一个测试:

import subprocess , sys , timep=subprocess.Popen(['ping','127.0.0.1','-n','10'], shell=False,stdout=False)while 1:    time.sleep(1)    ret=subprocess.Popen.poll(p)    if ret is None:        print p.pid,"running"    else:        print "Termined!"        p=subprocess.Popen(['ping','127.0.0.1','-n','10'], shell=False,stdout=False)
   调用cx_Freeze生成exe时候,仍然在-pixSubprocess时警告。但是拖动到cmd中运行,一切正常。

 然后,我又cxfreeze StartPeriodical.py --target-dir dist --base-name=win32gui生成监控exe,拖动到cmd中运行,一切又正常了。但是不知道为什么。





 


原创粉丝点击