How to fix "ImportError: DLL load failed" while importing win32api

PythonPython 3.xWinapiPipPywin32

Python Problem Overview


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 ^^

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

For my case, install and reinstall pywin32 doesn't help. After copied the two files from [installation directory of Anaconda]\Lib\site-packages\pywin32_system32 to C:\Windows\System32, it works.

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

Solution 3 - Python

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

Solution 4 - Python

Run Scripts\pywin32_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:\WINDOWS\system32, 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)

Solution 5 - Python

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

Solution 6 - Python

This happens when Lib\site-packages\pywin32_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:\...\Lib\site-packages\pywin32_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"]

Solution 7 - Python

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

So try this

pip install pywin32==225

Hope it solves your problem

Solution 8 - Python

According to pywin32 github you must run

pip install pywin32

and after that, you must run

python path\to\python\Scripts\pywin32_postinstall.py -install

taken from here. worked for me!

Solution 9 - Python

As of February 2022, downgrading tot version 303 of pywin32 solves the issue.

Check your pywin32 version:

pip show pywin32

Downgrade to version 300:

pip install pywin32==300 --upgrade

Restart Jupyter

Solution 10 - Python

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

pip uninstall pypiwin32
pip install pywin32

Solution 11 - Python

Windows 10, Python 3.8, PyWin32 v.302 using Anaconda

Here is what worked for me

Open an elevated command prompt activate environment

  • Windows Key
  • Type cmd
  • Right click Command Prompt and click Run as Administrator
  • conda activate [ENVIRONMENT]

Navigate to the environment you installed PyWin32 on, works if pip install or conda install is used

  • cd C:\Users\[USER]\anaconda3\envs\[ENVIRONMENT]\Scripts

Run the post install script that was added when installing PyWin32

  • python pywin32_postinstall.py -install

Solution 12 - Python

Yes it works!

C:\ProgramData\Anaconda3>py Scripts\pywin32_postinstall.py -install

Parsed arguments are: Namespace(destination='C:\ProgramData\Anaconda3\Lib\site-packages', install=True, quiet=False, remove=False, silent=False, wait=None) Copied pythoncom38.dll to C:\WINDOWS\system32\pythoncom38.dll Copied pywintypes38.dll to C:\WINDOWS\system32\pywintypes38.dll Registered: Python.Interpreter Registered: Python.Dictionary Registered: Python -> Software\Python\PythonCore\3.8\Help[None]=None -> Software\Python\PythonCore\3.8\Help\Pythonwin Reference[None]='C:\ProgramData\Anaconda3\Lib\site-packages\PyWin32.chm' Registered help file Pythonwin has been registered in context menu Creating directory C:\ProgramData\Anaconda3\Lib\site-packages\win32com\gen_py Can't install shortcuts - 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.8' is not a folder The pywin32 extensions were successfully installed.

Solution 13 - Python

Jupyter notebook github has the issue mentioned in the question. There are multiple solution proposed.

What worked for me was this answer with additional first step:

  1. pip uninstall pywin32
  2. pip install pywin32
  3. python [environment path]/Scripts/pywin32_postinstall.py -install

Solution 14 - Python

version 228 works best for me in Windows 10

pip uninstall pywin32
pip install pywin32==228

Solution 15 - Python

You should try some (or all) of my methods:

  1. Run terminal and use this command: conda install pywin32.

  2. Copying the two files from [installation directory of Anaconda]\Lib\site-packages\pywin32_system32 (there are only 2 files in this folder) and paste to C:\Windows\System32.

    In my case, the two files are pythoncom38.dll and pywintypes38.dll (it means my Python version is 3.8).

  3. Downgrading the version of pywin32 to 225 or lower by this command: pip install pywin32==225.

Solution 16 - Python

In the referecne to this comment question

> where is this Lib\site-packages\pywin32_system32 path?

Go to C directory, Users , your username , anaconda3 ,Lib, site-packages,pywin32_system32. you can find easily.

C:\Users\HP\anaconda3\Lib\site-packages\pywin32_system32

Solution 17 - Python

Currently there are two copies of the pythoncom*.dll files in directories. Pycharm is using the copy in directory C:\Windows\System32:-

