-1

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'
Joe
  • 78
  • 1
  • 7
  • Were you running this in anaconda or virtualenv? Also, can we see your code? – C.Nivs May 17 '19 at 18:33
  • in terminal; I can't copy the code cause it contains some delicate info (use it for business purposes) but it's certainly not the code, I applied it, it worked, I closed it, turned the laptop off, turned it on later, doubleclicked the code, ran it, boom. It just seems like the module has been removed from my machine, without me doin anything. – Joe May 17 '19 at 18:34
  • Just the basics of the script, you can remove/exclude sensitive info, we only need to see what you are running to reproduce the problem. For example, if it's the `import`, then all we need is `import ` or whatever you are running there – C.Nivs May 17 '19 at 18:38
  • Also, any extra info regarding how you call your script (bat/bash? command line?) and your platform (windows/mac/unix/linux) can also help pin down the issue – C.Nivs May 17 '19 at 18:41
  • aight one sec, will post it above – Joe May 17 '19 at 18:42
  • Which version of `python-docx` are you using and with what platform? – C.Nivs May 17 '19 at 18:47
  • In High Sierra using python 3.6.3, python-docx version 0.8.10, I can't replicate your issue – C.Nivs May 17 '19 at 18:49
  • yeh me neither I hadn't had any problems with the script so far; absolutely no clue what's goin on. I could try to un- and reinstall python? I'll post the complete error code above if it helps you; the version of python docx is 0.8.9, by platform what do you mean? and my python version is 3.6.4 – Joe May 17 '19 at 18:53
  • per [this issue](https://github.com/pypa/pip/issues/5236) you may need to upgrade `pip` using `curl https://bootstrap.pypa.io/get-pip.py | python3` if you can't use `pip install --upgrade pip` – C.Nivs May 17 '19 at 18:53
  • damn dude I figured it out, an unknown PC was controlling my laptop; when I detected it it immediately disconnected and now it's working... watch out folks... Thanks a lot and sorry for wasting your time C.Nivs – Joe May 17 '19 at 18:57
  • I think I'm more curious now, did that only solve the `pip install` errors or the import? The import doesn't make sense off the bat... but whatever works I guess – C.Nivs May 17 '19 at 19:01
  • haha I promise all I did was relaunching the script after cutting the connection to whoever that was; I didn't even close the script but just relaunched the one which failed by the import (which was still open) and boom all good, no more import errors nothing no errors. – Joe May 17 '19 at 19:12

1 Answers1

2

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
scanny
  • 26,423
  • 5
  • 54
  • 80
  • Nope I precisely tried that too according to one of your previous propositions I found scanny but obtained the exact same results. I swear all I did was cutting off an unknown machine which seemed to be connected to my laptop now it works. No clue what the heck that was. Apologies again for bothering, but who would expect that. – Joe May 17 '19 at 19:03
  • I've followed all the suggestions here and still getting the error message. I'm using Spyder - at one point I could "from docx import Document" from the Anaconda prompt but not from Spyder. Now I cannot even do it in the prompt window! – user3329732 Apr 13 '20 at 11:06