5

I am trying to install docx package. but getting the following ImportError:

ImportError: cannot import name Document

So as suggested here, I tried :

pip install python-docx but getting the following error (python version: 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/
Aditya Gupta
  • 157
  • 1
  • 8

4 Answers4

3

There was a problem installing python-docx in certain environments in the recently released v0.8.9. Installing v0.8.10 should rectify that for most users. If installation still fails (on certain Linux versions), updating setuptools has been reported to fix it:

$ pip install -U setuptools
scanny
  • 26,423
  • 5
  • 54
  • 80
0

If you are trying to install packages with pip you need to use sudo unless you are installing to a user directory. So it would be;

sudo pip install python-docx

You could also download the package from pypi untar it, cd into untared directory and run;

sudo python setup.py install

Updating setuptools to latest version may also be needed.

Jamie Lindsey
  • 928
  • 14
  • 26
  • None of the above worked. Throwing the same error `error: can't copy 'docx/templates/default-docx-template': doesn't exist or not a regular file` . Is it something related to mac os? I have installed using `sudo pip install python-docx` on my Unix based system, there it works fine. – Aditya Gupta Jan 10 '19 at 12:45
  • Updating `setuptools` to latest version and installing `python-docx` worked. Thanks :) – Aditya Gupta Jan 10 '19 at 13:00
  • I updated my answer as you replied with updating setuptools lol – Jamie Lindsey Jan 10 '19 at 13:04
  • Btw Jack, installing Python packages using `sudo` is probably not a terrific idea as it modifies the system Python installation. Generally when that "fixes" a problem there's something else wrong with the Python installation that is better remedied at its root cause. More on that here (and on search): https://stackoverflow.com/questions/15028648/is-it-acceptable-and-safe-to-run-pip-install-under-sudo – scanny Jan 12 '19 at 07:08
  • @scanny, "You only use sudo or elevated permissions when you want to install stuff for the global, system-wide Python installation".. in 5 years of python development I have yet to have issues installing packages globally, plus it encouraged me to learn where and how python packages are installed so that manual rollbacks are simple. On top of that I feel that install several versions of the same package to be a very strange behaviour - I tend to have several python versions on my system all with there own packages and all globally installed packages. I guess its an each to there own situation – Jamie Lindsey Jan 12 '19 at 09:02
0

For python2.7, pip installer downloads source for python-docx which requires to be complied, c++ build tools are required for the compilation process. xcode command line tools provides required libraries for c++ sources to be compiled. To install xcode command line tools, use following command:

xcode-select --install

After xcode command line tools, use pip install python-docx again, this time compilation process should complete without any error and you will have python-docx installed on your system.

arryph
  • 2,725
  • 10
  • 15
  • command line tools were already installed. Issue might be something related to `setuptools`. After updating `setuptools`, it worked. – Aditya Gupta Jan 10 '19 at 13:05
0

Updating setup tools worked for me to.

I did a:

sudo easy_install -U setuptools

Then installed it again and it worked fine.

ZF007
  • 3,708
  • 8
  • 29
  • 48
nonoti
  • 1