How to fix “ImportError: DLL load failed” while importing win32api

Posted on

Question :

How to fix “ImportError: DLL load failed” while importing win32api

I’m setting up an autoclicker in Python 3.8 and I need win32api for GetAsyncKeyState but it always gives me this error:

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

I’m on Windows 10 Home 64x. I’ve already tried

pip install pypiwin32

and it successfully installs but nothing changes. I tried uninstalling and re-installing python as well. I also tried installing ‘django’ in the same way and it actually works when I import django, so i think it’s a win32api issue only.

>>> import win32api

I expect the output to be none, but the actual output is always that error ^^

Asked By: Glxce

||

Answer #1:

Run Scriptspywin32_postinstall.py -install in an Admin command prompt

ref: https://github.com/mhammond/pywin32/issues/1431

edit: User @JoyfulPanda gave a warning:

Running this script with admin rights will also copy pythoncom37.dll, pywintypes37.dll (corresponding to the pywin32 version), into C:WINDOWSsystem32, which effectively overwrites the corresponding DLL versions from Anaconda already there. This later causes problem when openning (on Windows) “Start Menu > Anaconda3 (64-bit) > Anaconda Prompt (a_virtual_env_name)”. At least Anaconda 2019.07 has pywin32 223 installed by default. Pywin32 224 may work, but 225-228 causes problem for Anaconda (2019.07)

Answered By: José

Answer #2:

For my case, install and reinstall pywin32 doesn’t help. After copied the two files from [installation directory of Anaconda]Libsite-packagespywin32_system32 to C:WindowsSystem32, it works.

My environment is python 3.8 in miniconda. The two files are pythoncom38.dll and pywintypes38.dll.

Answered By: Bench Wang

Answer #3:

Solved

If you are working in a miniconda on conda environment. You could just install pywin32 using conda instead of pip.

This solved my problem

conda install pywin32

Answered By: Janzaib Masood

Answer #4:

For me, it worked by downgrading my pywin32 from version 227 to version 224. Just type the following command on any shell in administrator mode:

pip install --upgrade pywin32==224

Answered By: Aditya Saini

Answer #5:

This happens when Libsite-packagespywin32_system32 is not in the list of directories to search for DLL (PATH environment variable).

pywin32 (or one of its dependencies) adds this path at runtime to the PATH variable. If this is failing, or another component is overriding the PATH after it’s been set by pywin32, you will get the given error (ImportError: DLL load failed while importing win32api).

You can try to extend the PATH variable in the shell before starting Python.

On Windows:

set PATH=c:...Libsite-packagespywin32_system32;%PATH%

On Unix like systems:

export PATH=c:...Libsite-packagespywin32_system32:$PATH

If that doesn’t work, then the PATH maybe overridden within the Python program at runtime. Add the following line to your program just before pywin32 is used to verify its value:

import os
print(os.environ["PATH"])

As a last resort, you can extend the PATH variable before pywin32 is loaded:

Windows:

os.environ["PATH"] = r"c:...pywin32_system32;" + os.environ["PATH"]

Unix like:

os.environ["PATH"] = r"/.../pywin32_system32:" + os.environ["PATH"]
Answered By: ocroquette

Answer #6:

The answer is in jupyter notebook github.
https://github.com/jupyter/notebook/issues/4980

conda install pywin32 worked for me. I am using conda distribution and my virtual env is using Python 3.8

Answered By: vinodhraj

Answer #7:

According to pywin32 github you must run

pip install pywin32

and after that, you must run

python pathtopythonScriptspywin32_postinstall.py -install

taken from here. worked for me!

Answered By: Michael Tamillow

Answer #8:

pypiwin32 is an outdated distribution. Uninstall it and install pywin32:

pip uninstall pypiwin32
pip install pywin32
Answered By: phd

Leave a Reply

Your email address will not be published.