Python Multiprocessing error: AttributeError: module '__main__' has no attribute '__spec__'

24,277

Solution 1

The problem is not with the code / Python 3.6, it is with Spyder.

After some investigation I found that the code runs fine when executed in an external system terminal but not when run in Spyder's IPython console.

I was able to dump the contents of spec and assign them to a variable that was included inside main to allow this code to function within the IPython console.

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
    with Pool(5) as p:
       print (p.map(f, [1, 2, 3]))

Solution 2

pdb users

The question didn't specifically mention Spyder nor Conda (though it is tagged as such). Hence, I will note that I found this can also happen when using pdb.

E.g.

python -m pdb myprogram.py

Passing __spec__ = None would be a useful workaround if you wanted to persist with pdb.

Solution 3

the same probelm in Spyder (Anaconda3, python 3.6) when I try the external terminal.

Error message: AttributeError: module '__main__' has no attribute '__spec__'

I changed the Run console to 'Excute in current console', and applied it. then if that doesnot work, try other conselor and then change back to 'Excute in current console'. Finally, it works. no '__spec__ = None' is needed.

Share:
24,277
Admin
Author by

Admin

Updated on July 22, 2022

Comments

  • Admin
    Admin over 1 year

    I'm using Python 3.6 and am trying to follow along with the very first example at the website below (full code also below) and am getting the below error: https://docs.python.org/3.6/library/multiprocessing.html

    Error message: AttributeError: module '__main__' has no attribute '__spec__'

    Full example code:

    from multiprocessing import Pool
    
    def f(x):
        return x*x
    
    if __name__ == '__main__':
        with Pool(5) as p:
            print(p.map(f, [1, 2, 3]))
    

    I tried Googling it and searching Stack Overflow but I've only found one other case of this error and it did not have an answer.