GoAvsGo29
GoAvsGo29

Reputation: 45

pywin32 ImportError: DLL load failed: The specified module could not be found

I'm using python 3.6 and attempting to import win32com.client as win32 to be able to send an email with outlook but keep getting the above import error.

I've installed both pywin32 and pypiwin32 and run the postinstall script (did see it say "You do not have the permissions to install COM objects.)

I see the pywintpyes36.dll and pyhoncom36.dll in the location I'm running the script out of.

I've done a bit of searching and can't seem to find what I'm missing. Is it the fact that I was unable to install the COM objects? Any help would be greatly appreciated, thanks.

[EDIT]

I'm working out of a venv, and have installed both libraries using py -m pip install pywin32 and py -m install pypiwin32. I verified that I see the installation in the "site-packages" folder within my venv.

my imports:

import sys
sys.path.append("C:\path\venv\Lib\site-packages")
import tkinter as tk
import getpass
import os.path
import time
import os
import win32com.client as win32
import sqlite3
from datetime import datetime
from functools import partial

I have the sys.path.append there otherwise the module is not found at all.

Full error message:

Traceback (most recent call last):
File "C:\path\program.py", line 8, in <module>
import win32com.client as win32
File "C:\path\venv\Lib\site-packages\win32com\__init__.py", line 5, in <module>
from win32 import win32api
ImportError: DLL load failed: The specified module could not be found.

Upvotes: 2

Views: 7227

Answers (1)

djvg
djvg

Reputation: 14255

This may not be directly relevant for the OP, but may be of help to others who end up here based on the title.

Summary

Possible workaround for conda environments with Python>=3.8:

  • DO NOT pip install pywin32, but
  • DO conda install pywin32 (e.g. from conda-forge)

Details

I encountered the following error in a freshly created (Mini-)Conda environment with Python 3.9 on windows 10, after installing pywin32 via pip:

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

The pywin32 installation instructions mention this type of issue explicitly, and there are several related issues.

However, in my case something else was going on.

Here are some interesting observations:

  • I have numerous other, pre-existing, conda environments, with python versions ranging from 2.7 to 3.8, and corresponding pywin32 versions, in which the issue does not arise (just verified this).
  • All these environments have their own pywintypesXX.dll etc. yet they coexist peacefully.
  • There is no pywintypesXX.dll in my system32 folder.
  • I have never needed to run the pywin32_postinstall script, yet.

As it turns out, on my system, the import error only arises if I do a pip install pywin32 in a conda environment with Python>=3.8. The issue does not arise for Python 3.7 (nor for 2.7).

In a Python>=3.8 conda environment, conda install pywin32 fixed the issue (instead of using pip).

Apparently Python 3.8 changed the way dll files are found. This change has been incorporated in pywin32, but can still cause trouble if you mix conda and pip.

Related:

Upvotes: 5

Related Questions