79

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
Glxce
  • 901
  • 1
  • 5
  • 5

32 Answers32

102

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
iGian
  • 11,023
  • 3
  • 21
  • 36
Janzaib M Baloch
  • 1,305
  • 2
  • 5
  • 8
  • 4
    Thanks, this solved the issue for me. I'm using miniconda. – Johannes Schmidt Apr 12 '21 at 13:52
  • Worked for me on Win10, Python 3.9 and Miniforge. – Ismael EL ATIFI May 16 '21 at 13:04
  • I had to install it within a given conda environment (rather than in one I run jupyter notebook from). Nonetheless, it works! – Piotr Migdal Jul 23 '21 at 15:36
  • 4
    We shall hold a festival in your name! Win 10 64-bit. Hours wasted... but thanks to this, issue solved. conda install pywin32 in my activated env didn't affect system32 and it avoided the need to run the postinstall script which failed because wintypes DLL was not found. It shouldn't be possible to get into this ridiculous state (I don't know how it happened) and it should not take this much work to get out. But thank you thank you thank you. – Julian Moore Jul 30 '21 at 14:22
63

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.

Gjanetta
  • 37
  • 8
Bench Wang
  • 792
  • 1
  • 5
  • 3
  • 1
    where is this ```Lib\site-packages\pywin32_system32``` path? – Sabito stands with Ukraine May 15 '20 at 07:01
  • It worked for my issue. I use Pyhon3.8 either. I want to know why it happens? – Lynn Han May 22 '20 at 01:56
  • Same issue under Python 3.8. Solution works, but I had to take the files under my virtual environment `Lib` folder – Guido Jun 18 '20 at 12:45
  • This one actually helped me. I guess the problem is with Python 3.8 version. – rushikesh.meharwade Jul 12 '20 at 14:21
  • This helped me for 3.7 also. – Robse Jul 24 '20 at 09:45
  • 3
    This information helped, but putting 3rd party stuff in C:\Windows\System32 is a recipe for future trouble. Think for instance what will happen if pywin32 updates the DLL and you don't copy them again to C:\Windows\System32 after a pip update. You will get an inconsistent system. In addition, all programs of the computer will "see" these DLLs. You should rather set the PATH as/where required. – ocroquette Jul 26 '20 at 16:43
  • Worked perfectly for jupyter notebook as well. Thanks. – Shravya Mutyapu Jul 16 '21 at 09:42
  • At an admin command prompt, go to python's `Scripts` dir and run `python -B pywin32_postinstall.py -install` BUT if you have multiple `venv`'s, they better have the same version of _pywin32_ or the _Import Error_ will come back when you switch (`pip list` to check). See [this answer](https://stackoverflow.com/a/63361503/648689). – JimB Sep 25 '21 at 16:08
  • None of the suggested solutions solved this issue for me, but yours solved it for me. Thanks heaps. – Arash Apr 12 '22 at 01:48
45

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
Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
Aditya Saini
  • 675
  • 6
  • 14
  • 2
    I get this error under Python3.8: Could not find a version that satisfies the requirement pywin32==224 (from versions: 225, 226, 227, 228) – Guido Jun 18 '20 at 12:34
  • 26
    For python 3.8, if you cannt find pywin32==224, try pywin32==225. It worked for me. – Abhimanu Kumar Jul 04 '20 at 01:11
  • @Guido You must activate the base environment ```conda activate base``` – Cédric Dromzée Jul 22 '20 at 20:47
  • 11
    You must not activate the base env, only the env that you want to use. Downgrading from 228 to 225 worked for me. ```pip install --upgrade pywin32==225 worked``` – Johannes Schmidt Oct 21 '20 at 08:16
  • 5
    `pip install pywin32==225` also works for Python 3.9 – swimmer Jun 01 '21 at 17:47
  • Thanks, `pip install pywin32==225` worked for me on Windows, Python 3.8 – Oscar Chen Aug 24 '21 at 00:27
  • 3
    As of November 2021, `pip install --upgrade pywin32==300` works, pwin32 versions 301, 302 fail! [SO community members should get paid for their QA services :)] – Nir Nov 18 '21 at 19:27
  • In general for python 3.8, you can use: `pip install pywin32` – George Ogden Dec 30 '21 at 12:33
  • pip install pywin32==228 works perfectly for me – Laurent T Feb 11 '22 at 11:33
  • I had to pip install pywin32==225 (python 3.9) AND copy the 2 DLLs from my Python install directory to C:\Windows\System32. DLLs were version 37, and just copying them was not enough. First step upgraded the DLLs to 39, but was not enough, doing both solved this issue. – GISGe Nov 15 '22 at 15:20
  • So, this works, but will downgrading pywin32 be necessary every time I upgrade Python? My script used to work before the upgrade. – GISGe Nov 15 '22 at 15:23
39

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)

