PyWin32 (226) 和虚拟环境 [英] PyWin32 (226) and virtual environments

查看:39
本文介绍了PyWin32 (226) 和虚拟环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[PyPI]: pywin32 226 已于20191110发布.
适用大多数Python安装(例如适用于从Python),但不是在虚拟环境中(例如 创建VirtualEnv (v16.7.7) 和(基于进一步研究)Pythonvenv).
我使用了 Python v3.8.0v3.7.3 作为实验室老鼠.这是前者的输出:

<块引用>

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q058805040]>"e:\Work\Dev\VEnvs\py_064_03.08.00_test0\Scripts\python.exe" -c "导入 win32api"致命的 Python 错误:init_import_size:无法导入站点模块Python 运行时状态:已初始化回溯(最近一次调用最后一次):文件e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py",第 769 行,在 <module>主要的()文件e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py",第 746 行,在 mainpath_in_sys = addsitepackages(paths_in_sys)文件e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py",第 279 行,在 addsitepackages 中添加站点目录(站点目录,known_paths)文件e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py",第 202 行,在 addsitedir 中添加包(站点目录,名称,known_paths)文件e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py",第 170 行,在 addpackage 中执行(行)文件<string>",第 1 行,在 <module> 中文件e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site-packages\win32\lib\pywin32_bootstrap.py",第 14 行,在 <module>也许在 site.getsitepackages() 中:AttributeError: 部分初始化的模块site"没有属性getsitepackages"(很可能是由于循环导入)

解决方案

注意:我从 [SO]:PyWin32 和 Python 3.8.0(@CristiFati 的回答),因为这是一个不同的问题.在进一步操作之前,您可能需要先检查一下.

经过一番挖掘,结果证明这是一个 VirtualEnv 错误(或者至少,我是这么看的,因为 VirtualEnvsite.py 不包含 getsitepackages - 尽管它存在于 Pythonsite.py 中,因为 v2.7).
有几个问题(是)围绕着这种缺席(例如 [GitHub]: pypa/virtualenv - site.getsitepackages() 缺失),但它们似乎没有修复就关闭了(其中许多是由于不活动).此外,[GitHub]:[WIP] 下一代 virtualenv(重写)是一个很大的重构,似乎没有解决它.

回顾:这个错误(在VirtualEnv上下文中):

  • 也适用于其他Python版本(我可以确认v3.7.3(32位))
  • 每次解释器启动时都会发生

总而言之,不要在 (VirtualEnv) 虚拟环境中安装 PyWin32 226,因为它们最终会处于损坏状态!

PyWin32(官方 .whls)兼容性表"(没有检查 Anaconda(或其他此类工具)):

已提交 [GitHub]:mhammond/pywin32 - 虚拟环境的解决方法 (VirtualEnv)(合并于 20191114).在本地应用更改(有关如何执行的详细信息,请查看参考问题),修复它:

<块引用>

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q058805040]>蝙蝠*** 当粘贴到 StackOverflow(或其他)页面时,设置更短的提示以更好地适应 ***[提示]>".\venv_py_064_030800\Scripts\python.exe" -m pip list套餐版本---------- -------点 19.3.1pywin32 226设置工具 41.6.0轮 0.33.6[提示]>".\venv_py_064_030800\Scripts\python.exe" -c "导入 win32api"[提示]>


<小时>

更新#0

[PyPI]:pywin32 227(解决了这个问题问题),发表于 20191114

[PyPI]: pywin32 226 has been released on 20191110.
It works on most of the Python installations (e.g. works on the official versions downloaded from Python), but not on virtual environments (e.g. created with VirtualEnv (v16.7.7), and (based on further research) Python's venv).
I used Python v3.8.0 and v3.7.3 as lab rats. Here's the output for the former:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q058805040]> "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\Scripts\python.exe" -c "import win32api"
Fatal Python error: init_import_size: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py", line 769, in <module>
    main()
  File "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py", line 746, in main
    paths_in_sys = addsitepackages(paths_in_sys)
  File "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py", line 279, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py", line 202, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site.py", line 170, in addpackage
    exec(line)
  File "<string>", line 1, in <module>
  File "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\lib\site-packages\win32\lib\pywin32_bootstrap.py", line 14, in <module>
    for maybe in site.getsitepackages():
AttributeError: partially initialized module 'site' has no attribute 'getsitepackages' (most likely due to a circular import)

解决方案

Note: I branched this answer (and also the question) from [SO]: PyWin32 and Python 3.8.0 (@CristiFati's answer), as it's a different issue. You might want to check that one before going further.

After some digging, it turns out it's a VirtualEnv bug (or at least, that's how I see things, because VirtualEnv's site.py doesn't contain getsitepackages - although it was present in Python's site.py since v2.7).
There are several issues (that were) open revolving this absence (e.g. [GitHub]: pypa/virtualenv - site.getsitepackages() missing), but they appear to be closed without a fix (many of them due to inactivity). Also, [GitHub]: [WIP] The next-gen virtualenv (rewrite) which is a big refactor, doesn't seem to address it.

Recap: this error (in VirtualEnv context):

  • Also applies to other Python versions (I can confirm for v3.7.3 (32bit))
  • Happens every time when the interpreter starts

In conclusion, do not install PyWin32 226 in (VirtualEnv) virtual environments, as they will end up in a broken state!

PyWin32 (official .whls) "compatibility table" (didn't check Anaconda (or other such tools)):

Submitted [GitHub]: mhammond/pywin32 - Workaround for virtual environments (VirtualEnv) (merged on 20191114). Applying the changes locally (check the referenced question for details on how to do it), fixed it:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q058805040]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> ".\venv_py_064_030800\Scripts\python.exe" -m pip list
Package    Version
---------- -------
pip        19.3.1
pywin32    226
setuptools 41.6.0
wheel      0.33.6

[prompt]> ".\venv_py_064_030800\Scripts\python.exe" -c "import win32api"

[prompt]>



Update #0

[PyPI]: pywin32 227 (which addresses this issue), was published on 20191114!

这篇关于PyWin32 (226) 和虚拟环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