41

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.

pppery
  • 3,731
  • 22
  • 33
  • 46
user8474060
  • 809
  • 1
  • 7
  • 7
  • The posted code works fine on my machine – bendl Aug 16 '17 at 18:01
  • I'm using Anaconda / Spyder with Python 3.6... maybe that has something to do with it? – user8474060 Aug 16 '17 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 '17 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? – user8474060 Aug 16 '17 at 18:13
  • Are you certain that you are using the correct python executable in Spyder? – bendl Aug 16 '17 at 18:14
  • 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. – user8474060 Aug 16 '17 at 18:27
  • I got this error message today (`module '__main__' has no attribute '__spec__'`) when attempting to do this [pytorch tutorial](https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html) using Spyder. Specifically the line `dataiter = iter(trainloader)` in the tutorial triggered the error. The `__spec__ = None` solution below worked for me, but it seems odd that one must resort to this. Have any of the Spyder devs commented on this issue? @CarlosCordoba – littleO Jan 02 '19 at 16:06
  • One thing, since it is a problem caused by the Ipython concole, wouldn't it be correct to add the tag "ipython"? I say that based on the answer from user8474060. It might help some people having the same problem. – Jose M Gonzalez Feb 25 '20 at 12:34

5 Answers5

39

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]))
user8474060
  • 809
  • 1
  • 7
  • 7
  • 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 '17 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 '18 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. – Colin Helms Mar 01 '19 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.

JoeyC
  • 764
  • 11
  • 19
  • 1
    I feel like i"m no dummy, but I actually don't know what you mean here. How do you pass `__spec__ = None`? – cydonian Apr 06 '23 at 02:57
  • 1
    @cydonian, fwiw, I don't think you're a dummy either. My mind doesn't go back 3 years, but I think what I was trying to say was put this line in the `__main__` declaration. I think [Winand says it better in the comment above](https://stackoverflow.com/questions/45720153/python-multiprocessing-error-attributeerror-module-main-has-no-attribute/60922965?noredirect=1#comment83471090_45720872) – JoeyC Apr 07 '23 at 00:14
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.

Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74
Simin Zuo
  • 21
  • 3
  • 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 '22 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.

kiwi_kimchi
  • 345
  • 3
  • 12
0

I stumbled on this question in researching this error. I found that running

with Pool() as mp_pool:

caused the error. Changing this to:

if __name__ == '__main__':
    __spec__ = None
    with Pool() as mp_pool:

resolved it. Python 3.11.4. Fix had nothing to do with Spyder or Anaconda.

DrStrangepork
  • 2,955
  • 2
  • 27
  • 33