Skip to content

Pywin32 - DLL load failed while importing _win32sysloader #2580

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
Kuhndog444 opened this issue Apr 25, 2025 · 15 comments
Open

Pywin32 - DLL load failed while importing _win32sysloader #2580

Kuhndog444 opened this issue Apr 25, 2025 · 15 comments

Comments

@Kuhndog444
Copy link

Hello there :)
First of all, I hope this is the correct place for this request, if not, please let me know and I can put it where it belongs.
I tried to import win32com.client using Revit 2025's dynamo V.3.3.0 with Revit's preinstalled python 3.9.12 so I could send Emails because the marshal app has been downgraded. The best and easiest way I can see to get access the Outlook Application is to use win32com.client. I am trying to import it and it keeps throwing the error that _win32sysloader can't be found. I'm new to downloading packages for my scripts so, I'm not sure if I'm doing it correctly but, I've tried creating a Venv several times and installing pywin32 with different pythons and pips, e.g. python, python3, python3.13, pip, pip3, pip3.13. I can't figure out what's wrong and I keep running into the same issue.
Any help you could offer me in fixing this error would be greatly appreciated!

Expected behavior and actual behavior

I would think win32com.client could be imported

Actual behavior is Error Message: DLL load failed while importing _win32sysloader: ... "\lib\pywintypes.py", line 50, in import_pywin32_system_module\n import _win32sysloader

as you can see in my code below, I needed to append the path to find the other dlls etc. as well.

Steps to reproduce the problem

#Imports
#-------
import clr
import sys
sys.path.append(r'C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages')
sys.path.append(r"C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages\win32")
sys.path.append(r"C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages\win32\lib")
sys.path.append(r"C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages\win32ctypes\pywin32")
sys.path.append(r"C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages\win32ctypes")

import win32com.client

  1. import win32com.client

System information

Python version and distribution:
<python 3.13 & 3.9.12>

pywin32 version:
pywin32-310

Windows Version:
<Version 10.0.19045 Build 19045>

DLL locations:
installed with pip3.13 into Venv 'newenv' listed in the code above so all DLLs should be there except for the _win32sysloader.dll, however, _win32sysloader.pyd is located under win32 in the site-packaged folder.

@geppi
Copy link
Collaborator

geppi commented Apr 26, 2025

sys.path.append(r'C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages')
sys.path.append(r"C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages\win32")
sys.path.append(r"C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages\win32\lib")
sys.path.append(r"C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages\win32ctypes\pywin32")
sys.path.append(r"C:\Users##\OneDrive - MoodyNolan, Inc\Desktop\Venv\newenv\Lib\site-packages\win32ctypes")

These paths look strange to me.
Not that the hash character # or comma , would be forbidden in a Windows filename but did you try the imports on an installation with less esoteric filenames?

@Kuhndog444
Copy link
Author

Sorry for the misunderstanding for the filepaths, I changed the paths to the following:

#Imports
#-------
import clr
import sys
sys.path.append(r'C:\Users\user\Documents\env\Lib\site-packages')
sys.path.append(r"C:\Users\user\Documents\env\Lib\site-packages\win32")
sys.path.append(r"C:\Users\user\Documents\env\Lib\site-packages\win32\lib")
sys.path.append(r"C:\Users\user\Documents\env\Lib\site-packages\win32ctypes\pywin32")
sys.path.append(r"C:\Users\user\Documents\env\Lib\site-packages\win32ctypes")

import win32com.client

And, I still run into the same problem.

@mhammond
Copy link
Owner

DLL load failed while importing _win32sysloader: ... "\lib\pywintypes.py", line 50, in import_pywin32_system_module\n import _win32sysloader

This means Windows failed to load _win32sysloader.pyd due to a missing import - most likely pythonXX.dll (which seems odd). It might be that your PATH (not sys.path) needs to have Python on it.

sys.path.append(r"C:\Users\user\Documents\env\Lib\site-packages\win32")
sys.path.append(r"C:\Users\user\Documents\env\Lib\site-packages\win32\lib")
sys.path.append(r"C:\Users\user\Documents\env\Lib\site-packages\win32ctypes\pywin32")

I don't know what win32ctypes is, but the fact it has a pywin32 directory almost certainly means it's designed to not be used with pywin32. You almost certainly do not want 2 different directories each with copies of files from pywin32.

@Avasam
Copy link
Collaborator

Avasam commented Apr 28, 2025

FYI win32ctypes is https://pypi.org/project/pywin32-ctypes/
And yeah, it's meant as a pure-python alternative to pywin32 using cffi and/or ctypes.

@Kuhndog444
Copy link
Author

First of all, thank you all so much for your responses!

I double checked and _win32sysloader.pyd was loaded to the win32 folder. Also, I believe that my system is using the correct pythonXX.dlls because it uploaded it to the correct place, I also double checked the paths just in case.

