39

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.

8
  • The posted code works fine on my machine
    – bendl
    Aug 16, 2017 at 18:01
  • I'm using Anaconda / Spyder with Python 3.6... maybe that has something to do with it? Aug 16, 2017 at 18:04
  • 1
    I am using Spyder 3.1.2 with Python 3.6.0 (Anaconda 4.3.1) in Windows 7
    – bendl
    Aug 16, 2017 at 18:07
  • 3
    I'm on Spyder 3.1.4. I found the code works when I run the script from Command Prompt but gives errors in Spyder. Any idea if there is any configuration steps to make Spyder play nicely with multiprocessing? Aug 16, 2017 at 18:13
  • 2
    The Python interpreter is set to "Default (ie the same as Spyder's)". I changed Anaconda's settings to run in an external system terminal and it runs fine. Something about the IPython console is throwing errors. Aug 16, 2017 at 18:27

4 Answers 4

38

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]))
3
  • 11
    I'm actually surprised that worked, since __spec__ is not normally a string. Since it did work, you could probably just use None.
    – Kevin
    Aug 16, 2017 at 22:32
  • 15
    I tried __spec__ = __spec__ and my child processes began to start and stop infinitely. I also checked __spec__ variable when the script is started in system terminal. It is None. So I think __spec__ = None is the "right" way to fix this.
    – Winand
    Jan 13, 2018 at 21:04
  • 1
    Bless you Winand. I'm really glad I tried to execute the pythondocs example before my own code. Got this signature, successfully ran with "spec = None" This issue would have caused me infinite sulk. Mar 1, 2019 at 21:52
31

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.

1

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.

1
  • If anyone still has this pop up randomly (as I did), first try just closing your console, and running again in a new one. Sometimes the settings somehow get messed up.
    – Thomas909
    Oct 31, 2022 at 22:12
0

Same problem with Spyder (Anaconda3, python 3.7).

I used

from genetic_selection import GeneticSelectionCV

def main(): .... and as I was running the code, an error like this occured:

main_mod_name = getattr(main_module.__spec__, "name", None)

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

what I did is deleted "__spec__" in main_mod_name = getattr(main_module.__spec__, "name", None)

so I only have this: main_mod_name = getattr(main_module, "name", None)

the code then worked perfectly fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.