Python 如何在 virtualenv 中安装 win32com 模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14913607/
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
How to install win32com module in a virtualenv?
提问by kjo
I have installed both virtualenvand pywin32on my Windows 7 64-bit laptop, and each of them seems to work, but not with each other.
我已经在我的 Windows 7 64 位笔记本电脑上安装了virtualenv和pywin32,并且它们中的每一个似乎都可以工作,但不能相互配合。
More specifically, if a virtualenv is active, then running import win32com.clientin a python interactive shell fails with No module named win32com.client. The same expression succeeds, however, if no virtualenv is active.
更具体地说,如果 virtualenv 处于活动状态,则import win32com.client在 python 交互式 shell 中运行会失败并显示No module named win32com.client. 但是,如果没有 virtualenv 处于活动状态,则相同的表达式会成功。
When I try to install pywin32with pip(which is how I normally install modules when a virtualenv is active), I get the error:
当我尝试安装pywin32时pip(这是我在 virtualenv 处于活动状态时通常安装模块的方式),出现错误:
Could not find any downloads that satisfy the requirement pywin32
No distributions at all found for pywin32
...even though pywin32is one of the modules listed in the output of
...即使pywin32是输出中列出的模块之一
pip search pywin32
Therefore, to install pywin32at allI had to install it using an *.exe installer downloaded from SourceForge.
因此,要安装它pywin32,我必须使用从 SourceForge 下载的 *.exe 安装程序进行安装。
Is there any way to install pywin32within a virtualenv?
有没有办法pywin32在 virtualenv 中安装?
回答by chirinosky
Try this:
尝试这个:
- Download directly
- Enable your virtualenv
pip install --no-index --find-links:/local/dir/ SomePackage
- 直接下载
- 启用您的虚拟环境
pip install --no-index --find-links:/local/dir/ SomePackage
Check out #8 on pip-installer.org(sorry no permalink)/
在pip-installer.org上查看 #8 (抱歉没有永久链接)/
回答by Christian Long
UPDATE 2016
2016 年更新
There is now a version of pywin32 on PyPI that can be installed with pip. It is called pypiwin32, and it installs the package using the binary wheelformat.
现在 PyPI 上有一个 pywin32 版本,可以用 pip 安装。它被称为pypiwin32,它使用二进制wheel格式安装包。
https://pypi.python.org/pypi/pypiwin32
https://pypi.python.org/pypi/pypiwin32
pip install pypiwin32
That will work in a virtualenv, or with tox, etc.
这将适用于 virtualenv 或 tox 等。
Below this line is my previous old answer. That is now outdated information.
这条线下面是我以前的旧答案。这是现在过时的信息。
OLD ANSWER - OUTDATED. Modern versions of virtualenv default to --no-site-packages. That means that not having access to global site-packages is now the default behavior. Sadly, (as of July 2014) you can not pip install pywin32 in to your virtualenv. (here's the bug report) If you want to get pywin32 running inside a virtualenv, activate the virtualenv and use easy_install and the pywin32 installer exe file. For example easy_install "C:\Path\To\Downloads\pywin32-219.win32-py3.4.exe"
旧答案 - 过时了。现代版本的 virtualenv 默认为 --no-site-packages。这意味着无法访问全局站点包现在是默认行为。遗憾的是,(截至 2014 年 7 月)您无法将 pywin32 pip install 安装到您的 virtualenv 中。(这是错误报告)如果您想让 pywin32 在 virtualenv 中运行,请激活 virtualenv 并使用 easy_install 和 pywin32 安装程序 exe 文件。例如easy_install "C:\Path\To\Downloads\pywin32-219.win32-py3.4.exe"
回答by ldiary
Create a virtualenv and activate it:
cd c:\Users\ernesto.luzon
virtualenv --no-site-packages py351env
py351env\Scripts\activate
创建一个 virtualenv 并激活它:
cd c:\Users\ernesto.luzon
virtualenv --no-site-packages py351env
py351env\Scripts\activate
From here, you have two options:
从这里,您有两个选择:
Download pywin32 from sourceforge project: http://sourceforge.net/projects/pywin32/files/pywin32/
Download pywin32 from unofficial (but very helpful) binary site: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32
从sourceforge项目下载pywin32:http: //sourceforge.net/projects/pywin32/files/pywin32/
从非官方(但非常有用)二进制站点下载 pywin32:http: //www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32
Make sure you download the correct version for the Python Interpreter installed in your environment, otherwise you will encounter ImportError: DLL load failed: %1 is not a valid Win32 applicationlater.
确保为您的环境中安装的 Python 解释器下载正确的版本,否则您将遇到ImportError: DLL load failed: %1 is not a valid Win32 application.
If you downloaded from sourceforge, install it using easy_install:
(py351env) C:\Users\ernesto.luzon\Downloads>easy_install pywin32-220.win-amd64-py3.5.exe
如果您是从 sourceforge 下载的,请使用 easy_install 安装它:
(py351env) C:\Users\ernesto.luzon\Downloads>easy_install pywin32-220.win-amd64-py3.5.exe
If you downloaded from gohlke, install it using pip:
(py351env) C:\Users\ernesto.luzon\Downloads>pip install pywin32-220-cp35-none-win_amd64.whl
如果您从 gohlke 下载,请使用 pip 安装它:
(py351env) C:\Users\ernesto.luzon\Downloads>pip install pywin32-220-cp35-none-win_amd64.whl
In case you encounter ImportError: DLL load failed: The specified module could not be founderror later on, you need these additional steps:
如果您遇到ImportError: DLL load failed: The specified module could not be found错误,您需要执行以下额外步骤:
Run the post install script:
(py351env) C:\Users\ernesto.luzon>python py351env\Scripts\pywin32_postinstall.py -install
Copied pythoncom35.dll to C:\Users\ernesto.luzon\py351env\pythoncom35.dll
Copied pywintypes35.dll to C:\Users\ernesto.luzon\py351env\pywintypes35.dll
....
运行安装后脚本:
(py351env) C:\Users\ernesto.luzon>python py351env\Scripts\pywin32_postinstall.py -install
Copied pythoncom35.dll to C:\Users\ernesto.luzon\py351env\pythoncom35.dll
Copied pywintypes35.dll to C:\Users\ernesto.luzon\py351env\pywintypes35.dll
....
Notice where it copied the 'pythoncom35.dll' and 'pywintypes35.dll' files. You need to move these files to the folder:
C:\Users\ernesto.luzon\py351env\Lib\site-packages\win32
注意它复制了“pythoncom35.dll”和“pywintypes35.dll”文件的位置。您需要将这些文件移动到文件夹中:
C:\Users\ernesto.luzon\py351env\Lib\site-packages\win32

