Skip to content

Unable to install pywin32 package #137

@jtomori

Description

@jtomori

Hello,
I'd like to report issue with using pywin32 package (the latest version at the moment: 308) installed with rez pip.

Install command: rez pip -p [redacted] -i --python-version 3.11 pywin32

I've encountered the same issue in Python 3.9, 3.10, 3.11 (haven't tested newer or older versions).

Error occurs when I try to import a pywin32's submodule, e.g. win32cred.

rez env pywin32 python-3.10 -- python
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32cred
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing win32cred: The specified module could not be found.

Investigation

While below I've used win32cred as an example, more (if not all) submodules might be affected by this issue.

My current understanding of the issue is that win32cred.pyd (located in {root}/python/win32) is dynamically linked against pywintypes310.dll, which is present in {root}/python/pywin32_system32.

Python versions 3.7 and lower solved it by adding the pywin32_system32 path into the PATH environment variable, but that is no longer possible in newer versions. Instead os.add_dll_directory() is available for adding DLL search paths at runtime.

In fact, that's what I believe pywin32 is doing when installed directly with pip into an venv: it uses pywin32.pth to call import pywin32_bootstrap, which in turn calls os.add_dll_directory() for Python 3.8 and newer.

Fix

The easiest solution (inspired by this explanation) for me was to copy both DLLs from pywin32_system32: pythoncom310.dll, pywintypes310.dll into the win32 directory, so that they are next to the .pyd modules linking against them. This has solved the issue I was facing. Potentially those two DLLs might need to be copied into other folders as well if there are .pyd files requiring them.

An alternative solution would be to copy both DLLs into C:\Windows\System32 (or Python's installation folder), which would require admin rights - and for a good reason - it's probably not the best idea to copy random stuff there.

An additional improvements that I've made to package.py is to add the following paths into PYTHONPATH in the commands() section (as does pywin32.pth):

    env.PYTHONPATH.prepend("{root}/python/Pythonwin")
    env.PYTHONPATH.prepend("{root}/python/win32/lib")
    env.PYTHONPATH.prepend("{root}/python/win32")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions