python-pptx: AttributeError: module 'collections' has no attribute 'abc'

I just created a simple test.py file

from pptx import Presentation prs = Presentation()

but when I run it I get

`Traceback (most recent call last): File “C:\Python\lib\site-packages\pptx\compat_init_.py”, line 10, in <module> Container = collections.abc.Container AttributeError: module ‘collections’ has no attribute ‘abc’

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “D:\Docs\mycompany\Code\Python\Excel\test.py”, line 1, in <module> from pptx import Presentation File “C:\Python\lib\site-packages\pptx_init_.py”, line 14, in <module> from pptx.api import Presentation # noqa File “C:\Python\lib\site-packages\pptx\api.py”, line 15, in <module> from .package import Package File “C:\Python\lib\site-packages\pptx\package.py”, line 6, in <module> from pptx.opc.package import OpcPackage File “C:\Python\lib\site-packages\pptx\opc\package.py”, line 11, in <module> from pptx.compat import is_string, Mapping File “C:\Python\lib\site-packages\pptx\compat_init_.py”, line 14, in <module> Container = collections.Container AttributeError: module ‘collections’ has no attribute ‘Container’

Process finished with exit code 1`

Any idea why?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

@dejudicibus yes, I think that’s the right workaround until we fix this, to import collections.abc before pptx

import collections.abc

from pptx import Presentation

After import collections.abc, the collections module will have a .abc attribute and the Python2/3 machinery in python-pptx should work properly.

It looks like Python 3.10 is happy if you do both imports

  • import collections
  • import collections.abc
A:\>python
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> collections.__file__
'C:\\Python\\Python310\\lib\\collections\\__init__.py'
>>> OrderedDict = collections.OrderedDict
>>> import collections.abc
>>> collections.abc.__file__
'C:\\Python\\Python310\\lib\\collections\\abc.py'
>>> Container = collections.abc.Container
>>>