python 如何在无需包含主机环境的 site-packages 文件夹的情况下将 pywin32 与 virtualenv 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1830304/
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-11-03 23:10:02  来源:igfitidea点击:

How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder?

pythonwinapivirtualenvpyinstaller

提问by jkp

I'm working with PyInstallerunder Python 2.6, which is only partially supported due to the mess MS have created with their manifest nonense which now affects Python since it is now MSVC8 compiled.

我正在Python 2.6 下使用PyInstaller,由于 MS 用他们的清单废话创建的混乱现在影响 Python,因为它现在是 MSVC8 编译,因此仅部分支持。

The problem is that the manifest embedding support relies on the pywin32extensions in order to build which is a pain because without including the host's site-packages folder when I create the virtualenv(kinda defeats the point in a build environment) I cannot find a way to install the required extensions so they are accessible to PyInstaller.

问题是清单嵌入支持依赖于pywin32扩展来构建,这很痛苦,因为在创建virtualenv时不包括主机的站点包文件夹(有点在构建环境中失败了)我找不到方法安装所需的扩展,以便 PyInstaller 可以访问它们。

Has anyone found a solution to this issue?

有没有人找到解决这个问题的方法?

采纳答案by jkp

OK, well since I had to find a way forward I improvised. I've internally created a git repository with a hacked-together version of pywin32 that will install within a virtualenv using the standard setup.pyscript. It took a lot of fiddling to make it work right but I managed to get it to load and the dependent code now works as I need it to. If people feel this would be of benefit to the community please post a comment: if I get enough I'll try and put something up on my github account.

好吧,因为我必须找到前进的道路,所以我即兴创作。我在内部创建了一个 git 存储库,其中包含一个 pywin32 的 hacked-together 版本,它将使用标准setup.py脚本安装在 virtualenv 中。花了很多功夫才让它正常工作,但我设法让它加载,依赖代码现在可以按我的需要工作。如果人们觉得这对社区有益,请发表评论:如果我受够了,我会尝试在我的 github 帐户上放一些东西。

回答by david.libremone

I found http://old.nabble.com/Windows:-virtualenv-and-pywin32--td27658201.html(now a dead link) which offered the following solution:

我发现http://old.nabble.com/Windows:-virtualenv-and-pywin32--td27658201.html(现在是一个死链接)它提供了以下解决方案:

  1. Browse http://sourceforge.net/projects/pywin32/files/for the URL of the exe you want
  2. Activate your virtualenv
  3. Run easy_install http://PATH.TO/EXE/DOWNLOAD
  1. 浏览http://sourceforge.net/projects/pywin32/files/找到你想要的 exe 的 URL
  2. 激活您的虚拟环境
  3. easy_install http://PATH.TO/EXE/DOWNLOAD

This works with modern versions of setuptools (circa February 2014, reported by tovmeod in the comments).

这适用于现代版本的 setuptools(大约 2014 年 2 月,由 tovmeod 在评论中报告)。



If you are using an old version of setuptools (or distribute it merged back into setuptools), you may get this error message:

如果您使用的是旧版本的 setuptools(或将其分发并合并回 setuptools),您可能会收到以下错误消息:

error: c:\users\blah\appdata\local\temp\easy_install-ibkzv7\pywin32-214.win32-py2.6.exe is not a valid distutils Windows .exe

错误:c:\users\blah\appdata\local\temp\easy_install-ibkzv7\pywin32-214.win32-py2.6.exe 不是有效的 distutils Windows .exe

In which case:

在这种情况下:

  1. Download the exe yourself
  2. Activate your virtualenv
  3. Run easy_install DOWNLOADED_FILE.exe
  1. 自己下载exe
  2. 激活您的虚拟环境
  3. easy_install DOWNLOADED_FILE.exe


I rather hopefully tried "pip install" rather than "easy_install", but this didn't work, and likely never will (citation needed).

我更希望尝试“pip install”而不是“easy_install”,但这不起作用,并且可能永远不会(需要引用)。



Finally, I found but haven't tested a solution at http://www.mail-archive.com/[email protected]/msg272040.htmlwhich is:

最后,我在http://www.mail-archive.com/[email protected]/msg272040.html找到但尚未测试的解决方案,它是:

Solved this by copying the pywin32.pth file into my virtualenv site-packages and editing the file to point to the path.

通过将 pywin32.pth 文件复制到我的 virtualenv 站点包并编辑文件以指向路径来解决此问题。

If the other options don't work for you, maybe this will?

如果其他选项对您不起作用,也许这会起作用?

回答by Andrei Damian-Fekete

For Python 2.7 or 3.x use pypiwin32.

对于 Python 2.7 或 3.x,请使用pypiwin32

pip install pypiwin32

pip install pypiwin32

回答by cthepenier

This may have been improved since previous answer, since I've successfully installed pywin32 on sandbox on several machines without any specific "hacks" :

自从上一个回答以来,这可能已经有所改进,因为我已经在多台机器上的沙箱上成功安装了 pywin32,没有任何特定的“黑客”:

$ virtualenv sandbox
$ sandbox\scripts\activate
(sandbox) $ git clone https://github.com/Travis-Sun/pywin32.git
(sandbox) $ cd pywin32
(sandbox) $ python setup.py install

Tested with following environment :

在以下环境下测试:

回答by tr00st

Edit: Scratch this for now, appears to be some problems with the installation still...

编辑:现在抓紧这个,似乎安装仍然存在一些问题......

I got rather tired of the whole situation, and just created a set of converted wheels ("wheel convert <.exe>"). I'll try and keep them maintained for the most recent build, but do shout if there are any issues.

我对整个情况感到厌烦,只是创建了一组转换轮(“wheel convert <.exe>”)。我会尽量让它们在最新版本中得到维护,但如果有任何问题,请大声喊叫。

https://tr00st.co.uk/python/wheel/pywin32/

https://tr00st.co.uk/python/wheel/pywin32/

Installation can be done easily using pip and pointing to the package matching your version and architecture. For example, for Python 3.5/amd64:

使用 pip 并指向与您的版本和架构匹配的包,可以轻松完成安装。例如,对于 Python 3.5/amd64:

pip install https://tr00st.co.uk/python/wheel/pywin32/pywin32-219-cp35-none-win_amd64.whl

pip 安装https://tr00st.co.uk/python/wheel/pywin32/pywin32-219-cp35-none-win_amd64.whl

Caveat: The --upgrade process currently fails, as the uninstall procedure is unable to clean up after itself (Access Denied when cleaning up win32api.pyd) - this is only when removing the temporary directory, which can be manually deleted. Easiest way around this is to uninstall and reinstall instead of upgrading, then manually delete the temporary folder.

警告: --upgrade 进程当前失败,因为卸载程序无法自行清理(清理 win32api.pyd 时拒绝访问) - 这仅在删除临时目录时,可以手动删除。解决此问题的最简单方法是卸载并重新安装而不是升级,然后手动删除临时文件夹。