Skip to content
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

ImportError: DLL load failed while importing win32event: The specified module could not be found. #1431

Closed
inhahe opened this issue Oct 23, 2019 · 32 comments

Comments

@inhahe
Copy link

inhahe commented Oct 23, 2019

I just installed Python 3.8.0 32-bit, then installed a few modules including pywin32 using pip.
When I try "import win32event", I get the following error:

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import win32event
Traceback (most recent call last):
File "", line 1, in
ImportError: DLL load failed while importing win32event: The specified module could not be found.

The same happens when I try "import win32com".

Since I found this http://www.xavierdupre.fr/blog/2014-07-01_nojs.html , I tried adding c:\python3.8.0-32\lib\site-packages\pywin32_system32 to my path and then restarting cmd and importing win32event again and still got the same error.

I also tried adding

path = os.path.join(os.path.split(sys.executable)[0], "Lib","site-packages","pywin32_system32")
os.environ["PATH"] = os.environ["PATH"] + ";" + path

before calling qt5reactor.install() #(which is what's trying to load win32event), and that didn't work either.

I also have Python 2.7 and Python 3.6 installed, but I don't seem to to have pywin32 installed on those versions of Python.

I saw that a similar issue had been submitted, but it seems that it was decided that that error was due to some custom installation, and in my case there was no custom installation.

I just tried copying the dll's in c:\python3.8.0-32\lib\site-packages\pywin32_system32 to c:\python3.8.0-32, and now the import statement works.

@ch3nyy
Copy link

ch3nyy commented Oct 26, 2019

Same.
When I use miniconda, create a new conda-env, pip install pywin32 and import win32api, it turn out ImportError: DLL load failed.
And when I use pywin32 version223, it can be import normally
You can try pip install pywin32==223, and try again.

@lmazuel
Copy link

lmazuel commented Oct 29, 2019

Seems to be a Python 3.8 specific problem, I have the same thing:

> python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32file
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing win32file: The specified module could not be found.

@jiasli
Copy link

jiasli commented Oct 31, 2019

pywin32==225 works on Python 3.7, but not 3.8. Got same error while importing win32gui.

@CircuitCraft42
Copy link

Registering pywin32 with the system with py Scripts\pywin32_postinstall.py -install in an Admin command prompt seems to solve this problem, at least for me.

@v-python
Copy link

Hmm. But that doesn't seem to happen automatically when installing. I thought postinstall scripts were supposed to be automatic?

But even if it were automatic, it doesn't provide a method for making a "portable" installation of Python that can be moved from machine to machine, or shared on a cloud-synchronized installation, so a better solution that allowed for portability would be far preferable.

@mhammond
Copy link
Owner

Hmm. But that doesn't seem to happen automatically when installing. I thought postinstall scripts were supposed to be automatic?

It does if you use the installer, but due to limitations with wheels, it doesn't when using them.

But even if it were automatic, it doesn't provide a method for making a "portable" installation of Python that can be moved from machine to machine, or shared on a cloud-synchronized installation, so a better solution that allowed for portability would be far preferable.

pywin32 has features which makes that impossible in the general case, such as implementing COM servers or Windows services. Things "mostly" work pre 3.8, but Python seems to have made changes in that version which mean pywin32 needs to change how it does things.

@v-python
Copy link

Interesting, regarding wheels and postinstall. I thought wheels solved all the world's install problems :(

I can understand that COM servers and Windows services might require registration, but I venture to guess that most users of pywin32 aren't doing that, and that if they are, they'd know the need to register.

Yes 3.8 changed the rules for searching for DLLs on various paths. One has to call os.add_dll_directory. It is described in the 3.8 What's New, and a link to the PEP or something.

Code like this enables access from outside pywin32, but would be better built in to pywin32 somehow...

win32 = 0 pathwin32 = '' for iz in sys.path: if 'site-packages' in iz: pathwin32 = os.path.join( iz, 'pywin32_system32') if os.path.isdir( pathwin32 ): break pathwin32 = '' try: os.add_dll_directory( pathwin32 ) import win32gui import win32con win32 = 1 except: pass # checking for GUI interface module

@v-python
Copy link

I told that to be code, why did it mangle it?

win32 = 0
pathwin32 = ''
for iz in sys.path:
    if 'site-packages' in iz:
        pathwin32 = os.path.join( iz, 'pywin32_system32')
        if os.path.isdir( pathwin32 ):
            break
        pathwin32 = ''
try:
    os.add_dll_directory( pathwin32 )
    import win32gui
    import win32con
    win32 = 1
except:
    pass # checking for GUI interface module

@v-python
Copy link

Plain quoting worked better. I am not a GitHub expert, sorry.

@CircuitCraft42
Copy link

