subprocess.Popen running infinite loop with py2exe
I'm trying to use py2exe to compile a python script into an executable. I've set up the setup.py file just like it's described in documentation:
from distutils.core import setup
import py2exe
setup(console=['agent.py', 'test.py'])
The agent.py file simply uses subprocess.Popen to open开发者_运维知识库 another script:
import sys
import subprocess
print 'is this working?'
child = subprocess.Popen([sys.executable, 'test.py'])
The test.py file is
while 0 == 0:
print 'test'
When running this as a python script, it works fine. When running as a py2exe-compiled executable, it does not run.
When I try to change the file reference in agent.py from 'test.py' to 'test.exe', running the compiled agent.exe simply prints 'is this working?' on an infinite loop. What have I done wrong?
sys.executable
points to agent.exe
instead of python.exe
when run as compiled executable. You need to change your Popen
to:
child = subprocess.Popen(['test.exe'])
when running compiled executable. You can use hasattr(sys, "frozen")
to determine whether you're in frozen (py2exe) or not (Python script) mode.
That didn't quite work, but all I had to do was replace your answer with the full path name. Thanks! This worked:
app_path = os.path.realpath(os.path.join(
os.path.dirname(sys.executable), 'test.exe'))
child = subprocess.Popen(app_path)
精彩评论