ModuleNotFoundError: 没有名为“win32api”的模块

Bar*_*tin 1 python winapi module

这是收到的错误:

    Traceback (most recent call last):
  File "C:/Users/Joe Martin/AppData/Local/Programs/Python/Python37/test.py", line 12, in <module>
    import win32com.client
  File "C:\Users\Joe Martin\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\__init__.py", line 5, in <module>
    import win32api, sys, os
ModuleNotFoundError: No module named 'win32api'
Run Code Online (Sandbox Code Playgroud)

尝试导入 win32com.client 模块时会发生此错误。

尝试的解决方案:

  • 全新擦除并安装 Python 3.7
  • pip install pypiwin32
  • pip install pywin32
  • 运行 pywin32_postinstall.py

我找不到任何其他解决方案来解决这个问题。

Dra*_*SFT 5

这通常是因为PythonPath安装包后没有附加no 。检查文件--文件pywin32.pth夹下-- \\PythonVersion\\Lib\\site-packages\\

文件内容如下:

# .pth file for the PyWin32 extensions
win32
win32\lib
Pythonwin
# Entries needed for a "portable" installations, where the post_install script
# isn't run, which would normally copy the pywin32 core DLL files to either
# the top of the python directory.
# We just stick the source of these DLLs directly on the PATH.
import os;os.environ["PATH"]+=(';'+os.path.join(sitedir,"pywin32_system32"))
Run Code Online (Sandbox Code Playgroud)

或者创建一个PYTHONPATH环境变量,并将win32win32/lib路径附加到其中。

你也可以在项目中临时将这两条路径添加到 Python 中吗?

import sys
sys.path.append('\\PythonVersion\\lib\\site-packages\\win32')
sys.path.append('\\PythonVersion\\lib\\site-packages\\win32\\lib')
Run Code Online (Sandbox Code Playgroud)

添加的路径暂时只有效。