导入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
)?
答案 0 :(得分:74)
如果您使用的是python 3x,请不要pip install docx
代替
pip install python-docx
它与python 3x兼容
答案 1 :(得分:13)
pip uninstall docx
python_docx-0.8.6-py2.py3-none-any.whl
文件
pip install python_docx-0.8.6-py2.py3-none-any.whl
以重新安装docx。
这为我顺利解决了上述导入错误。只是为了提供解决方案...... 答案 2 :(得分:9)
要使用import docx
时,请确保安装 python-docx ,而不是 docx 。您可以安装模块通过运行pip install python-docx
。
安装名称 docx 用于其他模块 但是,
当您要导入python-docx模块时,
你需要跑步
import docx
,而不是import python-docx
。
如果您仍要使用docx模块,则:
首先,您需要确保已安装 docx 模块。
如果没有,那么只需运行pip install docx
。
如果显示“ *已满足需求*”
那么解决方案是:
在文本编辑器中打开 docx.py 文件并找到此代码
from exceptions import PendingDeprecationWarning
try:
from exceptions import PendingDeprecationWarning
except ImportError:
pass
答案 3 :(得分:8)
如果您使用的是python 3.x,请确保同时安装了 python-docx 和 docx 。
安装python-docx:
pip install python-docx
安装docx:
pip install docx
答案 4 :(得分:2)
在Python 3中,异常模块已被删除,所有标准异常都被移至内置模块。因此意味着不再需要明确导入任何标准异常。
答案 5 :(得分: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
答案 6 :(得分:1)
您可以安装docx
,而不是python-docx
您可以在安装python-docx
http://python-docx.readthedocs.io/en/latest/user/install.html#install
答案 7 :(得分:0)
我遇到了同样的问题,但是dir()
对我有用,我使用的是python 3.7.1
答案 8 :(得分:0)
您需要使其与python3一起使用。
sudo pip3 install python-docx
此安装在Python3中对我有效,无需任何其他添加。
python3
>> import docx
PS:请注意,'pip install python-docx'或apt-get python3-docx都没有用。