CircuitCraft42 commented Nov 1, 2019

I believe the reason the postinstall doesn't run automatically is because it requires elevated privileges, so it wouldn't work in a venv. It does specify this in the README, however.
During the postinstall, two dll files are copied to System32. I believe that is what fixes the error.

@v-python
Copy link

v-python commented Nov 1, 2019

@CircuitCraft42 You might be right about postinstall in venv, but I installed from a wheel as administrator, not in a venv, and postinstall doesn't run then either.

@CircuitCraft42
Copy link

Yeah, same. I don't know how hard it is to detect privileges and/or selectively run postinstall scripts.

@v-python
Copy link

v-python commented Nov 2, 2019

Personally, I would want postinstall scripts to be able to be optional, for the cases where you don't really need them. And for my uses of pywin32, I don't need registration, I just need to be able to load the DLLs, and I'd prefer to be able to do that without registration, so I can use the parts I use in a portable fashion.

vernondcole pushed a commit to vernondcole/pywin32 that referenced this issue Nov 16, 2019
This directory is now added to the start of PATH for version 3.7 and earlier,
and passed to os.add_dll_directory() on 3.8 and later. This will hopefully
work around problems loading pywintypes.dll in various situations.

Fixes mhammond#1432, fixes mhammond#1431
@Sandeep851
Copy link

pywin32==225 works on Python 3.7, but not 3.8. Got same error while importing win32gui.

For python 3.8.3, pywin32==225 worked for me, the existing pywin32==228 was uninstalled.

@llamafilm
Copy link

How can I make this work in a portable app, e.g. made with PyInstaller? I've tried a few different versions of pywin32 and I always get this error. I'm using Python 3.7 from Windows Store. I also get an error with the postinstall script

python3 .\env\Scripts\pywin32_postinstall.py -install
Parsed arguments are: Namespace(destination='C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.2288.0_x64__qbz5n2kfra8p0\\Lib\\site-packages', install=True, quiet=False, remove=False, silent=False, wait=None)
.\env\Scripts\pywin32_postinstall.py:164: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
Traceback (most recent call last):
  File ".\env\Scripts\pywin32_postinstall.py", line 633, in <module>
    install(args.destination)
  File ".\env\Scripts\pywin32_postinstall.py", line 334, in install
    LoadSystemModule(lib_dir, "pywintypes")
  File ".\env\Scripts\pywin32_postinstall.py", line 174, in LoadSystemModule
    mod = imp.load_dynamic(modname, filename)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2288.0_x64__qbz5n2kfra8p0\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
  File "<frozen importlib._bootstrap>", line 696, in _load
  File "<frozen importlib._bootstrap>", line 670, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 583, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1043, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: DLL load failed: The specified module could not be found.

@mhammond
Copy link
Owner

There are reports of issues with python from the python store - I'm yet to play with it. I suspect it doesn't put the DLLs where pywin32 expects.

@llamafilm
Copy link

I installed the official version instead of the Microsoft store and now it's working.

@yangislet
Copy link

Scripts\pywin32_postinstall.py -install

thanks you a lot ! 64bit of win10, python3.7 it's ok

@BirajCoder
Copy link

pywin32==225 works on Python 3.7, but not 3.8. Got same error while importing win32gui.

For python 3.8.3, pywin32==225 worked for me, the existing pywin32==228 was uninstalled.

This works .. Thanks for the advice you saved me a lot of work .. I am on python 3.8.5

@Twomem1234
Copy link

I tried uninstalling python and acaconda, then reinstalling anaconda + python within anaconda and this worked

@laisbsc
Copy link

laisbsc commented Nov 20, 2020

pywin32==225 works on Python 3.7, but not 3.8. Got same error while importing win32gui.

It worked for me! Using Python 3.8.5 had thee DLL issue on Jupyter Notebook and after installing pywin32==225 all is working fine 😸

@geminixiang
Copy link

pywin32==225 works on Python 3.7, but not 3.8. Got same error while importing win32gui.

For python 3.8.3, pywin32==225 worked for me, the existing pywin32==228 was uninstalled.

Thank you~~
Python 3.8 + jupyter lab + pywin32==225 => Working

