Forums

docx libraries

Hi,

I'm quite new to PythonAnywhere but loving it so far.

I have an app which opens a word document and interrogates it using the python libraries docx, docxshared and docx2python.

I ran pip install successfully in the bash Console for docx and docx2python libraries.

I am getting an error on the web page which seems to indicate that the docx library isn't importing. Here is my flaskapp.py code

from flask import Flask
from PythonME import main_prog

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route('/', methods=["GET","POST"])
def adder_page():
    result = main_prog()
    return result

if __name__ == "__main__":
    app.run(debug=True)

and my pythonME code is

from docx import *
from docx.shared import Pt
from docx2python import docx2python

def main_prog():
    document = Document('/home/dracstorey/mysite/countries.docx')
    len(document.paragraphs)
    return "Well done!"

Any help pointing me in the right direction would be appreciated.

What errors do you see in your logs? (link to logs are available on the "Web" page on PythonAnywhere).

[edited by admin: formatting]

    2020-11-22 15:24:20,257: Error running WSGI application
    2020-11-22 15:24:20,258: ModuleNotFoundError: No module named 'docx'
    2020-11-22 15:24:20,258:   File "/var/www/dracstorey_pythonanywhere_com_wsgi.py", line 16, in <module>
    2020-11-22 15:24:20,258:     from flask_app import app as application  # noqa
    2020-11-22 15:24:20,259: 
    2020-11-22 15:24:20,259:   File "/home/dracstorey/mysite/flask_app.py", line 5, in <module>
    2020-11-22 15:24:20,259:     from PythonME import main_prog
    2020-11-22 15:24:20,259: 
    2020-11-22 15:24:20,259:   File "/home/dracstorey/mysite/PythonME.py", line 1, in <module>
    2020-11-22 15:24:20,260:     from docx import *
    2020-11-22 15:24:20,260: ***************************************************
    2020-11-22 15:24:20,260: If you're seeing an import error and don't know why,
    2020-11-22 15:24:20,260: we have a dedicated help page to help you debug: 
    2020-11-22 15:24:20,260: https://help.pythonanywhere.com/pages/DebuggingImportError/
    2020-11-22 15:24:20,261: ***************************************************

Have you tried https://help.pythonanywhere.com/pages/DebuggingImportError/ ?

HI

Yes. So far:

  • I've found the docx.py file in the .local/lib/python3.8 folder so it is there.
  • As per the article I can run python3.8 -i /var/www/dracstorey_pythonanywhere_com_wsgi.py in bash console I get the ModuleNotFound error that it can't find the exceptions module. from the docx import line 1 ..

I thought the exceptions module was usually imported by default when python runs? Anyone else have this issue or know a fix? When I run the code on a local machine it's fine.

How did you install docx package? For Python 3.x you should install python-docx not docx.

Hi

I've literally just done and was about to post. It worked.

Thanks for all your help

Excellent, glad to hear you got it to work!