3

Say we have a script that works with no problem:

import module1

class SomeClass(object):
   def __init__(self):
      self.m = module1()

s = SomeCLass()

However if I use multiprocessing module with "spawn" context:

import module1
import multiprocessing as mp

class SomeClass(object):
   def __init__(self):
      self.m = module1()

s = SomeCLass()
ctx = mp.get_context('spawn')
proc = ctx.Process(target=SomeClass, kwargs={})
proc.daemon = True
proc.start()

I get the following error at proc.start():

   ModuleNotFoundError: No module named 'module1'

Yet, if I add the following lines before "import modlule1":

import sys
sys.path.insert(0, path_to_module1)

import module1
import multiprocessing as mp

class SomeClass(object):
   def __init__(self):
      self.m = module1()

s = SomeCLass()
ctx = mp.get_context('spawn')
proc = ctx.Process(target=SomeClass, kwargs={})
proc.daemon = True
proc.start()

it works with no problem. But how can I avoid the sys.path.insert or why does this happen? Is there a more elegant solution?

Alejandro
  • 879
  • 11
  • 27

0 Answers0