pathos ImportError: __import__ not found

2.9k Views Asked by At

I'm running the following code on Linux - Ubuntu with Python 3.5 in a virtual environment, and getting the error below. It works fine for threading and for the list comprehension part, but I'm having problems getting the multiprocessing to work.

multiprocess.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/multiprocess/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/lib/python3.5/dist-packages/multiprocess/pool.py", line 44, in mapstar
    return list(map(*args))
  File "/usr/local/lib/python3.5/dist-packages/pathos/helpers/mp_helper.py", line 15, in <lambda>
    func = lambda args: f(*args)
  File "<input>", line 2, in squared
ImportError: __import__ not found
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "/usr/local/lib/python3.5/dist-packages/pathos/multiprocessing.py", line 137, in map
    return _pool.map(star(f), zip(*args)) # chunksize
  File "/usr/local/lib/python3.5/dist-packages/multiprocess/pool.py", line 260, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/local/lib/python3.5/dist-packages/multiprocess/pool.py", line 608, in get
    raise self._value
ImportError: __import__ not found

Here is the code, it works with the thread pool, and for the list comprehension

import pathos
import numpy as np
import time

def squared(x):
    import time
    time.sleep(.5)
    return x ** 2


x = np.arange(400).reshape(50, 8)
p = pathos.pools.ProcessPool()
t = pathos.pools.ThreadPool()

st = time.time()
ans = [squared(i) for i in x]
et = time.time()
print(et-st)

st = time.time()
ans = p.map(squared, x)
et = time.time()
print(et-st)

st = time.time()
ans = t.uimap(squared, x)
list(ans)
et = time.time()
print(et-st)
1

There are 1 best solutions below

0
On

Turns out that I needed to save my file test.py for example, and slap a if name == 'main': right after the function definition, and then run python test.py from the command line, and now it works perfectly.

import pathos
import numpy as np
import time

def squared(x):
    # import time
    time.sleep(.5)
    return x ** 2


if __name__ == '__main__':
    x = np.arange(400).reshape(50, 8)
    p = pathos.pools.ProcessPool()
    t = pathos.pools.ThreadPool()

    st = time.time()
    ans = [squared(i) for i in x]
    et = time.time()
    print(et-st)

    st = time.time()
    ans = p.map(squared, x)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = p.imap(squared, x)
    list(ans)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = p.uimap(squared, x)
    list(ans)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = t.map(squared, x)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = t.imap(squared, x)
    list(ans)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = t.uimap(squared, x)
    list(ans)
    et = time.time()
    print(et-st)