ms-wordinstallationpython-3.xpipdocx

Trying to import docx in Python


I would like to be able to open/close/manipulate/read Word documents (docx files) on my Mac using Python. The python-docx module looked really useful, so I have been trying to install it on my system, to no avail.

Here's what I've done so far:

Successfully built python-docx
twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.
Installing collected packages: python-docx
Successfully installed python-docx-0.8.10

I wasn't completely sure what pyhamcrest was, but I installed it anyway just to be safe using pip install pyhamcrest

Frustrated, I also tried easy_install python-docx, the manual version, and even pip install docx . In all cases, when I run IDLE and type from docx import Document or just import docx, I get the following message in the shell:

Traceback (most recent call last):
File "/Users/[my_name]/Desktop/Medical.py", line 3, in <module>
import docx
ModuleNotFoundError: No module named 'docx'

Could anyone help point me in the right direction?


Solution

  • The Anaconda Python distribution has its own system of installation of packages. After the installation of Anaconda, the variable $PATH has been modified so that anaconda python was the first, and OsX's python in the last position.
    If you type "python" in a shell will execute the anaconda python, instead of the standard OsX python (which is 2.7, not 3.x).
    Following the instructions of the package python-docx, you have installed it using pip, which is the default method for installing packages, but this method is not valid for Anaconda Python. So, you finished installing python-docx for the python 2.7 of OsX. To install packages for anaconda, you must run the command
    conda install <package> The python-docx module for anaconda can be found in a separated repository called conda-forge; typing the command

    conda install -c conda-forge python-docx

    you will install the package and the requested dependencies.

    Other useful commands are:
    anaconda-navigator for exploring the Anaconda system

    anaconda-project for managing projects with anaconda

    idle3 for Anaconda Python shell.

    Before start coding, run anaconda-navigator and take a look at the 'Learning' section.

    For a better experience, I suggest PyCharm IDE for Anaconda from JetBrains.