José
  • 1,774
  • 1
  • 17
  • 21
  • 7
    To run pywin32_postinstall.py, you need to `python pywin32_postinstall.py -install`, else you'll get an importerror about winreg – gaborous May 17 '20 at 07:51
  • 1
    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). – JoyfulPanda Nov 12 '20 at 17:31
  • 22
    pip install --upgrade pywin32==225 worked for me..Thx – Binu Nov 27 '20 at 11:13
  • 2
    @Binu solution worked for me too as of 2021-06-04 – wtfzambo Jun 04 '21 at 15:07
13

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"]
ocroquette
  • 3,049
  • 1
  • 23
  • 26
  • 2
    I have added absolute path of 'pywin32_system32' to the os.environ['PATH'] and still can't import win32api. Only copying the two dlls to the system32 could work. – pansila Nov 17 '20 at 03:11
  • 2
    Maybe because of this change in 3.8: DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory() are searched for load-time dependencies. Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution. – ocroquette Nov 17 '20 at 07:09
  • Thanks for the background. In addition, I had to downgrade to version 224. – tripleee Sep 17 '21 at 10:23
  • C:\ProgramData\Anaconda3>py Scripts\pywin32_postinstall.py -install ...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. – user3761555 Apr 05 '22 at 00:59
13

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

vinodhraj
  • 177
  • 1
  • 7
11

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!

Michael Tamillow
  • 415
  • 4
  • 11
  • 2
    I think this is the most orthodox solution amongst all available solutions. It works because extra `pywin32` extensions need to be installed. – Kris Stern Sep 01 '21 at 05:35
  • [link](https://github.com/jupyter/notebook/issues/4980#issuecomment-600992296) to the answer on the [issue](https://github.com/jupyter/notebook/issues/4980) – Ian Thompson Jun 16 '22 at 14:34
  • This solution worked for me because pywin32 needed extra extensions to be installed. – Muhammad Arsalan Hassan Jan 13 '23 at 10:36
8

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
suhas sasetty
  • 81
  • 1
  • 2
  • 1
    for me a similar approach worked. I downgraded from 227 to 226. also a newer version like 301 did not workout for me. – jodá Jul 13 '21 at 09:35
5

What helped me was

  1. installing relevant binary from github.com/mhammond/pywin32
  2. executing the following commands in the x64 command line:

cd C:\ProgramData\Anaconda3\Scripts

python pywin32_postinstall.py -install

Anatoly Alekseev
  • 2,011
  • 24
  • 27
4

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

Alberto
  • 77
  • 8
3

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

pip uninstall pypiwin32
pip install pywin32
phd
  • 82,685
  • 13
  • 120
  • 165
  • 1
    thank you but unluckily nothing changes.. here's what i did `Successfully uninstalled pypiwin32-223 C:\Users\gfroz\AppData\Local\Programs\Python\Python38-32\Scripts>pip install pywin32 Requirement already satisfied: pywin32 in c:\users\gfroz\appdata\local\programs\python\python38-32\lib\site-packages (225)` **and then** `>>> import win32api Traceback (most recent call last): File "", line 1, in ImportError: DLL load failed while importing win32api: Impossibile trovare il modulo specificato. ` – Glxce Oct 29 '19 at 22:33
  • Try to reinstall it; after all your experiments with different packages the DLLs could be broken: `pip install --ignore-installed pywin32` – phd Oct 29 '19 at 23:40
  • 1
    thank you so much! actually it didn't work first when i just installed pywin32 but then i wrote in the scripts folder `pywin32_postinstall.py -install`, then i tried `import win32api` and no error appear! – Glxce Oct 30 '19 at 14:05
  • All pywin32_postinstall.py doing is copying pywintypes[pyton version].dll and pythoncom[pyver].dll from \Lib\site-packages\pywin32_system32\ into main dir. Actually better to just manually copy to \Library\bin. Then it all works. – vlad Oct 18 '20 at 02:56
3

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.

TheHai
  • 31
  • 2
  • I had to first downgrade pywin32 (created the correct version of the DLL - in my case 39 - THEN copy it to C:\Windows\System32. – GISGe Nov 15 '22 at 15:26
3

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
addohm
  • 2,248
  • 3
  • 14
  • 40
3

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.

user3761555
  • 851
  • 10
  • 21
2

version 228 works best for me in Windows 10

pip uninstall pywin32
pip install pywin32==228
Sergey Shubin
  • 3,040
  • 4
  • 24
  • 36
1

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
Vikram
  • 332
  • 3
  • 16
1

Maybe a bit late but it could help:

What happens is that you have two versions of pywin32 installed (One in the local enviroment and the other one in the main python installation)

To solve that uninstall both versions with:

conda uninstall pywin32
pip uninstall pywin32

And inside your virtual conda enviroment also execute:

conda uninstall pywin32
or 
pip uninstall pywin32

finally in your virtual enviroment / conda enviroment install with pip install pywin32 or conda.

The answers telling that it works with x version is because the version they install are compatible with the pywin32 dlls they have in system32.

Adding a support link: "DLL load failed: The specified procedure could not be found" when importing win32api or win32com

david.t_92
  • 1,971
  • 1
  • 11
  • 15
0

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

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Aastha
  • 1
  • 1
0

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

0

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!

0

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

MaxSM
  • 21
  • 3
0

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.

Vojtech Stas
  • 631
  • 8
  • 22
0

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
gourxb
  • 325
  • 1
  • 5
  • 13
0

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.

Nguhyw
  • 1
0

You should go into the folder {python folder path}/Lib/site-packages/pywin32_system32 and copy pythoncomXX.dll and pywintypesXX.dll to the folder C:/Windows/System32.

If you are using a virtual environment, then {python folder path} is the python folder used by the virtual environment, otherwise it is the folder where the global python is located. For example, I use conda to create a virtual environment called Frameless-Window, and install the package pywin32 in this virtual environment, then the {python folder path} on my computer should be D:/Anaconda/envs/Frameless-Window.

You should be very careful when copying the dll to the System32 folder. if there are dlls with the same name in the folder and pywin32 in your other virtual environment may use these two dlls, replacing the original dlls may cause this virtual environment had the same ImportError problem. After testing, I found that the dlls of pywin32 of version 227, 228 and 300 can be replaced with each other, and the dlls of pywin32 of versions 301, 302, 303 and 304 can also be replaced with each other, but if the dll of version 300 is replaced with the dll of version 301, it will raise ImportError.

zhiyi
  • 111
  • 1
  • 9
0

If you are launching a virtual env from another virutal env. Makes sure to install pywin32 in both envs. Lost way too many time with this....

Greg7000
  • 297
  • 3
  • 15
0

This worked for me conda install -c anaconda pywin32

King Chudi
  • 61
  • 2
0

Its about venv or virtualenv version problem about pywin32 check version of both of version of pywin32 (pip install pywin32)

than run this command on active venv

pip install --upgrade pywin32==305
0

I had the same issue after reinstalling anaconda 3.9 instead of 3.7 and placing inside the former env folder.

I launched the Anaconda Prompt application then typed in

cd C:\Users\%USERNAME%\anaconda3\Scripts
activate.bat

I suspect it updated the pythoncomXX.dll and pywintypesXX.dll used by anaconda.

Lokinou
  • 1,039
  • 10
  • 13
0

I managed to copy files from "yourPython"/Lib/site-packages/pywin32_system32, pywintypes310.dll and pythoncom310.dll to the app folder with the exe /win32 and in the app folder with the exe, I also copied the pywintypes311.dll file generated by pyinstaller in the app folder with the exe /win32 to System32

-1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
-1

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