Search code examples
pythonnode.jspython-3.xpython-docx

Error: ImportError: No module named docx --> using nodeJS python-shell package to control python script


I am trying something relatively simple but getting this error:

Error: ImportError: No module named docx

Here is my nodeJS script:

const python = require('python-shell');

const shell = new python('../Python/test.py');

let names = ['Hubert', 'Rupert', 'Sherbert', 'Wubbert', 'Paul'];

shell.send(JSON.stringify(names));

shell.on('message', message => console.log(message));

shell.end(message => {console.log(message)});

the python script "test.py":

import sys, json
from docx import Document

names = json.loads(sys.stdin.readlines()[0])
document = Document('test.docx')

for name in names:
    for paragraph in document.paragraphs:
        if '$$Name$$' in paragraph.text:
            paragraph.text = name
            document.save(name+'.docx')

print('completed')

The test.docx is a blank word file that has "$$Name$$" written at the top.

Any ideas? When I run any tests in pyCharm using docx it works fine and does not provide this error. Only when I call through python-shell in my node script.

I have tried setting the options like this:

const python = require('python-shell');

let options = {
    pythonPath : '/usr/local/bin/python3'
};

python.defaultOptions = options;

const shell = new python('../Python/test.py');

// rest of code is the same

I verified that this path is where python3 is located on my mac

I have had no luck using these options. All this does is provide a similar error: Error: docx.opc.exceptions.PackageNotFoundError: Package not found at 'test.docx


Solution

  • These are two different errors. The first means Python can't find the docx package (module). The second means the .docx file is either not found or not a valid .docx file.

    The second error is better; because that error comes from the docx library itself, it means the library is found and loaded. So whatever you're doing to make it find the docx package, you want to keep doing that.

    A common cause for getting the package not found error is that the current directory Python is using is different than you think it is and the file isn't found because the actual file path it's using doesn't exist.

    Note that the term "package" is used differently in two different contexts here. One is a Python package, like python-docx that you install with pip. The second is an Open Packaging Convention (OPC) package, which is what a .docx file is. So the second error is saying "I can't find a .docx file at the path you gave me" where the first one is saying "I don't think you installed the python-docx Python library".