PyDev/Python3.1 子进程错误 - 未声明编码



当我运行调用子进程子进程的 Parent.py 时.exe出现以下错误

  File "child.exe", line 1
SyntaxError: Non-UTF-8 code starting with 'x90' in file child.exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

如果我使用 child.py 运行 Parent.py,则执行会成功从 Eclipse 返回"来自父级的你好"。

如果我从发货的 IDLE 运行 Parent.py,则在子.exe和 child.py 的情况下都不会返回任何内容

我已经阅读了 http://python.org/dev/peps/pep-0263/文档并理解了它,可能误解了它意味着我应该添加我尝试添加到孩子的建议评论.exe......他们没有工作。

Parent.py

import os
import subprocess
import sys
if sys.platform == "win32":
    import msvcrt
    import _subprocess
else:
    import fcntl
# Create pipe for communication
pipeout, pipein = os.pipe()
# Prepare to pass to child process
if sys.platform == "win32":
    curproc = _subprocess.GetCurrentProcess()
    pipeouth = msvcrt.get_osfhandle(pipeout)
    pipeoutih = _subprocess.DuplicateHandle(curproc, pipeouth, curproc, 0, 1,
            _subprocess.DUPLICATE_SAME_ACCESS)
    pipearg = str(int(pipeoutih))  
else:
    pipearg = str(pipeout)
    # Must close pipe input if child will block waiting for end
    # Can also be closed in a preexec_fn passed to subprocess.Popen
    fcntl.fcntl(pipein, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
# Start child with argument indicating which FD/FH to read from
subproc = subprocess.Popen(['python', 'child.exe', pipearg], close_fds=False)
# Close read end of pipe in parent
os.close(pipeout)
if sys.platform == "win32":
    pipeoutih.Close()
# Write to child (could be done with os.write, without os.fdopen)
pipefh = os.fdopen(pipein, 'w')
pipefh.write("Hello from parent.")
pipefh.close()
# Wait for the child to finish
subproc.wait()



儿童.exe(用cx_freeze冷冻(

import os, sys

if sys.platform == "win32":
    import msvcrt
# Get file descriptor from argument
pipearg = int(sys.argv[1])
if sys.platform == "win32":
    pipeoutfd = msvcrt.open_osfhandle(pipearg, 0)
else:
    pipeoutfd = pipearg
# Read from pipe
# Note:  Could be done with os.read/os.close directly, instead of os.fdopen
pipeout = os.fdopen(pipeoutfd, 'r')
print(pipeout.read())
pipeout.close()
subprocess.Popen(['python', 'child.exe', pipearg], ...

问题是你试图让python读取一个二进制文件,如果child.exe是一个普通的Windows可执行文件。引发此错误的原因是二进制文件的字节超出了标准 ascii - utf8 标准,因此解释器无法读取它。

也许你想要的只是执行孩子.exe只是从中删除python行:

subprocess.Popen(['child.exe', pipearg], ...

最新更新