The 'win32ctypes' error was my mistake, I apologize for that but, it has been removed from my code and it is not in the Venv. That being said, I have started a new Venv on the C: drive named env (I removed all other references I could find related to pywin32). I used python3.13 to create the environment and pip3.13 to install pywin32. I am using Revit 2025, with dynamo's python version 3.9.12 (which I'm not sure if that should make a difference) to run the codes below:

Here's the cmd process:

C:\>python3.13 -m venv env

C:\>env\Scripts\activate

(env) C:\>pip3.13 install pywin32
Collecting pywin32
  Using cached pywin32-310-cp313-cp313-win_amd64.whl.metadata (9.4 kB)
Using cached pywin32-310-cp313-cp313-win_amd64.whl (9.5 MB)
Installing collected packages: pywin32
Successfully installed pywin32-310

[notice] A new release of pip is available: 25.0.1 -> 25.1
[notice] To update, run: python.exe -m pip install --upgrade pip

and the python code with python version 3.9.12:

#Imports
#-------
import clr
import sys
sys.path.append(r'C:\env\Lib\site-packages')
sys.path.append(r"C:\env\Lib\site-packages\win32")
sys.path.append(r"C:\env\Lib\site-packages\win32\lib")

import win32com.client

I believe this cleaned everything up properly but, I am still getting the same error. I'm at a loss because it appears that everything should be working properly.

@geppi
Copy link
Collaborator

geppi commented May 5, 2025

I'm wondering why you need to append these paths at all in your Python code when you run it in the activated venv where you also installed pywin32?

@mhammond
Copy link
Owner

mhammond commented May 5, 2025

I'm wondering why you need to append these paths at all in your Python code when you run it in the activated venv where you also installed pywin32?

Yeah, this seems suspicious - if those paths aren't on sys.path then that might point to the underlying problem. Also, depending on what's been run in the past, it might be worth making sure there are no py*.dll files under your Windows directories (because with the environment you described above, they will not be needed there, but old version could certainly cause problems somewhat like this)

@Avasam
Copy link
Collaborator

Avasam commented May 5, 2025

DLL locations:
installed with pip3.13 into Venv 'newenv' listed in the code above so all DLLs should be there except for the _win32sysloader.dll, however, _win32sysloader.pyd is located under win32 in the site-packaged folder.

Just to make sure we're all on the same track. Could you send the actual locations as printed by python -c "import pywintypes,pythoncom;print(pywintypes.__file__,pythoncom.__file__,sep='\n')" ? (full paths, you can redact out usernames)

@Kuhndog444
Copy link
Author

If it's any consolation, I got it to work by installing python 3.9 and I used that to install it. However, I'm worried because I am going to need to install this firmwide to access the users Microsoft Outlook app and want it to work correctly because I am not sure what they have installed on their machines even though I'm presuming they have the same folder %localappdata%\python-3.9.12-embed-amd64 and they shouldn't have any versions of python installed because we're all using Revit Dynamo to run the scripts. Plus, I am going to need to update it for every version of Revit moving forward.

Just to make sure we're all on the same track. Could you send the actual locations as printed by python -c "import pywintypes,pythoncom;print(pywintypes.file,pythoncom.file,sep='\n')" ? (full paths, you can redact out usernames)

