Python 3.4 :ImportError: no module named win32api
Asked Answered
M

7

18

I am using python 3.4 on windows 7. In order to open a doc file I am using this code:

import sys
import win32com.client as win32

word = win32.Dispatch("Word.Application")
word.Visible = 0
word.Documents.Open("MyDocument")
doc = word.ActiveDocument

I'M not sure why is this error popping up every time:

ImportError: no module named win32api

Although I have installed pywin32 from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32 and I have also checked the path from where I am importing. I have tried reinstalling pywin32 as well but that doesn't remove the error.

Meant answered 12/8, 2014 at 6:9 Comment(5)
Just to make sure: You installed the version for Python 3.4, and for either win32 or win-amd64, whichever one matches your Python installation, right?Schoolfellow
Also, try import win32api directly from an interactive interpreter. And, while we're at it, import importlib; print(importlib.util.find_spec('win32api'))Schoolfellow
thnxx @Schoolfellow .... going to try this now.Meant
@Meant which file did you download from that link?Cere
@Patrick i downloaded - pywin32‑219.win32‑py3.4.exe...its ok i guess coz m using python 3.4Meant
C
19

Try to install pywin32 from here :

http://sourceforge.net/projects/pywin32/files/pywin32/

depends on you operation system and the python version that you are using. Normally 32bit version should works on both 32 and 64 bit OS.

EDIT: moved to https://github.com/mhammond/pywin32/releases

Cleptomania answered 21/10, 2014 at 11:47 Comment(0)
G
17

This is a bug in the library itself, probably they used a different python implementation for creating this.

What they are trying to import is the site-packages\win32\win32api.pyd file, but the win32 folder is not in the path that python searches in, but site-packages is.

Try to replace the import win32api (inside win32com\__init__.py) to from win32 import win32api

Gerson answered 21/5, 2015 at 14:44 Comment(1)
When neither (1) pip install pypiwin32 nor (2) Nima's answer worked for me (Python 3.7 64 bit on Win 10), I successfully tried yours (3), but after a reboot (4), import win32api works too. Don't know whether (3) or (4) did the trick. May be (2) isn't needed either.Dede
S
13

I encountered the same error yestoday with Python 3.6.1 on Windows 7, and resolved it by "pip install pypiwin32".

Skate answered 31/3, 2017 at 2:24 Comment(0)
I
2

Had the same error trying to import win32com.client (using Python 2.7, 64-bit). I agree with TulkinRB, there seem to be path issues, but the fix suggested did not work for me, since I also could not import win32.

Perhaps my fix will also work in Python 3.4.

Eventually, installing the .exe from SourceForge as an administrator (as suggested in Rina Rivera's answer here) allowed me to import win32com.client from IDLE, but not when I executed the script I was originally trying to run.

In the end, I discovered 3 differences in the sys.path that had been extended when I installed as admin and opened IDLE, but were not applied when executing a script. By extending the sys.path in my script, I was able to get rid of the import errors when I executed it:

import sys
sys.path.extend(('C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin'))

Finally, if you want more than a temporary fix, the sys.path could be permanently extended by establishing IDLESTARTUP or PYTHONSTARTUP variables (as described here and here).

Intervale answered 6/7, 2016 at 17:57 Comment(0)
D
1

You can create the __init.py file inside the win32 folder and then go inside the win32com folder and change its __init__.py file, where it is import win32api, change to from win32 import win32api

Deplete answered 10/9, 2017 at 14:17 Comment(0)
O
0

I ended up debugging and copying and pasting the necessary files into the appropriate folders. It's a work-around until the bug is fixed, but it works.

Oceanography answered 16/3, 2016 at 4:54 Comment(0)
I
0

from https://github.com/mhammond/pywin32/issues/1151#issuecomment-360669440

append the 'pypiwin32_system32' path to your system PATH,

in a script this can be done like:

import os
sitedir='C:/where_ever/'
os.environ["PATH"]+=(';'+os.path.join(sitedir,"pypiwin32_system32"))
...

from powershell

$env:PATH="$PATH;C:\where_ever\pywin32_system32";
python.exe ...

for help on site dir, see What is python's site-packages directory?

Interlace answered 20/12, 2020 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.