0

For admin reasons I can't edit the PATH variable so I couldn't use pip to install my PyPi and 3rd party libraries/modules, so I worked around it and I installed their source code using the official download from PyPi.com

All went well with the PyPDF2 module, but the docx module just won't get imported in my script. The folder is saved in the same location as the main script and the PyPDF2 library. This is the code I used to make sure that the correct path was added to the main script:

import re, sys, os
os.chdir(r"\\WWG00M.ROOTDOM.NET\AFS-HOME5\GDOVERI\ICM\Desktop\PDF_Compare")
sys.path.insert(0, os.path.abspath(r'\\WWG00M.ROOTDOM.NET\[...]\PyPDF2-3.0.1'))
sys.path.insert(0, os.path.abspath(r'\\WWG00M.ROOTDOM.NET\[...]\python_docx-1.1.2'))
sys.path.append(r'\\WWG00M.ROOTDOM.NET\[...]\python_docx-1.1.2')
import PyPDF2
import docx

I added those sys.path lines because otherwise the script fails to import the modules, but I can't get to import the docx one even after adding sys.path.insert and sys.path.append.

What am I missing here?

1 Answer 1

0

I think the problem is the path. When you install Docx manually, the files to import are inside python_docx-1.1.2\src\docx. So, to import docx the path is python_docx-1.1.2\src. This does not happen with PyPDF because the "PyPDF2" folder with the files is already inside "PyPDF2-3.0.1" (PyPDF2-3.0.1\PyPDF2)

In other words, try to change the path r'\\WWG00M.ROOTDOM.NET\[...]\python_docx-1.1.2' to r'\\WWG00M.ROOTDOM.NET\[...]\python_docx-1.1.2\src'

(Remember to install lxml>=3.1.0 for docx)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Alex, that indeed solved the issue with the docx module. Now I can't get lxml to work properly. I installed the lxml module manually, added the folder to sys.path and imported it, but I get an ImportError for the etree method: "cannot import name 'etree' from 'lxml'". Any idea why?
That's probably the lack of dependences for lxml. I think it needs Cython, libxml2 and libxslt. I get that you can't use pip install for modules to your global enviroment due to admin privileges, but have you tried a virtualenv?
Thanks for the suggestion, I didn't know there was such a deep game of dependencies going on with these libraries ahah I'll try to download and install the 3 you mentioned and make them work. What virtual environment would you suggest? I am trying to do all of this in my company remote desktop and I have some limitations on the web browser too, so I might not be able to access developing and file sharing websites.
You're welcome! Python has a native module called "venv". Correct me if I'm wrong, you can't add the pip to path, but your python is, right? So you can try 2 things on your console: Use python -m pip install to install libraries; Create a virtual enviroment using python -m venv .venv, this create an virtual enviroment on the path the console is opened (Read more about "venv"). If these options do not work, sorry but idk how to help :(
didn't know about the venv, thx :) I can't edit any of the environment variables in my remote desktop. I even have to run my python scripts using the absolute path to python.exe :') I'm not a company dev, of course, I'm testing a couple of scripts to make my work easier but I don't have full Windows admin rights... btw I tried using pip this way but it turns out the connection to their servers is impossible because my company uses a proxy whose IP/port is hidden, so I guess I'll have to make do with manual installations. Don't worry, you've already helped a lot, thanks a million!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.