【问题标题】:Unable to install python-docx (MacOS)无法安装 python-docx (MacOS)
【发布时间】:2019-01-10 11:31:07
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    $ pip install -U setuptools
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 命令行工具已安装。问题可能与setuptools 有关。更新setuptools后,它工作了。
      【解决方案3】:

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

      sudo pip install python-docx
      

      你也可以download the package from pypi解压它,cd到解压目录并运行;

      sudo python setup.py install
      

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

      【讨论】:

      • 以上方法均无效。抛出相同的错误 error: can't copy 'docx/templates/default-docx-template': doesn't exist or not a regular file 。它与mac os有关吗?我已经在基于 Unix 的系统上使用 sudo pip install python-docx 进行了安装,它工作正常。
      • setuptools 更新到最新版本并安装python-docx 工作正常。谢谢:)
      • 我更新了我的答案,因为你回复了更新 setuptools 大声笑
      • 顺便说一句,杰克,使用sudo 安装 Python 包可能不是一个好主意,因为它修改了系统 Python 安装。通常,当它“修复”一个问题时,Python 安装还有其他问题,最好从根本原因上进行补救。更多关于这里(和搜索):stackoverflow.com/questions/15028648/…
      • @scanny,“当你想为全局、系统范围的 Python 安装安装东西时,你只能使用 sudo 或提升的权限”.. 在 5 年的 python 开发中,我还没有安装问题全局包,另外它鼓励我了解 python 包的安装位置和方式,以便手动回滚很简单。最重要的是,我觉得安装同一个包的多个版本是一种非常奇怪的行为——我的系统上倾向于有几个 python 版本,它们都有自己的包和所有全局安装的包。我想这是每个人都有自己的情况
      【解决方案4】:

      更新设置工具对我有用。

      我做了一个:

      sudo easy_install -U setuptools

      然后再次安装它,它工作正常。

      【讨论】:

        猜你喜欢
        • 2022-11-25
        • 1970-01-01
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        • 2021-03-05
        • 1970-01-01
        • 2022-09-29
        • 2016-04-04
        相关资源
        最近更新 更多