Blackbull
Blackbull

Reputation: 39

from lxml import etree ImportError: DLL load failed: The specified module could not be found

I recently installed a fresh Anaconda version 2019-10, which uses python 3.7.4. To be able to read/write MsWord .docx files from within Python I use library module python-docx, which I installed with:conda install -c conda-forge python-docx Installed is python-docx 0.8.10. Now a python script that I frequently used to read/write MsWord .docx files with my previous anaconda installation (with python3.5.4 and python-docx version unknown to me).

script: (shortened)


    import docx
    doc = docx.Document('demo.docx') # demo.docx exists in same dir
    print(len(doc.paragraphs))

suddenly throws an error:

Traceback (most recent call last):

  File "D:\pa\Python\ProjectsWorkspace\Py001Proj\src\printenfrompython\wordprinten.py", line 19, in <module>
    import docx
  File "C:\Users\pa\Anaconda3\lib\site-packages\docx\__init__.py", line 3, in <module>
    from docx.api import Document  # noqa
  File "C:\Users\pa\Anaconda3\lib\site-packages\docx\api.py", line 14, in <module>
    from docx.package import Package
  File "C:\Users\pa\Anaconda3\lib\site-packages\docx\package.py", line 9, in <module>
    from docx.opc.package import OpcPackage
  File "C:\Users\pa\Anaconda3\lib\site-packages\docx\opc\package.py", line 9, in <module>
    from docx.opc.part import PartFactory
  File "C:\Users\pa\Anaconda3\lib\site-packages\docx\opc\part.py", line 12, in <module>
    from .oxml import serialize_part_xml
  File "C:\Users\pa\Anaconda3\lib\site-packages\docx\opc\oxml.py", line 12, in <module>
    from lxml import etree
ImportError: DLL load failed: The specified module could not be found.

Is there a solution to this problem? I cannot go back to my previous installation!

Today I installed python-docx in my plain python3.7.5 installation using: 'pip install python-docx` Now the above error does not occur. Re-installed Anaconda and re-installed python-docx in anaconda with pip and the same error as above occurs. My OS is windows10. I run the test in both cases with Eclipse\PyDev, and switch in PyDev between the python3.7.5 and anaconda python3.7.4 interpreter.

Upvotes: 3

Views: 12392

Answers (3)

yusuf imran
yusuf imran

Reputation: 41

The simplest solution is (already provided above): Step 1: pip uninstall lxml Step 2: pip install lxml

However, while re-installing lxml through lxml-4.6.3.tar.gz, I faced problems. So, I tried the re-installation through the corresponding .whl file and succeeded!

Upvotes: 1

ujjwal_bansal
ujjwal_bansal

Reputation: 365

conda version : 4.9.2 (Installed in system) Trying to run:

scrapy startproject name_of_project

Error:

ImportError: DLL load failed while importing etree: The specified module could not be found.

Solution:

Step 1: pip uninstall lxml

Step 2: pip install lxml

Error resolved

New Scrapy project 'tutorial', using template directory 'C:\Users\New\miniconda3\lib\site-packages\scrapy\templates\project', created in:
    C:\Users\New\Documents\TOUR_TOC++\web_scraping\NSE\tutorial
    You can start your first spider with:
         cd tutorial
         scrapy genspider example example.com ...

Upvotes: 9

Blackbull
Blackbull

Reputation: 39

Solved!. During an attempt to re-install the lxml==4.1.1 package, the error log reported a number of errors, which contained hints for a solution.

  1. ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" ** make sure the development packages of libxml2 and libxslt are installed **
  2. building 'lxml.etree' extension:. error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/

So there might be a problem in in building lxml.etree because the correct Visual C++ compiler is not available. This appears to be confirmed in an article of Michael Hirsch dated 21 September, 2019 titled “Fix Python 3 on Windows error: Microsoft Visual C++ 14.0 is required” , see https://www.scivision.dev/python-windows-visual-c-14-required/

According to his advice I installed Microsoft Visual C++ 14.0 using offline installer: vs_buildtools.exe Select: Workloads → C++ build tools. Install options: select only the “Windows 10 SDK” (assuming the computer is Windows 10). To use MSVC cl.exe C / C++ compiler from the command line, additionally select the C++ build tools.

Then I installed libxml2 and lxml, which solved the problem:

try:
  from lxml import etree
  print("running with lxml.etree !")
except ImportError:
  print("Import etree from lxml failed !"

> running with lxml.etree!

Upvotes: 1

Related Questions