facebook-github-bot pushed a commit to facebookresearch/mmf that referenced this issue Feb 1, 2021
Summary:
Fixes Windows build after it was broken due to iopath dependency inclusion. Latest version of pywin is not compatible with python3.8, so we need to fix it at v225. Reference : [issue](mhammond/pywin32#1431).

Pull Request resolved: #758

Reviewed By: ytsheng

Differential Revision: D26171059

Pulled By: vedanuj

fbshipit-source-id: 332281c7987a8ba9cdb4cc3ec38f23476a2af146
@bai-yi-bai
Copy link

bai-yi-bai commented Sep 5, 2021

I had this issue and solved it in a way I had not seen documented anywhere else.
There are many open issues relating to this error message and posts all over the internet.

Python 3.7 does not seem compatible with pywin32==301.

I am coding a windows .bat script to set up a venv. I will edit this post with a link to my project once I release it.

System Configuration

I am using PyCharm 20.3.3 Community Edition as my IDE.
I have three versions of Python installed on my machine:
Python 3.7
Python 3.8.0
Python 3.9.0

C:\where python
C:\Users\myuser\AppData\Local\Programs\Python\Python39\python.exe
C:\Users\myuser\AppData\Local\Microsoft\WindowsApps\python.exe

Also, I have several virtual environments. My project venv created by PyCharm uses 3.9.0
In my requirements.txt I specify "pywin32==301".

My Original .bat Script (Broken)

python3 -m venv
cmd /k "activate && pip install -r ..\..\requirements.txt && python ..\..\do_a_thing.py venv && exit

However, I kept getting the import DLL error. I used winmerge to compare between the PyCharm-created venv and the comamnd-line created venv and found that '37.pyc' files were created instead of '39.pyc'

I added a && python --version after activate to see what version was being installed. Python 3.7!

C:\where python3
C:\Users\myuser\AppData\Local\Microsoft\WindowsApps\python3.exe

C:\>python3 --version
Python 3.7.9

Silly me... why was I using python3 instead of python?

My Fixed .bat Script (Working)

I removed the "3" from the python command. My venv command installed version 3.9 correctly and ran my code without issue.

python -m venv
cmd /k "activate && pip install -r ..\..\requirements.txt && python ..\..\do_a_thing.py venv && exit

Analysis

My python 3.9 installation does not include a "python3.exe".
I don't know when this was dropped. The documentation mentions it:
https://docs.python.org/3/using/windows.html

My guess is that a lot of legacy code/scripts are using the "python3" command instead of "python". This is exactly what I did... I rarely execute python from the Windows command line.

Again,
Python 3.7 does not seem compatible with pywin32==301.

@mhammond
Copy link
Owner

mhammond commented Sep 5, 2021

Python 3.7 does not seem compatible with pywin32==301.

I suspect the problem here will be more like "Windows AppStore installed Python isn't compatible with pywin32" - I'll try and find time to dig into this some more.

@bprasad26
Copy link

pywin32==225 works on Python 3.7, but not 3.8. Got same error while importing win32gui.

For python 3.8.3, pywin32==225 worked for me, the existing pywin32==228 was uninstalled.

This works .. Thanks for the advice you saved me a lot of work .. I am on python 3.8.5

yes, it also solved my problem. Thanks for the help.

@aliwaqas333
Copy link

on windows10 python 3.8, use pip install --upgrade pywin32==225

This worked for me.

@AkitotheExiled
Copy link

Getting the same issue on the latest Python version. Anyone else have any solutions? The above haven't worked for me

@mhammond
Copy link
Owner

Getting the same issue on the latest Python version.

There's a chance that the release version of python 3.11 has issues, so try 3.10 until I get a new build out (which I'm slowly working on but is turning out to be non-trivial)

@AkitotheExiled
Copy link

@mhammond Sorry. I meant the latest public release(3.10.5). I figured the pre-release would have issues. Is there a confirmed working version of Python or a working solution? Also, are you having to do this for every release as this issue as been here since 2019 and was previously closed

@mhammond
Copy link
Owner

Every example of this that I have seen has been explained by duplicate installs of pywin32 such that there are multiple copies of the DLLs - have you seen https://github.com/mhammond/pywin32/blob/main/README.md#the-specified-procedure-could-not-be-found--entry-point-not-found-errors?

@AkitotheExiled
Copy link

AkitotheExiled commented Jun 30, 2022

@mhammond So, the post_install script is installing pythoncom37 and pywintypes37 instead of 310 or whatever the latest version is. It gets put into SysWOW64. I've been dragging it into my venv\site-packages but I'm now using python 3.10.5. Should I uninstall 3.7?

Edit: Uninstalled older versions. Now getting correct 310 dlls. Need to move them now...

edit2: Moved the .dls to two locations. The first being Appdata/.../site-packages/win32 and the second being venv...\win32. The program still gives me win32api module does not have getCursorPos() and pycharm gives me an error that win32api module doesn't exist. It finds win32con though

@mhammond
Copy link
Owner

the post_install script is installing pythoncom37 and pywintypes37 instead of 310

That must have been because you used python 3.7 to run the post-install script.

Re your other issues: win32api.__file__ will tell you what win32api is being used, or if your environment can't find win32api, then your installation is messed up - sorry, but I can't diagnose that from here and pywin32 is using standard the python installation processes.

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