The Python Oracle

Python, Windows, and Multiprocessing

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands

--

Chapters
00:00 Python, Windows, And Multiprocessing
01:51 Accepted Answer Score 2
03:01 Thank you

--

Full question
https://stackoverflow.com/questions/2604...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #windows #multiprocessing #virtualenv #pythonimport

#avk47



ACCEPTED ANSWER

Score 2


You can work around this by using an absolute import for OtherCustomClass:

from base_module import OtherCustomClass

I'm not exactly sure why, but it seems that when multiprocessing spawns a new process and imports your __main__, it's not able to handle the implicit relative import you're using with OtherCustomClass. If you explicitly import it from base_module, it works fine. My guess is that the spawned child process is not recognized as being part of the base_module package, so the implicit import fails, but that's just a guess.

Note that you shouldn't be using implicit relative imports anyway (they're altogether removed from Python 3), so switching to an absolute import isn't a bad thing.

Also of note, that doing an explicit relative import works on Python 3.4:

from . import OtherCustomClass

But it fails on Python 2.7:

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 495, in prepare
    '__parents_main__', file, path_name, etc
  File "C:\Users\oreild1\Desktop\base_module\Launcher.py", line 5, in <module>
    from . import OtherCustomClass
ValueError: Attempted relative import in non-package
error:
Traceback (most recent call last):
  File "C:\Users\oreild1\Desktop\base_module\Launcher.py", line 18, in main
    basmod = BaseModule(argv[0], argv[1])
  File "C:\Users\oreild1\Desktop\base_module\Launcher.py", line 10, in __init__
    self.manager.start()
  File "C:\python27\lib\multiprocessing\managers.py", line 528, in start
    self._address = reader.recv()
EOFError