title: Module not found error with multiprocessing when called from another module
body: <p>I'm starting to include multiprocessing into my code, as the task I'm trying to automate is rather costly computationally speaking. The module structure in the code I have is the following, based on the stackoverflow information I've been gathering. I'm working on python 3.7 in Win10.</p>
  <p><strong>main</strong>: where the multiprocessing function is called, besides loading inputs and such.</p>
  <pre><code>import pandas as pd
  import run
  
  def do():
      df=pd.DataFrame({'Identifier': ['id_1', 'id_1', 'id_1', 'id_1', 'id_1', 'id_2', 'id_2', 'id_2', 'id_2', 'id_2', 'id_3', 'id_3', 'id_3', 'id_3', 'id_3'],
                   'float_id': [1, 2, 3, 4, 5, 10, 25, 33, 45, 50, .1, .2, .3, .4, .5],
                   'a': np.random.rand(15),
                   'b': np.random.rand(15),
                   'c': np.random.rand(15)})
      
      v_column=['a', 'b', 'c']
      
      df_out=run.function_multiprocessing(df, v_column)
  
      return df_out
  
  if __name__=='__main__':
      df_out=do()
  </code></pre>
  <ul>
  <li><strong>run</strong>: where the multiprocessing function is located</li>
  </ul>
  <pre><code>import defs
  import pandas as pd
  
  import multiprocessing
  
  
  def iterator(data, id_col, value_col):
      for col in value_col:
          yield (data[col].values, data[id_col].values)
  
  def function_multiprocessing(data, v_column):
      list_df=[]
      with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
          for identifier, df_f in data.groupby(['Identifier']): 
              print(identifier)
              data_f=pool.starmap(defs.function_to_apply, iterator(df_f, 'float_id', v_column))
              
              out=pd.DataFrame(data_f, index=[identifier])
              list_df.append(out)
      
      df_out=pd.concat(list_df)
      
      return df_out
  </code></pre>
  <ul>
  <li><strong>defs</strong>: where the function to &quot;multiprocess&quot; (<em>function_to_apply</em>) is located</li>
  </ul>
  <p>None of these modules belong to my PYHTONPATH, and are located in the same folder. What I'm trying to do is apply a function over each &quot;Identifier&quot;, based on the values found in &quot;float_id&quot; and the values of columns &quot;a&quot;, &quot;b&quot; and &quot;c&quot; respectively. We may think of a weighted average of the column values based on the &quot;float_id&quot; values for the sake of simplicity.</p>
  <p>When I execute the code, I get the following error -no matter what I try-, over and over again for each worker.</p>
  <pre><code>Process SpawnPoolWorker-1:
  Traceback (most recent call last):
    File &quot;C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py&quot;, line 297, in _bootstrap
      self.run()
    File &quot;C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py&quot;, line 99, in run
      self._target(*self._args, **self._kwargs)
    File &quot;C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py&quot;, line 110, in worker
      task = get()
    File &quot;C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\multiprocessing\queues.py&quot;, line 354, in get
      return _ForkingPickler.loads(res)
  ModuleNotFoundError: No module named 'defs'
  </code></pre>
  <p>I've tried including the line <code>multiprocessing.set_start_method(&quot;fork&quot;)</code> after importing the multiprocessing module, which raised an error. The same for <code>&quot;spawn&quot;</code>. I also tried including the module &quot;defs&quot; as a parameter in the <code>function_multiprocessing</code> method, and calling it from <code>main</code>, with no success. The same error occurs when providing &quot;function_to_apply&quot; as a parameter.</p>
  <p>¿What am I doing wrong?¿How could I make this work?</p>
  <p>Thanks a lot beforehand!</p>
  <p><strong>UPDATE</strong>: When importing the <code>defs.py</code> module within the <code>function_multiprocessing</code> method as such</p>
  <pre><code>def function_multiprocessing(data, v_column):
      import defs
      
      list_df=[]
      ...
  </code></pre>
  <p>it does not raise any errors. However, when providing the module as a variable it did not work.</p>