Search code examples
pythonpython-3.xpython-docx

ImportError: cannot import name 'Document'


I ran a code generated in python 3.6.5, it perfectly worked (countless times), now I turned on my computer, runned it again (didn't change anything at all) and:

ImportError: cannot import name 'Document'

I tried to uninstall docx or python-docx via pip command according to previous posts, and:

Cannot uninstall requirement docx, not installed

then I tried to install, and:

Could not fetch URL https://pypi.python.org/simple/docx/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:719) - skipping
Could not find a version that satisfies the requirement docx (from versions: )
No matching distribution found for docx

What is going on here?

from tkinter import *
from functools import partial
from docx import Document
from docx.shared import Inches

the third line gives the problem (apparently) and I'm working on a mac OS High Sierra 10.13.6

Full Error Code:

Traceback (most recent call last):
  File "/Users/cezi/Desktop/Products_Field+Entries.py", line 5, in <module>
    from docx import Document
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/__init__.py", line 3, in <module>
    from docx.api import Document  # noqa
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/api.py", line 14, in <module>
    from docx.package import Package
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/package.py", line 9, in <module>
    from docx.opc.package import OpcPackage
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/opc/package.py", line 9, in <module>
    from docx.opc.part import PartFactory
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/opc/part.py", line 13, in <module>
    from ..oxml import parse_xml
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/oxml/__init__.py", line 89, in <module>
    from .section import (  # noqa
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/oxml/section.py", line 7, in <module>
    from copy import deepcopy
  File "/Users/cezi/Desktop/copy.py", line 1, in <module>
    from docx import Document
ImportError: cannot import name 'Document'

Solution

  • There is a PyPi package named docx which is not python-docx. The main module of the docx package is also named "docx", but it has no Document object. You need to get rid of the mistakenly installed one and add the right one. In pip you would remedy that this way:

    $ pip uninstall docx
    $ pip install python-docx