简体   繁体   English

导入错误:没有名为 win32com.client 的模块

[英]ImportError: No module named win32com.client

I am currently using python 2.7 and trying to open an Excel sheet.我目前正在使用 python 2.7 并尝试打开 Excel 工作表。 When using the code below:使用以下代码时:

import os
from win32com.client import Dispatch

xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.Visible = True
# Open the file we want in Excel
workbook = xlApp.Workbooks.Open('example.xls')

I get this error:我收到此错误:

ImportError: No module named win32com.client导入错误:没有名为 win32com.client 的模块

Is there any possibility of getting the error since I am using 64-bit Windows machine?由于我使用的是 64 位 Windows 机器,是否有可能出现错误?

pip install pywin32对我不起作用,但pypiwin32了。

win32com.client is a part of pywin32 win32com.clientpywin32的一部分

So, download pywin32 from here所以,从这里下载 pywin32

Try this command:试试这个命令:

pip install pywin32

Note笔记

If it gives the following error:如果它给出以下错误:

Could not find a version that satisfies the requirement pywin32>=223 (from pypiwin32) (from versions:)找不到满足要求的版本 pywin32>=223 (from pypiwin32) (from versions:)
No matching distribution found for pywin32>=223 (from pypiwin32)找不到 pywin32>=223 的匹配分布(来自 pypiwin32)

upgrade 'pip', using:升级“pip”,使用:

pip install --upgrade pip

Try both pip install pywin32 and pip install pypiwin32 .尝试pip install pywin32pip install pypiwin32

It works.有用。

I realize this post is old but I wanted to add that I had to take an extra step to get this to work.我意识到这篇文章很旧,但我想补充一点,我必须采取额外的步骤才能让它发挥作用。

Instead of just doing:而不是仅仅做:

pip install pywin32

I had use use the -m flag to get this to work properly.我曾使用-m标志使其正常工作。 Without it I was running into an issue where I was still getting the error ImportError: No module named win32com .没有它,我遇到了一个问题,我仍然收到错误ImportError: No module named win32com

So to fix this you can give this a try:因此,要解决此问题,您可以尝试一下:

python -m pip install pywin32

This worked for me and has worked on several version of python where just doing pip install pywin32 did not work.这对我有用,并且已经在几个版本的 python 上工作,其中只执行pip install pywin32不起作用。

Versions tested on:测试版本:

3.6.2, 3.7.6, 3.8.0, 3.9.0a1. 3.6.2、3.7.6、3.8.0、3.9.0a1。

Had the exact same problem and none of the answers here helped me.有完全相同的问题,这里的答案都没有帮助我。 Till I find this thread and post直到我找到这个帖子并发布

Short: win32 modules are not guaranted to install correctly with pip.简而言之:不能保证使用 pip 正确安装 win32 模块。 Install them directly from packages provided by developpers on github .直接从开发人员在 github 上提供的包中安装它们 It works like a charm.它就像一个魅力。

You must install the package pywin32 :您必须安装包pywin32

pip install pywin32

After installation import win32com.client安装后import win32com.client

Python has the “Python for Windows Extensions” package known as pywin32 that allows us to easily access Window's Component Object Model (COM) and control Microsoft applications. Python 拥有名为pywin32的“Python for Windows Extensions”包,它允许我们轻松访问 Window 的组件对象模型 (COM) 并控制 Microsoft 应用程序。

Try to install the "pywin32" file, you can find in https://github.com/mhammond/pywin32/releases尝试安装“pywin32”文件,你可以在https://github.com/mhammond/pywin32/releases 中找到

Install the version that you use in your IDLE, and try to install, after you can open your project and compile another turn!安装你在IDLE中使用的版本,然后尝试安装,打开你的项目再编译一遍即可!

thanks !谢谢 !

in some cases where pywin32 is not the direct reference and other libraries require pywin32-ctypes to be installed;在某些情况下,pywin32 不是直接引用,其他库需要安装 pywin32-ctypes; causes the "ImportError: No module named win32com" when application bundled with pyinstaller.当应用程序与 pyinstaller 捆绑在一起时,会导致“ImportError: No module named win32com”。

running following command solves on python 3.7 - pyinstaller 3.6在 python 3.7 上运行以下命令可以解决 - pyinstaller 3.6

pip install pywin32==227

I'm using Visual Studio Code on a 64-bit laptop using Windows.我在使用 Windows 的 64 位笔记本电脑上使用 Visual Studio Code。 I eventually got this to work!我最终得到了这个工作! First install pywin32 as normal:首先正常安装pywin32:

python -m pip install pywin32

If you're using Code Runner, you may need to make sure that you have the right path to your module in your code:如果您使用的是 Code Runner,您可能需要确保您的代码中有正确的模块路径:

