Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

python - PyWin32 (226) and virtual environments

[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:WorkDevStackOverflowq058805040]> "e:WorkDevVEnvspy_064_03.08.00_test0Scriptspython.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:WorkDevVEnvspy_064_03.08.00_test0libsite.py", line 769, in <module>
    main()
  File "e:WorkDevVEnvspy_064_03.08.00_test0libsite.py", line 746, in main
    paths_in_sys = addsitepackages(paths_in_sys)
  File "e:WorkDevVEnvspy_064_03.08.00_test0libsite.py", line 279, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "e:WorkDevVEnvspy_064_03.08.00_test0libsite.py", line 202, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "e:WorkDevVEnvspy_064_03.08.00_test0libsite.py", line 170, in addpackage
    exec(line)
  File "<string>", line 1, in <module>
  File "e:WorkDevVEnvspy_064_03.08.00_test0libsite-packageswin32libpywin32_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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:WorkDevStackOverflowq058805040]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

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

[prompt]> ".venv_py_064_030800Scriptspython.exe" -c "import win32api"

[prompt]>



Update #0

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...