1

I have this simple program:

from PIL import Image
import pyscreenshot as ImageGrab

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

This works perfectly fine on Ubuntu, but it gives the following error on 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.

There is no multiprocessing. I saw some other answers, but they did not help.

Can some please suggest a possible problem here?

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Henil Shah
  • 137
  • 4
  • 14
  • `This probably means that you are on Windows and you have forgotten to use the proper idiom in the main module` ... Did you not read that? – OneCricketeer Apr 27 '17 at 13:38

2 Answers2

3

There's a known problem with the multiprocessing module on Windows (to elaborate on roganosh remark): using multiprocessing module must be done in a function or in the __main__ section (after all the imports are initialized), not in the root of the script because of the way Windows spawns the python executable (hence the "bootstrap phase" error). No issue on Linux. Looks very much like the same issue as RuntimeError on windows trying python multiprocessing.

Try changing into this code:

from PIL import Image
import pyscreenshot as ImageGrab

if __name__ == "__main__":
    im=ImageGrab.grab()
    im.show()
Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Huh, I thought the `if __name__ == '__main__'` guard had to be within the module that implemented `multiprocessing`, but you're suggesting that anything that uses other libraries that eventually call `multiprocessing` can also be fixed in this way? – roganjosh Apr 27 '17 at 13:42
  • Interesting. In any case, I left a link to the Windows version of the library in my answer, so it's simple enough to fix :) – roganjosh Apr 27 '17 at 13:46
  • let's see how it turns out for the OP. Then it will rain upvotes :) – Jean-François Fabre Apr 27 '17 at 14:02
  • 1
    Please replace "forks" with "spawns". Also, instead of saying it needs to be "done in a procedure", it should say that usage ultimately has to be gated by checking for the `__main__` module. In child processes it gets executed with a different name. This protects the script from recursively blowing up with processes creating processes and so on. Nowadays multiprocessing is better about detecting this and failing immediately, but I remember when forgetting to check for `__main__` was a painful mistake. – Eryk Sun Apr 27 '17 at 21:12
2

The traceback indicates that there is multiprocessing being used in the background, not explicitly in your own code. Specifically, it is being called by pyscreenshot\procutil.py. The relevant lines of the 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)

Since the issue is in the library, there would be nothing you could do except modify the library yourself. However, this page says that pyscreenshot is a "Replacement for the ImageGrab Module, which works on Windows only". So instead, you should install ImageGrab library, which seems to do exactly the same thing, but is only compatible with Windows and MacOS (see here)

roganjosh
  • 12,594
  • 4
  • 29
  • 46