I tried to use the above command and I got the following error in the cmd prompt:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Apps > Advanced app settings > App execution aliases.
FYI, I uninstalled python on my machine until I get this all sorted out (which is probably why I'm receiving this), I don't think I need it as I'm trying to replicate what the rest of the firm has installed until I can fix and distribute it. (I got confused/lost sorting through files on my C: Drive.)

Yeah, this seems suspicious - if those paths aren't on sys.path then that might point to the underlying problem. Also, depending on what's been run in the past, it might be worth making sure there are no py*.dll files under your Windows directories (because with the environment you described above, they will not be needed there, but old version could certainly cause problems somewhat like this)

I also keep checking all the sys.path from the Environment Variables and it just points to the python.exe files in %USERPROFILE%\AppData\Local\Microsoft\WindowsApps (python.exe & python3.exe), the dlls I've found on the sys.path are for other programs, i.e. Visual Studio etc.

FYI, I'm going to attempt to install pywin32 on everyone's machine in their %localappdata%\python-3.9.12-embed-amd64 folder so they can use the package, but I want this to work correctly first.

@Avasam
Copy link
Collaborator

Avasam commented May 8, 2025

I tried to use the above command and I got the following error in the cmd prompt:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Apps > Advanced app settings > App execution aliases.

That means there's no "python" executable currently on your PATH. Replace python with the same command you use to start your Python interpreter (ie: py, the full python path, etc.)


I also keep checking all the sys.path from the Environment Variables and it just points to the python.exe files in %USERPROFILE%\AppData\Local\Microsoft\WindowsApps (python.exe & python3.exe)

There also seems to be some known(-ish) issue with the Microsoft Store installation, refer to #1383

@Kuhndog444
Copy link
Author

Okay, I think I did what you asked and got the following response:

"C:\Users\user\AppData\Local\python-3.9.12-embed-amd64\python" -c "import pywintypes,pythoncom;print(pywintypes.__file__,pythoncom.__file__,sep='\n')" Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'pywintypes'

@Avasam
Copy link
Collaborator

Avasam commented May 8, 2025

Is pywin32 installed? I see you're using an embed install, but that's not quite the same error as #1916 . Looks similar to #1618 (assuming you do have pywin32 installed)

This comment seems especially relevant: #1618 (comment)

The root cause of this issue lies in the fact that if a ._pth file exists, all other .pth files will be ignored. To resolve this, you can rename python39._pth to python39.pth.

@Kuhndog444
Copy link
Author

Okay, I researched the comments above

Is pywin32 installed? I see you're using an embed install, but that's not quite the same error as #1916 . Looks similar to #1618 (assuming you do have pywin32 installed)
This comment seems especially relevant: #1618 (comment)

and think I finally figured it out. Even though this code doesn't install pywin32 into a virtual environment, it installs it locally per user and that should work theoretically/hopefully right? I still need to test it out on a few of my coworkers before I get confirmation though.

Here's my code:

import clr
import os
import shutil

getPipFile = r'\\path\to\downloaded\get-pip.py\file\on\server\get-pip.py'
newPipFile = r'C:\Users\<USERNAME>\AppData\Local\python-3.9.12-embed-amd64\get-pip.py'
pathToPy = r'C:\Users\<USERNAME>\AppData\Local\python-3.9.12-embed-amd64'


if os.path.exists(newPipFile) == False:
    shutil.copy(getPipFile, pathToPy)
    shutil.copy(r'\\path\to\python39._pth\file\with\uncommentated\import site\on\server\python39._pth', pathToPy)
    os.chdir(str(pathToPip))
    os.system('python get-pip.py')
    os.system('python -m pip install --target Lib\site-packages pywin32')

sys.path.append(r"C:\Users\<USERNAME>\AppData\Local\python-3.9.12-embed-amd64\Lib\site-packages")
sys.path.append(r"C:\Users\<USERNAME>\AppData\Local\python-3.9.12-embed-amd64\Lib\site-packages\win32")
sys.path.append(r"C:\Users\<USERNAME>\AppData\Local\python-3.9.12-embed-amd64\Lib\site-packages\win32\lib")

import win32com.client as win32

outlook = win32.Dispatch("Outlook.Application")
Msg = outlook.CreateItem(0)
Msg.Display()

# Assign your output to the OUT variable.
OUT = "YAAY! It Worked!!!"

I'm not sure if it will work for everyone in our firm but, I feel much more confident that it will. I also still need to append the win32 and win32\lib paths because it doesn't find pywintypes or _win32sysoader without appending them however, it seems to be working fine now. Once I test it and add a couple of failsafe checks it should be ready to release to the firm. Do you think it will be okay per user or should I be doing something different?

@Avasam
Copy link
Collaborator

Avasam commented May 10, 2025

Looks like you have to append to your path because pywin32.pth is never loaded because you have a ._pth file.
I honestly don't know what's the best solution here. But depending on your needs, another potential solution is to either try renaming python39._pth to python39.pth, or append the content of pywin32.pth to python39._pth if you must keep it for embed purposes.

pywin32.pth just creates 3 import path aliases:

# .pth file for the PyWin32 extensions
win32  # can do `import winxpgui` instead of `import win32.winxpgui`
win32\lib  # can do `import winerror` instead of `import win32.lib.winerror`
Pythonwin  # can do `import win32ui` instead of `import pythonwin.win32ui`

And 1 dynamic code execution to register dll locations:

# And some hackery to deal with environments where the post_install script
# isn't run.
import pywin32_bootstrap

Which loads this module: https://github.com/mhammond/pywin32/blob/main/win32/Lib/pywin32_bootstrap.py (everytime you import anything)

@Kuhndog444
Copy link
Author

Looks like you have to append to your path because pywin32.pth is never loaded because you have a ._pth file.
I honestly don't know what's the best solution here. But depending on your needs, another potential solution is to either try renaming python39._pth to python39.pth, or append the content of pywin32.pth to python39._pth if you must keep it for embed purposes.
pywin32.pth just creates 3 import path aliases:

I tried all of these suggestions and in different configurations (even tried installing pywin32 directly inside the python-3.9.12-embed-amd64 folder as per your post in #1618 (comment)) and the easiest /best thing for me is just to append the paths like I did previously. I'm not sure if this will work for everyone until I get it tested. The issue seems to be that the python39.pth file doesn't seem to be finding the Lib folder and pywin32.pth file for some reason even when I have appended the site-packages path in the main code

sys.path.append(r"C:\Users<USERNAME>\AppData\Local\python-3.9.12-embed-amd64\Lib\site-packages")

and even when I appended them to the python39.pth file (unless I've been doing it wrong because I can't get it to work correctly) so, I would think it should be able to read the pywin32.pth file when win32com is called but, it doesn't seem to be finding the pywin32.pth file.

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

4 participants