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()
代码中直接使用标准库中的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
脚本运行状态下,当需要使用全路径引用py文件时(如当前模块路径的父目录中的.py文件),可以通过添加路径的方法减少引用层数。假设plugins处于代码的根目录下,如果想引用plugins\Demo1\Demo1MainFrame.py,可以用下述方法:
Import plugin.Demo1. Demo1MainFrame
也可以先在程序最开始执行:
sys.path.append("./Plugins")
然后再使用:
Import Demo1. Demo1MainFrame
但遗憾的是,这种方法在pyInstaller打包后的程序中不能使用,会出现模块导入错误。