在python3.3中导入docx时我有错误ImportError:没有名为'exceptions'的模块

问题描述 投票:41回答:5

当我导入docx我有这个错误:

>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'

如何解决此错误(python3.3docx 0.2.4)?

python python-3.x python-docx
5个回答
92
投票

如果你使用的是python 3x,请不要使用pip install docx代替

pip install python-docx 

它与python 3x兼容

官方文件:https://pypi.org/project/python-docx/


14
投票
  1. 使用pip uninstall docx卸载docx模块
  2. python_docx-0.8.6-py2.py3-none-any.whl下载http://www.lfd.uci.edu/~gohlke/pythonlibs/文件
  3. 运行pip install python_docx-0.8.6-py2.py3-none-any.whl以重新安装docx。这为我顺利解决了上述导入错误。只是为了提供解决方案......

3
投票

在Python 3中,异常模块被删除,所有标准异常都被移动到内置模块。因此意味着不再需要明确导入任何标准异常。

copied from


1
投票

正如之前在评论中提到的那样,问题是docx模块与Python 3不兼容。它已在github上的pull-request中修复:https://github.com/mikemaccana/python-docx/pull/67

由于异常现在是内置的,解决方案是不导入它。

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

1
投票

你可能会安装docx,而不是python-docx

你可以看到这个安装python-docx

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

© www.soinside.com 2019 - 2024. All rights reserved.