How to troubleshoot an AttributeError: __exit__ in multiproccesing in Python?

How to troubleshoot an AttributeError: __exit__ in multiproccesing in Python?

The problem is in this line:

with pattern.findall(row) as f:

You are using the with statement. It requires an object with __enter__ and __exit__ methods. But pattern.findall returns a list, with tries to store the __exit__ method, but it cant find it, and raises an error. Just use

f = pattern.findall(row)

instead.

It is not the askers problem in this instance but the first troubleshooting step for a generic AttributeError: __exit__ should be making sure the brackets are there, e.g.

with SomeContextManager() as foo:
    #works because a new object is referenced...

not

with SomeContextManager as foo:
    #AttributeError because the class is referenced

Catches me out from time to time and I end up here -__-

How to troubleshoot an AttributeError: __exit__ in multiproccesing in Python?

The error also happens when trying to use the

with multiprocessing.Pool() as pool:
   # ...

with a Python version that is too old (like Python 2.X) and does not support using with together with multiprocessing pools.

(See this answer https://stackoverflow.com/a/25968716/1426569 to another question for more details)

Leave a Reply

Your email address will not be published.