title: Python - ImportError: loader for [module] cannot handle __mp_main__
body: <p>I've searched around the web and couldn't find anyone experiencing the same problem. </p>
  
  <p>I am creating a file replicator that uses flask for the backend. The replicator takes in an uploaded file and masks some of the data within the file before exporting it. It does this for a user specified amount. In order to speed up the replication, I am using python's multiprocessing library to speed up the process and got it working in macOS. However, when I tried running it on a VM using Windows 2008 Server, I run into an ImportError.</p>
  
  <pre><code>Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\spawn.py", line 223, in prepare
    _fixup_main_from_name(data['init_main_from_name'])
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\spawn.py", line 249, in _fixup_main_from_name
  alter_sys=True)
  File "C:\Program Files (x86)\Python36-32\lib\runpy.py", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:\Program Files (x86)\Python36-32\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files (x86)\Python36-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\esglabuser\Desktop\asib-de-identifier\server\server.py", line 22, in &lt;module&gt;
    app = Flask(__name__)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\flask\app.py", line 357, in __init__
    instance_path = self.auto_find_instance_path()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\flask\app.py", line 662, in auto_find_instance_path
    prefix, package_path = find_package(self.import_name)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\flask\helpers.py", line 766, in find_package
    filename = loader.get_filename(root_mod_name)
  File "&lt;frozen importlib._bootstrap_external&gt;", line 398, in _check_name_wrapper
  
  ImportError: loader for server.server cannot handle __mp_main__
  </code></pre>
  
  <p>After trying to debug, the function calls the processes to start and then the error is raised.</p>
  
  <pre><code># function starts processes to replicate file 'amount' times 
  proc_queue = Queue()
  proc_lock = Lock()
  
  proc_queue.put((file_path, file_type, 0, amount))
  
  # ----- Create Worker Processes ----- #
  try:
      num_cores = cpu_count()
  except NotImplementedError:
      num_cores = 8
  
  workers = [Process(target=replicate, args=(proc_queue, proc_lock)) for _ in range(num_cores)]
  for p in workers:
      p.daemon = True
      p.start()    # &lt;--- When the ImportError is raised
  
  # ----- Join all workers ----- #
  for proc in workers:
      proc.join()
  </code></pre>
  
  <p>The replicate function works as follows:</p>
  
  <pre><code>def replicate(queue, lock):
      while True:
          # ----- CRITICAL SECTION START ----- #
          with lock:
              file_path, file_type, num_times, amount = queue.get()
  
              if num_times &gt;= amount:
                  queue.put((file_path, file_type, num_times, amount))
                  break
              else:
                  num_times += 1
  
              queue.put((file_path, file_type, num_times, amount))
          # ----- CRITICAL SECTION END ----- #
  
      # Do some data transformation with file
      # Export new file
  </code></pre>
  
  <p>I found topics on the difference between how Windows and macOS handle child processes and passing arguments, but I am passing all the arguments through to the child processes on instantiation. Is there something else that I am missing that differs between the two OSes or is my implementation causing problems. </p>
  
  <p>Anyone have any ideas/thoughts on what could be going wrong?</p>