当我导入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.3
,docx 0.2.4
)?
如果你使用的是python 3x,请不要使用pip install docx
代替
pip install python-docx
它与python 3x兼容
pip uninstall docx
卸载docx模块python_docx-0.8.6-py2.py3-none-any.whl
下载http://www.lfd.uci.edu/~gohlke/pythonlibs/文件pip install python_docx-0.8.6-py2.py3-none-any.whl
以重新安装docx。这为我顺利解决了上述导入错误。只是为了提供解决方案......在Python 3中,异常模块被删除,所有标准异常都被移动到内置模块。因此意味着不再需要明确导入任何标准异常。
正如之前在评论中提到的那样,问题是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
你可能会安装docx
,而不是python-docx
你可以看到这个安装python-docx
http://python-docx.readthedocs.io/en/latest/user/install.html#install