在Python 2.6中导入win32api出错
在64位的Windows操作系统上运行python26的时候,我遇到了这样的错误:
import win32api" error in Python 2.6: pywintypes26.dll
或者
pythoncom26.dll missing
ImportError: DLL load failed: The specified module could not be found.
我已经完成了python26的msi安装,所有的dll文件都可以在C:\Python26\Lib\site-packages\pywin32_system32这个路径下找到。
12 个回答
我在安装64位的Python 3.4.2时遇到了类似的问题。我从http://www.lfd.uci.edu/~gohlke/pythonlibs/下载了安装程序pywin32‑219.win‑amd64‑py3.4.exe。
在这个网站上,它明确说明:
“Python 3.4的用户必须从提升权限的命令提示符手动运行 python.exe Scripts\pywin32_postinstall.py -install
。”
我第一次没有这样做;我是在普通的命令提示符下安装的,结果得到了以下反馈:
c:\python34>python.exe Scripts\pywin32_postinstall.py -install
Copied pythoncom34.dll to C:\Python34\pythoncom34.dll
Copied pywintypes34.dll to C:\Python34\pywintypes34.dll
You do not have the permissions to install COM objects.
The sample COM objects were not registered.
etc.etc.
Finishing with "The pywin32 extensions were succesfully installed" ...
我只读了最后一句话,然后开始运行一些代码,结果出现了dll加载失败的问题。
所以,我做了一些研究,启动了一个提升权限的命令提示符(怎么做可以参考“http://www.sevenforums.com/tutorials/783-elevated-command-prompt.html”),然后再次运行:
c:\python34>python.exe Scripts\pywin32_postinstall.py -install
Copied pythoncom34.dll to C:\Windows\system32\pythoncom34.dll
Copied pywintypes34.dll to C:\Windows\system32\pywintypes34.dll
Registered: Python.Interpreter
Registered: Python.Dictionary
Registered: Python
-> Software\Python\PythonCore\3.4\Help[None]=None
-> Software\Python\PythonCore\3.4\Help\Pythonwin Reference[None]='C:\\Python34\\Lib\\site-packages\\PyWin32.chm'
Pythonwin has been registered in context menu
Shortcut for Pythonwin created
Shortcut to documentation created
The pywin32 extensions were successfully installed.
现在我的代码运行得很顺利(就这个问题而言……唉,还有很多其他事情要做)。
我也遇到了导入win32api的问题。
pywin32的安装后脚本出错了,这个脚本应该负责复制一些文件,比如pythoncom26.dll
、pythoncomloader26.dll
和pywintypes26.dll
等。我手动运行了一下这个脚本,结果我的安装问题就解决了。
python scripts\pywin32_postinstall.py -install
我把 C:\Python26\Lib\site-packages\pywin32_system32
里的 pywintypes26.dll 和 pythoncom26.dll 这两个文件复制到了 C:\Python26\Lib\site-packages\win32
里,这样问题就解决了!