Forums

python-docx not working 'ModuleNotFound'

I am trying to build a new app on my website to allow me to push form data to a doc and figured the python-docx library would be perfect to accomplish this. I got onto my virtual work environment and then I pip installed it as so

pip install python-docx

No problem, installed successfully.

Tried to import it as suggest in the documentation and I keep getting this error

ModuleNotFoundError: No module named 'docx'

I checked the virtual environment folder and see it was installed. I ran the following line as well

pip show python-docx

and it showed it was installed in the right location as well.

I read on github the same error happening and the lxml lib needed to be uninstalled. I tried that, didn't work. Uninstalled python-docx and reinstalled (which reinstalled lxml), didn't work. Not sure what else to do at this point.

https://help.pythonanywhere.com/pages/Virtualenvs

Are you sure that your webapp is running in that venv?

We have a help page dedicated to problems like this https://help.pythonanywhere.com/pages/DebuggingImportError/

I'm positive that it is installed in the virtual environment and that I am running it from within that environment

Where are you seeing that error message? I don't see it in the logs for your website.

Its in the terminal when I run it. I created a file called anewtestfile.py to test it out. This is the message I get

    Traceback (most recent call last):
  File "/home/thecodingrecruiter/the-coding-recruiter-website/tcr/anewtestfile.py", line 1, in <module>
    from docx import Document
ModuleNotFoundError: No module named 'docx'

Have you activated your virtualenv in the terminal? If you're starting a Bash console, then you need to run workon your-env-name to do that before running python your-script.py; if you're running it using the "Run" button in the editor, you should put a "hashbang" at the start of the file -- see the last example on this help page.

Thanks. I added in the hashbang as suggested and am now getting relatively the same thing. I am getting an ImportError.

Here is the error

Traceback (most recent call last):
  File "/home/thecodingrecruiter/the-coding-recruiter-website/tcr/anewtestfile.py", line 3, in <module>
    import docx
ImportError: No module named docx

Here is my Shebang/Hashbang

1
#!/home/thecodingrecruiter/.virtualenvs/myenv python

I've tried it with python, without python, with python2.7 and a few other variations.

Can we take a look at your account, and maybe try to run the file in question in the context of your account? We can do that from our admin interface, but we always ask for permission before looking at your files and data.

Yes, please feel free to do that.

Thank you so much for your continued support of this. This is only my second site on here and deployed out to the web, so I'm still learning quite a bit.

Ah, I should have seen the error in your post of your hashbang above. It should be this:

1
#!/home/thecodingrecruiter/.virtualenvs/myenv/bin/python

-- note that there's no space in the line, it's just the complete path to the Python interpreter in your virtualenv. I've made that change to your file /home/thecodingrecruiter/the-coding-recruiter-website/tcr/anewtestfile.py and you can see that it works now.

Thank you so much Giles. I appreciate all of your help.

No problem, glad we could work it out.

No module named docx problem

We have a help page about how to install modules here: http://help.pythonanywhere.com/pages/InstallingNewModules/