无法安装python-docx(MacOS)

时间:2019-01-10 11:31:08

标签: python macos python-2.7 importerror python-docx

我正在尝试安装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/

4 个答案:

答案 0 :(得分:1)

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

$ pip install -U setuptools

答案 1 :(得分:0)

如果您尝试使用pip安装软件包,除非要安装到用户目录,否则需要使用sudo。就是这样

sudo pip install python-docx

您也可以download the package from pypi解压缩它,将其cd到解压缩的目录中并运行;

sudo python setup.py install

可能还需要将setuptools更新到最新版本。

答案 2 :(得分:0)

对于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

答案 3 :(得分:0)

更新设置工具对我有用。

我做了一个:

sudo easy_install -U setuptools

然后再次安装它,效果很好。