Python 使用py2exe运行无限循环的subprocess.Popen
我正在尝试使用py2exe将python脚本编译成可执行文件。 我已经按照文档中的描述设置了setup.py文件:Python 使用py2exe运行无限循环的subprocess.Popen,python,subprocess,popen,py2exe,Python,Subprocess,Popen,Py2exe,我正在尝试使用py2exe将python脚本编译成可执行文件。 我已经按照文档中的描述设置了setup.py文件: from distutils.core import setup import py2exe setup(console=['agent.py', 'test.py']) agent.py文件仅使用subprocess.Popen打开另一个脚本: import sys import subprocess print 'is this working?' child = su
from distutils.core import setup
import py2exe
setup(console=['agent.py', 'test.py'])
agent.py文件仅使用subprocess.Popen打开另一个脚本:
import sys
import subprocess
print 'is this working?'
child = subprocess.Popen([sys.executable, 'test.py'])
test.py文件是
while 0 == 0:
print 'test'
当作为python脚本运行时,它运行良好。当作为py2exe编译的可执行文件运行时,它不会运行
当我尝试将agent.py中的文件引用从“test.py”更改为“test.exe”时,运行编译的agent.exe只需在无限循环中打印“这是否正常工作?”。我做错了什么?
sys.executable
在作为编译的可执行文件运行时指向agent.exe
,而不是python.exe
。您需要将Popen
更改为:
child = subprocess.Popen(['test.exe'])
运行编译后的可执行文件时。您可以使用hasattr(sys,“freezed”)来确定您是否处于冻结(py2exe)模式或不处于冻结(Python脚本)模式。这不太管用,但我所要做的就是用完整路径名替换您的答案。谢谢这起到了作用:
app_path = os.path.realpath(os.path.join(
os.path.dirname(sys.executable), 'test.exe'))
child = subprocess.Popen(app_path)
这不太管用,但我所要做的就是用完整的路径名替换您的答案。谢谢这是有效的:app_path=os.path.realpath(os.path.join(os.path.dirname(sys.executable),'test.exe'))child=subprocess.Popen(app_path)