Skip to content

ImportError: DLL load failed while importing win32api: The specified procedure could not be found #2597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
clckwrkbdgr opened this issue May 5, 2025 · 2 comments

Comments

@clckwrkbdgr
Copy link

Expected behavior and actual behavior

Since version >=309, importing any module after installation results in ImportError.

Expected:

No ImportError.

Steps to reproduce the problem

Any import statement fails:

    import win32api
ImportError: DLL load failed while importing win32api: The specified procedure could not be found.

I've tried to re-run python pywin32_postinstall.py -install manually, but it results in following error:

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

I've even copied libraries from pywin32_system32 to C:\Windows\system32 manually, but no luck.

I see no depedency issues with those dll, and import pywintypes work ok.

Reverting to pywin32==308 also work ok, no import errors.

System information

Python version and distribution:
Python 3.12.2

pywin32 version:
Version: 310

Windows Version:
Edition Windows 11 Enterprise
Version 24H2
OS build 26100.3775

DLL locations:

C:\Program Files\Python312\pywintypes312.dll
C:\Users\<user>\AppData\Roaming\Python\Python312\site-packages\pywin32_system32\pythoncom312.dll
@Avasam
Copy link
Collaborator

Avasam commented May 5, 2025

Looks like you have one DLL installed in your user site-packages, and the other directly in your global Python 3.12 install. This is suspicious. let's try to cleanly uninstall pywin32, remove possibly leftover dlls, and reinstall.

Could you

  1. uninstall pywin32
    1.1. Run the postinstall remover: pywin32_postinstall -remove. This should remove the dlls from the system folder. If it fails, you can remove the dlls manually as listed below.
    1.2. Uninstall the pywin32 package itself (pip uninstall -y pywin32 and/or python -m pip uninstall -y pywin32 and/or py -3.12 -m pip uninstall -y pywin32)
  2. then delete these:
    • pywintypes312.dll
    • pythoncom312.dll
    • site-packages\pywin32_system32
      from the following locations:
      • C:\Program Files\Python312\
      • %appdata%\Python\Python312\
      • C:\Windows\system32 (should already be done by pywin32_postinstall -remove)
  3. then re-install pywin32
  4. if you needed it, re-run the postinstall script (pywin32_postinstall -install)

Then please rerun python -c "import pywintypes,pythoncom;print(pywintypes.__file__,pythoncom.__file__,sep='\n')" to check the new locations if they make more sense.

Note that any file moved manually in the past cannot be detected by a python uninstaller (here pip)

@clckwrkbdgr
Copy link
Author

python pywin32_postinstall.py -remove failed with the same error:

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

Uninstalled pywin32 manually and removed all found libraries.

Had to clean pywintypes312.dll manually from C:\Program Files\Python312\. I have userservices written in Python that are constantly running and apparently they block this lib from being auto-removed during post-install action so it remains forever.


Installed pywin32 again.

> python -c "import pywintypes,pythoncom;print(pywintypes.__file__,pythoncom.__file__,sep='\n')"
C:\Users\<user>\AppData\Roaming\Python\Python312\site-packages\pywin32_system32\pywintypes312.dll
C:\Users\<user>\AppData\Roaming\Python\Python312\site-packages\pywin32_system32\pythoncom312.dll

python -c "import win32api" work ok now, but when I tried to start userservices, they failed with following exception:

The service did not respond to the start or control request in a timely fashion.
Possible reason: Missing library in pywin32 module directory:
C:\Users\<user>\AppData\Roaming\Python\Python312\site-packages\win32\pywintypes312.dll
Found this lib in different location:
C:\Users\<user>\AppData\Roaming\Python\Python312\site-packages\pywin32_system32\pywintypes312.dll
Try to copy it to C:\Users\<user>\AppData\Roaming\Python\Python312\site-packages\win32\pywintypes312.dll and re-check

Attempted to run python pywin32_postinstall.py -install, but it still failed with the same error:

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

So I copied them manually as message suggested, but userservice still fail to start, no details this time:

The service did not respond to the start or control request in a timely fashion.

Finally copied both libs manually back to C:\Program Files\Python312\ and userservices are starting successfully again, and import also continues to work.

Displayed locations have changed though:

> python -c "import pywintypes,pythoncom;print(pywintypes.__file__,pythoncom.__file__,sep='\n')"
C:\Program Files\Python312\pywintypes312.dll
C:\Program Files\Python312\pythoncom312.dll

For now I will probably pay attention to all of these locations and perform manual check/cleaning/installation after every update of the package.

I assume something is off either with the way pywin32 installer accesses these system locations (but it worked prior to 308, or maybe something have changed during the latest Windows upgrade?), or with my environment settings. I have these entries in my %PATH%:

C:\Users\<user>\AppData\Roaming\Python\Python312\Scripts
C:\Program Files\Python312
C:\Program Files\Python312\Scripts\
C:\WINDOWS\system32

Should I maybe add some of these to the PATH as well?

C:\Users\<user>\AppData\Roaming\Python\Python312\site-packages\win32\
C:\Users\<user>\AppData\Roaming\Python\Python312\site-packages\pywin32_system32\

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants