Python 3.4 :ImportError: 没有名为 win32api 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25257274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:57:57  来源:igfitidea点击:

Python 3.4 :ImportError: no module named win32api

pythonpywin32

提问by Maxxie

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

我在 Windows 7 上使用 python 3.4。为了打开一个 doc 文件,我使用了这个代码

import sys
import win32com.client as win32

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

M not sure why is this error popping up everytime

我不知道为什么每次都会弹出这个错误

ImportError: no module named win32api

导入错误:没有名为 win32api 的模块

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

虽然我已经从http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32安装了 pywin32 并且我还检查了我导入的路径...我也尝试重新安装 pywin32 但这并没有删除错误.....

any suggestions....please help

任何建议....请帮忙

Thanks

谢谢

采纳答案by Nima Soroush

Try to install pywin32 from here :

尝试从这里安装 pywin32:

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

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.

取决于您的操作系统和您使用的 python 版本。通常 32 位版本应该适用于 32 位和 64 位操作系统。

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

编辑:移至https://github.com/mhammond/pywin32/releases

回答by TulkinRB

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

这是库本身的一个错误,可能他们使用了不同的 python 实现来创建它。

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.

他们试图导入的是 site-packages\win32\win32api.pyd 文件,但 win32 文件夹不在 python 搜索的路径中,但 site-packages 是。

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

尝试将import win32api(inside win32com\__init__.py)替换为from win32 import win32api

回答by Bennybear

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.

我最终调试并将必要的文件复制并粘贴到适当的文件夹中。在修复错误之前,这是一种解决方法,但它有效。

回答by hanaQokus

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.

尝试导入 win32com.client(使用 Python 2.7,64 位)时出现同样的错误。我同意TulkinRB,似乎存在路径问题,但建议的修复对我不起作用,因为我也无法导入 win32。

Perhaps my fix will also work in Python 3.4.

也许我的修复也适用于 Python 3.4。

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

最终,以管理员身份从SourceForge安装 .exe (如Rina Rivera在此处的回答中所建议)允许我从 IDLE 导入 win32com.client,但在我执行最初尝试运行的脚本时却不能

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:

最后,我发现 sys.path 中有 3 个差异,这些差异在我以管理员身份安装并打开 IDLE 时已扩展,但在执行脚本时并未应用。通过在我的脚本中扩展 sys.path,我能够在执行它时摆脱导入错误:

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 hereand here).

最后,如果您想要的不仅仅是临时修复,可以通过建立 IDLESTARTUP 或 PYTHONSTARTUP 变量(如此此处所述)来永久扩展 sys.path 。

回答by flyisland

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

我昨天在 Windows 7 上使用 Python 3.6.1 遇到了同样的错误,并通过“pip install pypiwin32”解决了它。

回答by Renan Ferreira de Lima

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

可以在win32文件夹内创建__init.py文件,然后进入win32com文件夹,修改它的__init__.py文件,这里是import win32api,改成from win32 import win32api