C:\Windows\System32 C:\Users\sharandi\AppData\Local\Programs\Python\Python38\Lib\site-packages\pywin32_system32

The files are: - pythoncom38.dll - 559 KB pywintypes38.dll - 138 KB

Solution 18 - Python

I downloaded and installed the latest Microsoft Visual C++ 2015-2019 package from (https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads). You may have to restart your machine after installation.

Then from conda command prompt activate your virtual environment and go to the Scripts folder of the virtual environment(>cd "path of venv"/Scripts)

Once you are in the Scripts folder run the following command "python pywin32_postinstall.py -install"

This will install the required dll's to the appropriate folders in the virtual environment.

And that's how I got it to work!

Solution 19 - Python

I have had this issue with Jupyter in Anaconda. After following all listed advices, without clear understanding what I am doing, nothing worked for me except one thing. I have updated indexes of Anaconda environments and I've got my kernels back. The screenshot

Solution 20 - Python

This problem for me occured when recreating venv from requirements.txt. This venv was initially created on Ubuntu Linux and then recreated on Win 10.

I had to clear the environment and install everyrhing again manually. Did the work for me.

Solution 21 - Python

Solved

I tried uninstalling and reinstalling pywin32 multiple ways, but the problem persisted. The following way solved the problem:

pip install --upgrade jupyter_client    
pip install --upgrade pywin32==224 --force-reinstall

Solution 22 - Python

hi this question i'm solve as below: 1.check directory C:\Windows\System32, is exist these file? pythoncom37.dll pywintypes37.dll or pythoncom36.dll pywintypes36.dll the number is python version .

  1. if the file is exist delete it.

and then this issue will be solve.

Solution 23 - Python

That is Simple. After Many Tries, I Discovered a Solution. Just Type the Following Commands in Windows 10. I am Using Pycharm, Python Version 3.9

Note:- This Works for All Python Versions !!

pip uninstall pywin32

pip install pywin32

That is Simple.

if That Does Not Work, Then Try:-

pip uninstall win32api
pip install win32api

After Trying These Commands, Try the first 2 commands again.

If It is Still Not Coming, Then Comment This Question What the problem is !!

Solution 24 - Python

I am a miniconda user. I got this error first after installed some python environment then deleted it. So I reinstalled the jupyter notebook and it replaced some missing files and issue is fixed.

conda install jupyter notebook

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlxceView Question on Stackoverflow
Solution 1 - PythonJanzaib M BalochView Answer on Stackoverflow
Solution 2 - PythonBench WangView Answer on Stackoverflow
Solution 3 - PythonAditya SainiView Answer on Stackoverflow
Solution 4 - PythonJoséView Answer on Stackoverflow
Solution 5 - PythonvinodhrajView Answer on Stackoverflow
Solution 6 - PythonocroquetteView Answer on Stackoverflow
Solution 7 - Pythonsuhas sasettyView Answer on Stackoverflow
Solution 8 - PythonMichael TamillowView Answer on Stackoverflow
Solution 9 - PythonAlbertoView Answer on Stackoverflow
Solution 10 - PythonphdView Answer on Stackoverflow
Solution 11 - PythonaddohmView Answer on Stackoverflow
Solution 12 - Pythonuser3761555View Answer on Stackoverflow
Solution 13 - PythonVikramView Answer on Stackoverflow
Solution 14 - PythonShivaji fullstackView Answer on Stackoverflow
Solution 15 - PythonTheHaiView Answer on Stackoverflow
Solution 16 - PythonAasthaView Answer on Stackoverflow
Solution 17 - PythonDilip SharanView Answer on Stackoverflow
Solution 18 - PythonRaghunath ChandranView Answer on Stackoverflow
Solution 19 - PythonMaxSMView Answer on Stackoverflow
Solution 20 - PythonVojtech StasView Answer on Stackoverflow
Solution 21 - PythongourxbView Answer on Stackoverflow
Solution 22 - PythonNguhywView Answer on Stackoverflow
Solution 23 - PythonHacker--Rohan RajView Answer on Stackoverflow
Solution 24 - PythonBerkanKRNView Answer on Stackoverflow