无法安装 python-docx (MacOS)

Unable to install python-docx (MacOS)

我正在尝试安装 docx 包。但得到以下 ImportError:

ImportError: cannot import name Document

所以按照建议 here,我试过了:

pip install python-docx 但出现以下错误(python 版本:2.7.15)

..
..
..

    creating build/lib/docx/templates
    copying docx/templates/default-header.xml -> build/lib/docx/templates
    copying docx/templates/default-settings.xml -> build/lib/docx/templates
    copying docx/templates/default-footer.xml -> build/lib/docx/templates
    error: can't copy 'docx/templates/default-docx-template': doesn't exist or not a regular file 
    Command 

 /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import  

setuptools,tokenize;__file__='/private/var/folders/0c/v_yb4q7n2h3fg94rlfrr165r0000gn/T/pip-build-ks26RP/python-docx/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n',  

'\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/0c/v_yb4q7n2h3fg94rlfrr165r0000gn/T/pip-1SQvtb-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/0c/v_yb4q7n2h3fg94rlfrr165r0000gn/T/pip-build-ks26RP/python-docx/

如果您尝试使用 pip 安装软件包,您需要使用 sudo 除非您正在安装到用户目录。原来如此;

sudo pip install python-docx

您也可以 download the package from pypi 解压它,cd 进入解压目录然后 运行;

sudo python setup.py install

可能还需要将设置工具更新到最新版本。

对于python2.7,pip安装程序下载需要编译的python-docx源,编译过程需要c++构建工具。 xcode command line tools 为要编译的 c++ 源提供所需的库。要安装 xcode command line tools,请使用以下命令:

xcode-select --install

xcode command line tools 之后,再次使用 pip install python-docx,这次编译过程应该完成且没有任何错误,您将在系统上安装 python-docx

在最近发布的 v0.8.9 的某些环境中安装 python-docx 时出现问题。安装 v0.8.10 应该可以解决大多数用户的问题。如果安装仍然失败(在某些 Linux 版本上),据报告更新 setuptools 可以修复它:

$ pip install -U setuptools

更新设置工具对我有用。

我做了一个:

sudo easy_install -U setuptools

然后又安装了一次,效果很好。