使用pyInstaller打包的一点经验
2014-07-12 17:11
204 查看
1 Popen
class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)在脚本状态下stdout=None, stderr=None时运行不会有任何问题,但经过pyInstall打包成exe后,运行时会出现
”[Error 6] The handle is invalid” 异常,解决方法如下:
脚本原始调用方法:
Import subprocess
p = subprocess.Popen("myapp.exe", stdin=subprocess.PIPE, stdout=None, stderr=None)
p.stdin.write('xxx')
适配pyInstaller的写法:
Import subprocess
p = subprocess.Popen("x.exe", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.write('xxx')
p.stdout.close()
p.stderr.close()
2 Multiprocessing
代码中直接使用标准库中的multiprocessing.Process和multiprocessing.Pool,在打包成可执行程序后会创建进程失败。对于pyinstaller的one-directory模式,只要再程序的最开始调用multiprocessing. freeze_support()就可以了,但对于one-file模式,还需要使用下面修改后的Process和Pool。
import multiprocessing.forking
import multiprocessing.pool
import os
import sys
class _Popen(multiprocessing.forking.Popen):
def __init__(self, *args, **kw):
if hasattr(sys, 'frozen'):
# We have to set original _MEIPASS2 value from sys._MEIPASS
# to get --onefile mode working.
# Last character is stripped in C-loader. We have to add
# '/' or '\\' at the end.
os.putenv('_MEIPASS2', sys._MEIPASS + os.sep)
try:
super(_Popen, self).__init__(*args, **kw)
finally:
if hasattr(sys, 'frozen'):
# On some platforms (e.g. AIX) 'os.unsetenv()' is not
# available. In those cases we cannot delete the variable
# but only set it to the empty string. The bootloader
# can handle this case.
if hasattr(os, 'unsetenv'):
os.unsetenv('_MEIPASS2')
else:
os.putenv('_MEIPASS2', '')
class
Process(multiprocessing.Process):
_Popen = _Popen
class
Pool(multiprocessing.pool):
Process = Process
3
模块引用
脚本运行状态下,当需要使用全路径引用py文件时(如当前模块路径的父目录中的.py文件),可以通过添加路径的方法减少引用层数。假设plugins处于代码的根目录下,如果想引用plugins\Demo1\Demo1MainFrame.py,可以用下述方法:Import
plugin.Demo1. Demo1MainFrame
也可以先在程序最开始执行:
sys.path.append("./Plugins")
然后再使用:
Import
Demo1. Demo1MainFrame
但遗憾的是,这种方法在pyInstaller打包后的程序中不能使用,会出现模块导入错误。
4
其它
打包过程中若遇到临时文件访问权限的问题,可临时禁用杀毒和防火墙功能后再尝试;pyInstall仅支持ascii编码方式,打包后可执行文件数径中不能有中文。
相关文章推荐
- Delphi使用server组件控制excel的一点经验
- Delphi使用server组件控制excel的一点经验
- 对 WebBrowser 和 AxWebBrowser 两个控件使用经验的一点补充
- 使用RamDisk虚拟硬盘一点经验
- QTP中Description对象使用的一点经验
- ActiveReports使用的一点小经验
- 学习和分享一点简单的System.Web.HttpRuntime.Cache的使用经验
- 长期使用中型Access数据库的一点经验与缺点
- 使用Microsoft Enterprise Library 3.1 (May,2007) Exception Handling Block的一点经验
- VC6使用#pragma warning的一点经验
- ASP.net中使用ListBox一点经验
- [原创]谈谈使用远程桌面连接的一点经验
- css使用的一点经验
- VC6使用#pragma warning的一点经验
- 使用hibernate的Cascade的一点经验
- Delphi使用server组件控制excel的一点经验
- jsp开发网站使用cookie的一点经验
- 关于使用SQLDMO.Backup和SQLDMO.Restore的一点经验
- 一点Hibernate + spring 使用经验
- ASP.NET2.0中使用AJAX1.0控件的一点经验