Issue
[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)
Solution
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)):
- "Normal" (official) Python installations:
- v226 is OK
- VirtualEnvs (and also Python's standard venvs):
- v225 is OK
- For Python 3.8.0, [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyWin32/v225 should be used
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!
Answered By - CristiFati Answer Checked By - Katrina (WPSolving Volunteer)