159

I am using Python 2.7 and I want to use pywin32-214 on Windows 7. I installed pywin32-214 by using the MSI installer. But when I import win32api in my Python script, it throws the error:

no module named win32api

What should I do? How can I use win32api on Windows 7?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
akshay
  • 1,641
  • 2
  • 11
  • 7
  • Do you have multiple versions of Python installed? If so are you sure you are trying to import from the same one that you installed it to? – Claudiu Jan 24 '14 at 22:47
  • It's possible that the path msi installer installed to is different from the path you're trying to import from. – aidnani8 Jan 24 '14 at 23:41
  • Claudiu: no, i have only installed python 2.7 on my pc – akshay Jan 25 '14 at 23:46
  • user3193087: Actually, msi installer installed files in to my /python27/... subdirectory. you can import any files which are installed in the subdirectories. Am I right?? – akshay Jan 25 '14 at 23:49
  • I'm getting this when trying to run a Jupyter notebook in PyCharm. It work fine before, not sure what happened... – stefanbschneider Dec 09 '20 at 07:14

16 Answers16

279

This is resolve my case as found on Where to find the win32api module for Python?

pip install pypiwin32
Community
  • 1
  • 1
Ciwidey Developer
  • 2,815
  • 1
  • 10
  • 3
  • I am using python2.7 and pypiwin32 has been successfully installed – syam Jun 08 '17 at 00:01
  • 3
    This only worked for me AFTER I restarted Spyder. Most times new packages work immediately after being installed, but not in this case. – Sean McCarthy Jun 29 '18 at 17:46
  • Worked in March 2020 for Python 3.7.5 ! Not sure why this even lingers on though.. – endless Mar 15 '20 at 18:29
  • i used it with python2.7 and I get the error "ERROR: Could not find a version that satisfies the requirement pywin32>=223 (from pypiwin32) (from versions: none) " – Avin Mathew Jun 24 '20 at 05:10
  • Don't do this. [\[SO\]: ImportError: No module named win32com.client (@CristiFati's answer)](https://stackoverflow.com/a/75310161/4788546). – CristiFati Apr 20 '23 at 19:08
55

According to pywin32 github you must run

    pip install pywin32

and after that, you must run

    python Scripts/pywin32_postinstall.py -install

I know I'm reviving an old thread, but I just had this problem and this was the only way to solve it.

Juano
  • 756
  • 6
  • 6
  • 1
    This worked for me. I use pipenv in my project. Had to run the post_install.py in my env folder. – poWar Sep 25 '19 at 06:50
  • I'm hitting some issues with this right now as well. Is there any insight as to why it wouldn't run? i am getting this error. Traceback (most recent call last): File "C:\Users\tws07yi\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\__init__.py", line 32, in SetupEnvironment key = win32api.RegOpenKey(HKEY_LOCAL_MACHINE , keyName, 0, KEY_QUERY_VALUE) AttributeError: module 'win32api' has no attribute 'RegOpenKey' – twseewx Apr 01 '20 at 18:59
  • 1
    @twseewx try running it as admin. – Juano Apr 01 '20 at 20:42
  • @Juano stame result as last time. – twseewx Apr 02 '20 at 00:19
  • @twseewx try specifying a different version of pywin32 when installing. I just installed it again and for me is working. Maybe your download was corrupted, or you may try updating your python version. – Juano Apr 02 '20 at 00:41
  • @Juano i just uninstalled it, using pip, and reinstalled it. Still the same error as above. I am using Python 3.7.7. (64bit) I downloaded pywin32-227 I am on Windows 10x64 (could my 64 bit be the issue?) – twseewx Apr 02 '20 at 12:38
  • 1
    @Juano I found this page here, https://github.com/mhammond/pywin32/releases, and will be re-downloaded and installing from binary with the AMD 64bit release for Python 3.7. – twseewx Apr 02 '20 at 12:49
  • @twseewx check your bitness. Maybe it should match your system's and I'm not sure if the default install you get is 32-bit. – Juano Apr 03 '20 at 14:52
  • 1
    @Juano i installed it using the a .whl file and pip after a fresh install. as of now it works, but i need to do that for all of my packages ti li can test it. pip install 'package' is blocked by my works firewall it seems.... – twseewx Apr 07 '20 at 18:48
  • This was what I had to do. I think this should be marked as the right answer. – Miguel Rentes Dec 02 '20 at 10:25
  • @Juano Thank you, this worked for me in PyCharm. Opened the project terminal(button at the bottom of the window) and ran the post install script. – dmb Jul 07 '23 at 16:06
50

I had an identical problem, which I solved by restarting my Python editor and shell. I had installed pywin32 but the new modules were not picked up until the restarts.

If you've already done that, do a search in your Python installation for win32api and you should find win32api.pyd under ${PYTHON_HOME}\Lib\site-packages\win32.

Nouman
  • 6,947
  • 7
  • 32
  • 60
Erica Kane
  • 3,137
  • 26
  • 36
18

I didn't find the package of the most voted answer in my Python 3 dist.

I had the same problem and solved it installing the module pywin32:

In a normal python:

pip install pywin32

In anaconda:

conda install pywin32

My python installation (Intel® Distribution for Python) had some kind of dependency problem and was giving this error. After installing this module it stopped appearing.

neves
  • 33,186
  • 27
  • 159
  • 192
4

I had both pywin32 and pipywin32 installed like suggested in previous answer, but I still did not have a folder ${PYTHON_HOME}\Lib\site-packages\win32. This always lead to errors when trying import win32api.

The simple solution was to uninstall both packages and reinstall pywin32:

pip uninstall pipywin32
pip uninstall pywin32
pip install pywin32

Then restart Python (and Jupyter). Now, the win32 folder is there and the import works fine. Problem solved.

stefanbschneider
  • 5,460
  • 8
  • 50
  • 88
  • 1
    Uninstalling pywin32 and installing again did the trick for me. Thanks a lot. Very grateful ! – Laurent T Aug 04 '22 at 06:35
  • 1
    After restarting this really worked thanks – Toms Nov 02 '22 at 21:31
  • I have tried uninstalling and restarting. Have tried all other solutions mentioned in this post - used PIP3, ran "pywin32_postinstall.py", did force-installation, and ran the command "pip install pywin32==225". Nothing seem to be working. It is not recognizing pywin32. I keep getting the error "No module named 'win32service' ". I am using Python3.9 and running it on Windows11. I want to build a Windows service it it keeps failing at the declaration section itself. Appreciate any help I would get in addressing this. – Ashwin Kumar May 08 '23 at 13:36
3

The following should work:

pip install pywin32

But it didn't for me. I fixed this by downloading and installing the exe from here:

https://github.com/mhammond/pywin32/releases

Samuel
  • 8,063
  • 8
  • 45
  • 41
2

After installing pywin32

Steps to correctly install your module (pywin32)

  1. First search where is your python pip is present

    1a. For Example in my case location of pip - C:\Users\username\AppData\Local\Programs\Python\Python36-32\Scripts

  2. Then open your command prompt and change directory to your pip folder location.

    cd C:\Users\username\AppData\Local\Programs\Python\Python36-32\Scripts
    
    C:\Users\username\AppData\Local\Programs\Python\Python36-32\Scripts>pip install 
    pypiwin32
    

Restart your IDE

All done now you can use the module .

sameer_nubia
  • 721
  • 8
  • 8
2

This line: import win32com
got me the error no module named win32api.

Using this command in elevated terminal: pip install pywin32-ctypes and pip install pywin32 and based on the error displayed, replacing:
import win32apifrom win32ctypes.pywin32 import win32api
import pywintypesfrom win32.lib import pywintypes
import _win32sysloaderfrom win32 import _win32sysloader
in your source file, or even the files of the packages that report the error (know what you are doing if you choose this approach) may solve this error. But better would be to just add the corresponding directories into the python path variable, for better integration with the python loading system, more info here: https://realpython.com/python-import/

So I put this content:

python38.zip
.
./lib
./lib/site-packages
./lib/site-packages/win32
./lib/site-packages/win32/lib
./lib/site-packages/win32ctypes/pywin32
./lib/site-packages/win32ctypes


# Uncomment to run site.main() automatically
#import site

(order DOES matter) into this file: <python_root_installation_directory>/python38._pth That way, correct libraries load when standard imports are used. If there is a cache import somewhere in the library, it will work, and the imports inside the libraries work as well.

This works for me and my installation, so your environment may be set up differently and this guide may not be fully compatible, but it is a good step in solving the issue, maybe modification or extension of my steps above may lead to the solution in another distribution.

Patrik Staron
  • 331
  • 1
  • 9
1

Try this, it worked for me, it may help you!

 pip install pywin32==225
Alama1
  • 131
  • 2
1

I've tried all of your answers and finally got a solution. my issue was that I installed from both pip and python interpreter on my Pycharm IDE. I just removed win32compact from my interpreter and it works.

  • Does this post contribute any new insight? Then please [edit] to make that more obvious. Otherwise, if it only is meant as a "thanks", please delete this. – Yunnosch Sep 25 '22 at 19:15
1

let me summarize, correct me if wrong, as below:

# update to newest pywin32
python -m pip install -U pywin32 pypiwin32

# run the post-install @ref https://stackoverflow.com/questions/21343774/importerror-no-module-named-win32api
python %CONDA_PREFIX%\Scripts\pywin32_postinstall.py -install

# double check
python -c "print( __import__('win32api') )"
mgttt
  • 351
  • 3
  • 4
1

restarting the idle after installing pywin32 works for me

0

In my case, the only thing that worked was to download the appropriate wheel from: https://pypi.org/project/pywin32/#files, and install with --force-reinstall.

pip install pywin32-300-cp37-cp37m-win_amd64.whl --force-reinstall

somberlain
  • 69
  • 6
0

I found solution here: https://www.ti-enxame.com/pt/python/pywin32-e-python-3.8.0/813327700/

I was able to run it on Spyder without error, but It wasn't working on cmd prompt

I just import the module pywintypes before win32api

import pywintypes
import win32api
0

I tried to reinstall pywin32, installed different versions, but nothing could make pywin work. The only thing that did finally help me was running

python pywin32_postinstall.py

which is located at Anaconda3\Scripts folder. Thanks for sameer_nubia for highlighting the location.

rt_dobry
  • 1
  • 1
  • Does this post contribute any new insight? Then please [edit] to make that more obvious. Otherwise, if it only is meant as a "thanks", please delete this. – Yunnosch Sep 25 '22 at 19:15
0

I solve this by

python -m pip install -U pywin32 pypiwin32
Ray Ronnaret
  • 138
  • 2
  • 10