How can I add pywin32 into my add-on?

Hi, how can I add pywin32 to my addon? This is what my wheels look like:

wheels = [
    "./wheels/PySide6_Essentials-6.8.0.2-cp311-abi3-win_amd64.whl",
    "./wheels/shiboken6-6.8.0.2-cp311-abi3-win_amd64.whl",
    "./wheels/pywin32-308-cp311-cp311-win_amd64.whl"
]

pywin32 files are properly installed into ...\Blender\4.3\extensions\.local\lib\python3.11\site-packages\win32, etc… (they’re .pyd files, not sure if this makes a difference)

The error I’m getting is:
RuntimeError: Error: No module named 'win32gui'
(when doing import win32gui)

Thanks

The way it works pywin32 doesn’t just install the package win32 package, it also creates pywin32.pth file in site-packages:

# .pth file for the PyWin32 extensions
win32\lib
Pythonwin
# And some hackery to deal with environments where the post_install script
# isn't run.
import pywin32_bootstrap

This file basically adds more directories to sys.path, so python is able to find win32gui.pyd.

sys.path
# [..., 'C:\\Python311\\Lib\\site-packages\\win32', 'C:\\Python311\\Lib\\site-packages\\win32\\lib', 'C:\\Python311\\Lib\\site-packages\\Pythonwin']

So, the questions are:

  1. Does Blender also installs .pth file to site-packages or it’s lost?
  2. Does Blender’s path is taking into account .pth file? (check sys.path when you’re sure extension is loaded, also can try to restart Blender after).

If either .pth file is missing or sys.path is not modified, then it seems like a Blender bug that you can report to https://projects.blender.org/

you’re in kinda luck i’ve got a hack, but it causes a minor delay at startup. was planning to look into it today, bizarre to stumble on this

i needed this for pyttsx3 for blender-text-to-speech addon but be warned it’s very much a hack so you may benefit from me investigating it. I’ll jot down my notes anyway and links to the code.

Step 1 - check for pyttsx3

First code to run in init.py I try and import pyttsx3 within a try-except block, and install it if it’s missing.

Step 2 - check for pywin32

Next is a similar try-catch to try import pywintypes, however if missing i had to install pypiwin32,
note the subtle difference i.e import pywintypes, pip install pypiwin32 and you will get pywin32… kinda almost

issue 1: .DDL files in wrong path

After install i found two .ddl files weren’t in the right path. I didn’t investigate why, I just moved the files but hopefully i just need to change my pip --target flag but I also found…

issue 2: new lib paths need to be added to sys.path

unsure what this is a side effect of or just confounder… for now i just check/add the paths

by the way…

***> if you can run blender with the system python environment might be much easier, you can delete the blender python dir to force blender to use the system wide install or you can open blender with the --python-use-system-env flag just make sure the python versions are the same ***

back to Step 2 - check for pywin32- check for pywin32

Try import pywintypes in a try-except block. If it imports, hopefully it works and you can stop here.

A ModuleNotFoundError exception could mean multiple things so in that case I do a check to see if one of the .ddl files are where it’s meant to be. If the file is there, it’s been installed and just needs the lib paths to be appended

If the files aren’t there, it’s not installed, then I:

  1. do the pip install of the module pypiwin32

  2. move the ddl files:

    pythoncom310.dll
    pywintypes310.dll

    Are installed into:
    BASE/lib/pywin32_system32

    But I found I needed them to be moved to:
    BASE/lib/win32/lib

    Where BASE is the path to blenders python interpreter, Which currently in windows I’m getting with:

    Path(str(sys.executable)).parent.parent

    (prior to version 2.92 you can use bpy.app.version)

  3. append the lib paths the sys path

It seems I added two paths to sys.path:

`BASE/lib/win32`
`BASE/lib/win32/lib`

surprisingly has worked across blender releases so I just forgot about it…

So if i had to guess it all stems from the ddl not being in the right place, maybe it’s all been fixed in the new blender i’ll try and do a bit more digging, it’s been years and I spotted atleast 1 careless bug…