【问题标题】:RuntimeError on Windows trying to run a simple programWindows 上的 RuntimeError 试图运行一个简单的程序
【发布时间】:2017-09-25 08:04:14
【问题描述】:

我有这个简单的程序:

from PIL import Image
import pyscreenshot as ImageGrab

print "hi"
im=ImageGrab.grab()
im.show()

这在 Ubuntu 上运行良好,但在 Windows 上会出现以下错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\multiprocessing\forking.py", line 380, in main
    prepare(preparation_data)
  File "C:\Python27\lib\multiprocessing\forking.py", line 509, in prepare
    '__parents_main__', file, path_name, etc
  File "C:\Users\Administrator\Downloads\sample.py", line 5, in <module>
    im=ImageGrab.grab()
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 46, in gra
b
    return _grab(to_file=False, childprocess=childprocess, backend=backend, bbox
=bbox)
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 29, in _gr
ab
    return run_in_childprocess(_grab_simple, imcodec.codec, to_file, backend, bb
ox, filename)
  File "C:\Python27\lib\site-packages\pyscreenshot\procutil.py", line 28, in run
_in_childprocess
    p.start()
  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "C:\Python27\lib\multiprocessing\forking.py", line 258, in __init__
    cmd = get_command_line() + [rhandle]
  File "C:\Python27\lib\multiprocessing\forking.py", line 358, in get_command_li
ne
    is not going to be frozen to produce a Windows executable.''')
RuntimeError:
            Attempt to start a new process before the current process
            has finished its bootstrapping phase.

            This probably means that you are on Windows and you have
            forgotten to use the proper idiom in the main module:

                if __name__ == '__main__':
                    freeze_support()
                    ...

            The "freeze_support()" line can be omitted if the program
            is not going to be frozen to produce a Windows executable.

没有多重处理。我看到了其他一些答案,但它们没有帮助。

有人可以在这里提出一个可能的问题吗?

【问题讨论】:

  • This probably means that you are on Windows and you have forgotten to use the proper idiom in the main module ... 你没看吗?

标签: python windows python-2.7


【解决方案1】:

Windows 上的多处理模块存在一个已知问题(详细说明 roganosh 备注):使用 multiprocessing 模块必须在函数或 __main__ 部分中完成(在所有导入初始化之后),而不是在由于 Windows 生成 python 可执行文件的方式(因此出现“引导阶段”错误),脚本的根目录。在 Linux 上没有问题。看起来和RuntimeError on windows trying python multiprocessing很像。

尝试改成这样的代码:

from PIL import Image
import pyscreenshot as ImageGrab

if __name__ == "__main__":
    im=ImageGrab.grab()
    im.show()

【讨论】:

  • 嗯,我认为if __name__ == '__main__' 守卫必须在实现multiprocessing 的模块中,但您建议使用其他库最终调用multiprocessing 的任何内容也可以修复以这种方式?
  • 有趣。无论如何,我在答案中留下了指向该库的 Windows 版本的链接,因此修复起来很简单:)
  • 让我们看看 OP 的结果如何。然后会下雨投票:)
  • 请将“forks”替换为“spawns”。此外,与其说它需要“在一个过程中完成”,不如说它最终必须通过检查__main__ 模块来控制使用。在子进程中,它以不同的名称执行。这可以保护脚本不被创建进程等的进程递归地炸毁。现在多处理更好地检测到这一点并立即失败,但我记得忘记检查 __main__ 是一个痛苦的错误。
【解决方案2】:

回溯表明在后台使用了multiprocessing,而不是在您自己的代码中明确使用。具体来说,它被pyscreenshot\procutil.py 调用。 traceback 的相关行:

File "C:\Python27\lib\site-packages\pyscreenshot\procutil.py", line 28, in run
_in_childprocess
    p.start()
  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)

由于问题出在库中,除了自己修改库之外,您无能为力。但是,this pagepyscreenshot 是“ImageGrab 模块的替代品,仅适用于 Windows”。因此,您应该安装 ImageGrab 库,它似乎做同样的事情,但只与 Windows 和 MacOS 兼容(参见 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多