-1

While running the multiprocessing.py (contents of the file below)

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 get ImportError: cannot import name 'Pool'

I have also tried the 2 solutions given in this - python - ImportError: cannot import name Pool but unfortunately the problem exist.

pppery
  • 3,434
  • 13
  • 24
  • 37
Vivek Sharma
  • 17
  • 1
  • 3
  • Adding info about your system, and way you installed python could be really helpful. – Alex Baranowski Oct 23 '17 at 09:43
  • What happens if you run just `from multiprocessing import Pool` in a newly started Python interpreter? – jmd_dk Oct 23 '17 at 09:47
  • 4
    Do not name your modules the same as other modules you want to use. You are importing your own module instead of the one in the standard library. – Stop harming Monica Oct 23 '17 at 09:52
  • Possible duplicate of [Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – pppery Aug 20 '19 at 15:25

4 Answers4

10

Rename filename. Avoid using `multiprocessing.py' whcih conflicts with the system module.

sabu
  • 101
  • 1
  • 5
1

To get rid of import error name the file something else instead of multiprocessing.py, as filename should not be same as module name.

You will be able to import the Pool function after a change of filename and your code should work on python3. But if you are using python2 the code will not work.

In Python 2.x and 3.0, 3.1 and 3.2, multiprocessing.Pool() objects are not context managers. You cannot use them in a with the statement. Only in Python 3.3 and up can you use them as such.

From the Python 3 multiprocessing.Pool() documentation:

New in version 3.3: Pool objects now support the context management protocol – see Context Manager Types. enter() returns the pool object, and exit() calls terminate().

Below code will work on python2

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))

Output

[1, 4, 9]
Amarpreet Singh
  • 2,092
  • 13
  • 26
1

Avoid using filename as "multiprocessing.py" as it conflicts with that of the package used.

Install: pip install multiprocessing

Import: from multiprocessing import Pool

Sample Code:

from multiprocessing import Pool

def  f(x):
    return x**3

if __name__ == '__main__':
    pool = Pool(processes=4)              
    result = pool.apply_async(f, [10])    
    print(result.get(timeout=1))           
    print(pool.map(f, range(10)))  
  • multiprocessing is part of the standard library in python 3 and python 2 is not supported anymore, there is no need to install it with pip – Gabriel Sep 02 '20 at 14:28
0

When you try and import a library python searches for the module name in a specific series of steps. So when you name the file the same as the module name, it loads your file, not the library. Since your file doesn't have a Pool function, it cannot import it. If multiprocessing was a builtin module, this error would nt come, but it is not recommended to name files the same as the python libraries or modules.

See the accepted answer here to better understand this process. How does python find a module file if the import statement only contains the filename?

kmcodes
  • 695
  • 1
  • 7
  • 19
  • To add further, using the blow at the top should also solve your problem if naming the same as inbuilt modules from __future__ import absolute_import – kmcodes Oct 23 '17 at 10:08