ImportError: no module named win32api [duplicate]
Asked Answered
B

17

163

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?

Berkow answered 24/1, 2014 at 22:39 Comment(5)
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?Breccia
It's possible that the path msi installer installed to is different from the path you're trying to import from.Daffy
Claudiu: no, i have only installed python 2.7 on my pcBerkow
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??Berkow
I'm getting this when trying to run a Jupyter notebook in PyCharm. It work fine before, not sure what happened...Despite
K
282

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

pip install pypiwin32
Korry answered 11/3, 2016 at 19:43 Comment(6)
I am using python2.7 and pypiwin32 has been successfully installedBoudicca
This only worked for me AFTER I restarted Spyder. Most times new packages work immediately after being installed, but not in this case.Fango
Worked in March 2020 for Python 3.7.5 ! Not sure why this even lingers on though..Hent
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) "Vying
Don't do this. [SO]: ImportError: No module named win32com.client (@CristiFati's answer).Rapeseed
Requirement already satisfied: pywin32 in c:\python311\lib\site-packages (306)Eidolon
S
59

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.

Secondhand answered 29/1, 2018 at 17:17 Comment(11)
This worked for me. I use pipenv in my project. Had to run the post_install.py in my env folder.Privity
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'Entomophilous
@Entomophilous try running it as admin.Secondhand
@Secondhand stame result as last time.Entomophilous
@Entomophilous 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.Secondhand
@Secondhand 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?)Entomophilous
@Secondhand I found this page here, github.com/mhammond/pywin32/releases, and will be re-downloaded and installing from binary with the AMD 64bit release for Python 3.7.Entomophilous
@Entomophilous check your bitness. Maybe it should match your system's and I'm not sure if the default install you get is 32-bit.Secondhand
@Secondhand 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....Entomophilous
This was what I had to do. I think this should be marked as the right answer.Monophthong
@Secondhand 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.Ober
R
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.

Riella answered 21/8, 2014 at 3:42 Comment(2)
This helped me too. At first it wouldn't work. Then I restarted everything and it worked.Fango
This worked for some reason...Disentail
G
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.

Gobioid answered 16/5, 2017 at 21:43 Comment(1)
i used conda install pywin32 and it worked. Thanks huge.Polyanthus
D
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.

Despite answered 9/12, 2020 at 7:22 Comment(3)
Uninstalling pywin32 and installing again did the trick for me. Thanks a lot. Very grateful !Examination
After restarting this really worked thanksCourage
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.Nonpareil
W
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

Warp answered 23/12, 2020 at 19:56 Comment(0)
W
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 .

Walkin answered 2/11, 2019 at 6:8 Comment(1)
Your solution was the only efficiently for me. Thank you.Latrinalatrine
F
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.

Faith answered 3/5, 2021 at 0:38 Comment(0)
S
1

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

 pip install pywin32==225
Shake answered 25/7, 2021 at 13:56 Comment(0)
I
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.

Intensifier answered 15/9, 2022 at 12:48 Comment(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.Subconscious
E
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://mcmap.net/q/149573/-importerror-no-module-named-win32api-duplicate
python %CONDA_PREFIX%\Scripts\pywin32_postinstall.py -install

# double check
python -c "print( __import__('win32api') )"
Epicenter answered 3/11, 2022 at 3:44 Comment(0)
C
1

restarting the idle after installing pywin32 works for me

Caporetto answered 1/3, 2023 at 10:21 Comment(0)
L
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

Lallans answered 27/4, 2021 at 14:34 Comment(0)
J
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
Jugulate answered 10/1, 2022 at 18:59 Comment(0)
K
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.

Krysta answered 8/8, 2022 at 10:25 Comment(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.Subconscious
R
0

I solve this by

python -m pip install -U pywin32 pypiwin32
Riyadh answered 30/12, 2022 at 6:22 Comment(0)
W
0

If it is showing an error after installing ---> pip install pywin32

Simply just Restart your code editor it will solve the problem

Windtight answered 4/9, 2023 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.