我有这个简单的程序:
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.
没有多重处理。我看到了其他一些答案,但他们没有帮助。
有人可以在这里建议一个可能的问题吗?
答案 0 :(得分:2)
回溯表示后台使用multiprocessing
,而不是在您自己的代码中明确使用。具体来说,它由pyscreenshot\procutil.py
调用。追溯的相关行:
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 page表示pyscreenshot
是“ImageGrab模块的替换,仅适用于Windows”。因此,您应该安装ImageGrab
库,这似乎完全相同,但只与Windows和MacOS兼容(请参阅here)
答案 1 :(得分:2)
Windows上的多处理模块存在已知问题(详细说明roganosh注释):使用multiprocessing
模块必须在函数或__main__
部分中完成(在所有导入初始化之后) ),因为Windows生成python可执行文件的方式(因此“bootstrap阶段”错误),因此不在脚本的根目录中。在Linux上没有问题。看起来与RuntimeError on windows trying python multiprocessing非常相似。
尝试更改此代码:
from PIL import Image
import pyscreenshot as ImageGrab
if __name__ == "__main__":
im=ImageGrab.grab()
im.show()