158

when I import docx I have this error:

  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/docx-0.2.4-py3.3.egg/docx.py", line 30, in <module>
    from exceptions import PendingDeprecationWarning
ImportError: No module named 'exceptions'

How to fix this error (python3.3, docx 0.2.4)?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
user3472559
  • 1,691
  • 2
  • 10
  • 3
  • 3
    The `exceptions` module does not exist in Python 3 (exceptions defined there were added to `__builtin__` anyway). Looks like the conversion of DocX to Python 3 is not complete yet. – Frédéric Hamidi Mar 31 '14 at 15:25

11 Answers11

312

If you are using python 3x don't do pip install docx instead go for

pip install python-docx 

It is compatible with python 3.x

Official Documentation available here: https://pypi.org/project/python-docx/

Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
Arun
  • 3,440
  • 1
  • 11
  • 19
33

When want to use import docx, be sure to install python-docx, not docx.You can install the module by running pip install python-docx.

The installation name docx is for a different module However,

when you are going to import the python-docx module, you’ll need to run import docx, not import python-docx.

if still you want to use docx module then:

First of all, you will need to make sure that the docx module is installed. If not then simply run pip install docx. If it shows '*requirement already satisfied*' then the solution is :

  1. Go to the library to find docx.py file, you'll need to go to directory where you installed python then \Lib\site-packages\ and find docx.py file
  2. Open docx.py file in text editor and find this code

    from exceptions import PendingDeprecationWarning
    
  3. Replace the above code with
try:
    from exceptions import PendingDeprecationWarning
except ImportError:
    pass
  1. Save the file
  2. Now you can run import docx module in Python 3.x without any problem
Sameer Khan
  • 331
  • 3
  • 4
20
  1. Uninstall docx module with pip uninstall docx
  2. Download python_docx-0.8.6-py2.py3-none-any.whl file from http://www.lfd.uci.edu/~gohlke/pythonlibs/
  3. Run pip install python_docx-0.8.6-py2.py3-none-any.whl to reinstall docx.

This solved the above import error smoothly for me.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Vancent
  • 477
  • 5
  • 16
15

If you're using python 3.x, Make sure you have both python-docx & docx installed.

Installing python-docx :

pip install python-docx

Installing docx :

pip install docx
Kalpit
  • 171
  • 1
  • 5
10

You may be install docx, not python-docx

You can see this for install python-docx

http://python-docx.readthedocs.io/en/latest/user/install.html#install

fedotsoldier
  • 190
  • 1
  • 16
某某某
  • 325
  • 3
  • 9
8

In Python 3 exceptions module was removed and all standard exceptions were moved to builtin module. Thus meaning that there is no more need to do explicit import of any standard exceptions.

copied from

sajid
  • 807
  • 1
  • 9
  • 23
5

pip install python-docx

this worked for me, try installing with admin mode

Guru Prasad
  • 51
  • 1
  • 1
3

The problem, as was noted previously in comments, is the docx module was not compatible with Python 3. It was fixed in this pull-request on github: https://github.com/mikemaccana/python-docx/pull/67

Since the exception is now built-in, the solution is to not import it.

docx.py
@@ -27,7 +27,12 @@
 except ImportError:
     TAGS = {}

-from exceptions import PendingDeprecationWarning
+# Handle PendingDeprecationWarning causing an ImportError if using Python 3
+try:
+    from exceptions import PendingDeprecationWarning
+except ImportError:
+    pass
+
 from warnings import warn

 import logging
dsh
  • 12,037
  • 3
  • 33
  • 51
Dmitry
  • 31
  • 2
1

You need to make it work with python3.

                     sudo pip3 install python-docx

This installation worked for me in Python3 without any further additions.

             python3
             >> import docx

PS: Note that the 'pip install python-docx' or apt-get python3-docx are not useful.

Shagun
  • 11
  • 1
  • You can't really say 'pip install python-docx' is not useful, as it does exactly the same as the pip3 command if you are in a python3 environment – The Javatar Apr 05 '20 at 15:46
0

I had the same problem, after installing docx module, got a bunch of errors regarding docx, .oxml and lxml...seems that for my case the package was installed in this folder:

C:\Program Files\Python3.7\Lib\site-packages

and I moved it one step back, to:

C:\Program Files\Python3.7\Lib

and this solved the issue.

Salio
  • 1,058
  • 10
  • 21
alex
  • 1
0

python imports exceptions module by itself comment out import line. it worked for me.

VaibhavP
  • 1
  • 1
  • 1
    They don't have an "import" line (an "import exceptions" line). An external module does. That's the problem. Well, since the question is 8 years old, it is more than probable that that bug in that module has been solved for now. Please, when answering to old question, be sure to understand both questions and existing answer, and explain how your answer bring a new information that was not given at the time. – chrslg Dec 08 '22 at 11:26