import sys
sys.path.append("C:\\_path_to_virtual_environment\\Lib\\site-packages\\")

import win32com.client as win32

Now change directory in your terminal windows to your scripts folder and run this:现在将终端窗口中的目录更改为脚本文件夹并运行:

python pywin32_postinstall.py -install

This will change your error message to say you're missing the win32api module.这将更改您的错误消息,表明您缺少 win32api 模块。 To get this, install this:要得到这个,安装这个:

python -m pip install pypiwin32

I wouldn't say I had a profound insight what all these commands do, but it (finally) solved my problem!我不会说我对所有这些命令的作用有深刻的了解,但它(最终)解决了我的问题!

ImportError: No module named win32com.client导入错误:没有名为 win32com.client 的模块

  1. Open Command prompt in admin mode在管理员模式下打开命令提示符

  2. Install win32com.client安装 win32com.client

    a.一种。 By pip install method通过pip安装方法

     pip install win32 If this throws error: version of win32 not determined then try installing via b. By pypi install method

    b.By pypi install method通过pypi安装方法

    python -m pip install pywin32

3. Add program path to the python path 3.在python路径中添加程序路径

Win32COM is (and was always) part of PyWin32 . Win32COM是(并且一直是) PyWin32的一部分

1. PyWin32 1.PyWin32

[GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions is a Python wrapper over WinAPI s). [GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions is a Python wrapper over WinAPI s).
Some documentation can be found at [ME.TimGolden]: Python for Win32 Extensions Help .一些文档可以在[ME.TimGolden]: Python for Win32 Extensions Help找到。

Lately, binaries ( .whl s) are published on [PyPI]: pywin32 (starting with v222 (from 20180121 ) - there are a couple of older records, but they don't contain any (useful) packages).最近,二进制文件 ( .whl s) 在[PyPI] 上发布:pywin32 (从v222开始(从20180121开始)——有一些较旧的记录,但它们不包含任何(有用的)包)。

As many other software, it was previously hosted on [SourceForge]: mhammond - Python for Windows Extensions (binaries (( .exe ) installers) were released there as well - last one v221 (from 20170329 )).与许多其他软件一样,它以前托管在[SourceForge] 上:mhammond - Python for Windows 扩展(二进制文件(( .exe )安装程序)也在那里发布 - 最后一个v221 (来自20170329 ))。

2. PyPIWin32 2.PyPIWin32

Only exists on [PyPI]: pypiwin32 .仅存在于[PyPI]: pypiwin32 上

Currently (at answer time), there are only 3 versions:目前(在回答时)只有 3 个版本:

  1. v219 (from 20141203 ): v219 (自20141203 起):

    • Contains .whl s for various Python versions包含各种Python版本的.whl
  2. v220 (from 20170112 ): v220 (自20170112 起):

    • Only contains .whl s for Python 3.6仅包含.whl s for Python 3.6
  3. v223 (from 20180226 ): v223 (自20180226 起):

    • Only contains one .whl with no data (but having PyWin32 as a dependency)仅包含一个没有数据.whl (但具有PyWin32作为依赖项)

Also check Mark Hammond's answer: [GitHub]: mhammond/pywin32 - Add CascadeWindows wrapper (@mhammond's comment) .另请查看 Mark Hammond 的回答: [GitHub]: mhammond/pywin32 - Add CascadeWindows wrapper (@mhammond's comment)

From the above, one can only conclude that PyPIWin32 was just a playground for testing the newly introduced .whl packaging scheme and (except 1 st version) is just garbage.从上面只能得出结论, PyPIWin32只是测试新引入的.whl打包方案的游乐场,并且(1 版除外)只是垃圾。

As a consequence:作为结果:

Do NOT install PyPIWin32 ( pip install pypiwin32 )不要安装PyPIWin32pip install pypiwin32

Installing PyWin32 is all you need: python -m pip install pywin32 .您只需要安装PyWin32python -m pip install pywin32

If it doesn't work after that, there are other issues.如果在那之后它不起作用,还有其他问题。
Here are a couple of links with potential problems (and ways to overcome them), generic advices, and so on:以下是一些与潜在问题(以及克服这些问题的方法)、一般建议等相关的链接:

I don't have any experience with this particular library (I'm just getting into it myself), but I did notice that you only imported the Dispatch class from the module. 我对这个特定的库没有任何经验(我只是自己进入它),但我注意到你只从模块中导入了Dispatch类。 So you either need to just change your import as such: 所以你要么只需要改变你的导入:

import win32com.client

OR change your instance call like so: 或者改变你的实例调用:

xlApp = Dispatch("Excel.Application")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM