Skip to content

Fix pywin32_bootstrap.py to handle early initialization environments #2596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Devel-Dan
Copy link

Fix pywin32_bootstrap.py to handle early initialization environments

This patch adds robust error handling to pywin32_bootstrap.py to address
issues occurring during very early Python initialization, particularly
in VS Code's Python extension (Pylance/IntelliSense).

Problem:
During early initialization when .pth files are processed, neither
os.add_dll_directory() nor even os.environ['PATH'] may be fully available
yet, causing errors like:

  • AttributeError: module 'os' has no attribute 'add_dll_directory'
  • KeyError: 'PATH'

Solution:

  • Wrap the os.add_dll_directory() call in a try/except block
  • Add fallback to PATH environment manipulation with proper error handling
  • Add final fallback that silently passes if all else fails

@Devel-Dan Devel-Dan force-pushed the fix-pywin32-bootstrap-early-init branch from 33f3e9c to 03cd054 Compare May 2, 2025 05:11
Copy link
Collaborator

@Avasam Avasam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] address issues occurring during very early Python initialization, particularly in VS Code's Python extension (Pylance/IntelliSense).
[...] neither os.add_dll_directory() nor even os.environ['PATH'] may be fully available yet

Can you give an example (MRE) demonstrating this, (and fixed by this patch)? Especially if it fixes an existing issue in https://github.com/mhammond/pywin32/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20(path%20OR%20DLL)

A test would go a long way in demonstrating the use case(s) and preventing regressions. For example, very similar code (which also made sure to avoid duplicating the PATH addition) was removed in https://github.com/mhammond/pywin32/pull/2207/files#diff-a9afdb98cf78a938058e200c4603b1c1fb5465202d6f69ee23458ce5e7e3b668 as an "obsolete code path" as part of dropping support for Python 3.7. (since add_dll_directory is expected to always exist on Python >= 3.8)

Could you confirm on which version of Python you are trying to run this as well?

@Devel-Dan
Copy link
Author

Devel-Dan commented May 2, 2025

[...] address issues occurring during very early Python initialization, particularly in VS Code's Python extension (Pylance/IntelliSense).
[...] neither os.add_dll_directory() nor even os.environ['PATH'] may be fully available yet

Can you give an example (MRE) demonstrating this, (and fixed by this patch)? Especially if it fixes an existing issue in https://github.com/mhammond/pywin32/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20(path%20OR%20DLL)

A test would go a long way in demonstrating the use case(s) and preventing regressions. For example, very similar code (which also made sure to avoid duplicating the PATH addition) was removed in https://github.com/mhammond/pywin32/pull/2207/files#diff-a9afdb98cf78a938058e200c4603b1c1fb5465202d6f69ee23458ce5e7e3b668 as an "obsolete code path" as part of dropping support for Python 3.7. (since add_dll_directory is expected to always exist on Python >= 3.8)

Could you confirm on which version of Python you are trying to run this as well?

I was using python 3.13.1 I don't think the version matters here though

  1. install python 3.8+
  2. Install pywin32: pip install pywin32
  3. Install VS Code with the Python extension
  4. Create a new Python project
  5. Open VS Code's output panel (View -> Output) and select "Python Language Server" from the dropdown
  6. Look for these errors in the log when VS Code initializes:
2025-05-01 21:04:56.372 [info] [Info  - 9:04:56 PM] (74896) Auto-excluding **/.*
2025-05-01 21:04:56.625 [info] [Info  - 9:04:56 PM] (74896) Assuming Python version 3.13.1.final.0
2025-05-01 21:04:57.091 [info] [Info  - 9:04:57 PM] (74896) Found 15 source files
2025-05-01 21:04:57.113 [info] [Info  - 9:04:57 PM] (74896) BG: Priority queue background worker(6) root directory: file:///c%3A/Users/User/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist
2025-05-01 21:04:57.113 [info] [Info  - 9:04:57 PM] (74896) BG: Priority queue background worker(6) started
2025-05-01 21:04:57.115 [info] [Info  - 9:04:57 PM] (74896) Setting environmentName for service "vivio": "3.13.1 (global)"
2025-05-01 21:04:57.116 [info] [Info  - 9:04:57 PM] (74896) Setting pythonPath for service "vivio": "C:\Python313\python.exe"
2025-05-01 21:04:57.117 [info] [Info  - 9:04:57 PM] (74896) No include entries specified; assuming c:\Users\User\vivio
2025-05-01 21:04:57.118 [info] [Info  - 9:04:57 PM] (74896) Auto-excluding **/node_modules
2025-05-01 21:04:57.118 [info] [Info  - 9:04:57 PM] (74896) Auto-excluding **/__pycache__
2025-05-01 21:04:57.119 [info] [Info  - 9:04:57 PM] (74896) Auto-excluding **/.*
2025-05-01 21:04:57.337 [info] [Info  - 9:04:57 PM] (74896) Assuming Python version 3.13.1.final.0
2025-05-01 21:04:57.756 [info] [Info  - 9:04:57 PM] (74896) Found 1 source file
2025-05-01 21:04:59.083 [info] [Info  - 9:04:59 PM] (74896) BG: Indexer background runner(7) root directory: file:///c%3A/Users/User/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist (index)
2025-05-01 21:04:59.083 [info] [Info  - 9:04:59 PM] (74896) BG: Indexing(7) started
2025-05-01 21:04:59.974 [info] Error parsing pth file: Error processing line 7 of /c:\Python313\Lib\site-packages/pywin32.pth:


2025-05-01 21:04:59.974 [info] Error parsing pth file: 

Error parsing pth file:   Traceback (most recent call last):

Error parsing pth file:     File "<frozen site>", line 206, in addpackage


2025-05-01 21:04:59.975 [info] Error parsing pth file:     File "<string>", line 1, in <module>

Error parsing pth file:     File "/$c$/Python313\Lib\site-packages\win32\lib/pywin32_bootstrap.py", line 20, in <module>

Error parsing pth file:       os.add_dll_directory(path)


2025-05-01 21:04:59.975 [info] Error parsing pth file:       ^^^^^^^^^^^^^^^^^^^^

Error parsing pth file:   AttributeError: module 'os' has no attribute 'add_dll_directory'


2025-05-01 21:04:59.975 [info] Error parsing pth file: 
Remainder of file ignored
Error parsing pth file: 

I tried doing what that attribute was performing and adding to "PATH" but i got this

2025-05-01 21:28:17.473 [info] [Info  - 9:28:17 PM] (73704) BG: Indexing finished(7).
2025-05-01 21:28:17.843 [info] Error parsing pth file: Error processing line 7 of /c:\Python313\Lib\site-packages/pywin32.pth:

2025-05-01 21:28:17.843 [info] Error parsing pth file: 

Error parsing pth file:   Traceback (most recent call last):

Error parsing pth file:     File "<frozen site>", line 206, in addpackage

2025-05-01 21:28:17.843 [info] Error parsing pth file:     File "<string>", line 1, in <module>

Error parsing pth file:     File "/$c$/Python313\Lib\site-packages\win32\lib/pywin32_bootstrap.py", line 27, in <module>

2025-05-01 21:28:17.843 [info] Error parsing pth file:       os.environ['PATH'] = path + os.pathsep + os.environ['PATH']

Error parsing pth file:                                                ~~~~~~~~~~^^^^^^^^

Error parsing pth file:     File "<frozen os>", line 716, in getitem

2025-05-01 21:28:17.844 [info] Error parsing pth file:   KeyError: 'PATH'

Error parsing pth file: 
Remainder of file ignored
Error parsing pth file:

It seems like this issue is occurring because VS Code is processing this .pth file too early? - before the environment is fully initialized.

@Avasam
Copy link
Collaborator

Avasam commented May 2, 2025

Thanks for the additional details.

I'm still trying to at least replicate your issue. So it can be checked for in the future, or seeing if it's a Pylance issue that can be raised there (because it's a really odd issue)

My setup:
Python 3.13 with pywin32 310 installed in a virtual environment. Command Python: Restart Language Server with a Python file open on a project that uses pywin32. (I also tried Developer: Reload Window and Developer: Restart Extension Host. Even completely restarting VSCode)

Pylance 2025.4.102 (pre-release)

2025-05-02 18:36:20.238 [info] (Client) Failed to register MCP configuration provider: Error: Extension 'ms-python.vscode-pylance' CANNOT use API proposal: mcpConfigurationProvider.
Its package.json#enabledApiProposals-property declares:  but NOT mcpConfigurationProvider.
 The missing proposal MUST be added and you must start in extension development mode or use the following command line switch: --enable-proposed-api ms-python.vscode-pylance
2025-05-02 18:36:20.294 [info] [Info  - 6:36:20 PM] Server process exited successfully
2025-05-02 18:36:20.764 [info] (Client) Pylance async client (2025.4.102) started with python extension (2025.4.0)
2025-05-02 18:36:20.984 [info] [Info  - 6:36:20 PM] (32232) Server root directory: file:///c%3A/Users/Avasam/.vscode/extensions/ms-python.vscode-pylance-2025.4.102/dist
2025-05-02 18:36:20.986 [info] [Info  - 6:36:20 PM] (32232) Pylance language server 2025.4.102 (pyright version 1.1.400, commit b962ffc2) starting
2025-05-02 18:36:20.992 [info] [Info  - 6:36:20 PM] (32232) Starting service instance "AutoSplit" for workspace "e:\Users\Avasam\Documents\Git\AutoSplit"
2025-05-02 18:36:21.033 [info] [Info  - 6:36:21 PM] (32232) Loading pyproject.toml file at e:\Users\Avasam\Documents\Git\AutoSplit\pyproject.toml
2025-05-02 18:36:21.035 [info] [Info  - 6:36:21 PM] (32232) Setting pythonPath for service "AutoSplit": "E:\Users\Avasam\Documents\Git\AutoSplit\.venv\Scripts\python.exe"
2025-05-02 18:36:21.036 [info] [Info  - 6:36:21 PM] (32232) No include entries specified; assuming e:\Users\Avasam\Documents\Git\AutoSplit
2025-05-02 18:36:21.488 [info] [Info  - 6:36:21 PM] (32232) Found 22 source files
2025-05-02 18:36:21.506 [info] [Info  - 6:36:21 PM] (32232) Loading pyproject.toml file at e:\Users\Avasam\Documents\Git\AutoSplit\pyproject.toml
2025-05-02 18:36:21.507 [info] [Info  - 6:36:21 PM] (32232) Setting pythonPath for service "AutoSplit": "E:\Users\Avasam\Documents\Git\AutoSplit\.venv\Scripts\python.exe"
2025-05-02 18:36:21.507 [info] [Info  - 6:36:21 PM] (32232) No include entries specified; assuming e:\Users\Avasam\Documents\Git\AutoSplit
2025-05-02 18:36:21.860 [info] [Info  - 6:36:21 PM] (32232) Found 22 source files
2025-05-02 18:36:22.049 [info] [Info  - 6:36:22 PM] (32232) BG: Priority queue background worker(2) root directory: file:///c%3A/Users/Avasam/.vscode/extensions/ms-python.vscode-pylance-2025.4.102/dist
2025-05-02 18:36:22.050 [info] [Info  - 6:36:22 PM] (32232) BG: Priority queue background worker(2) started
2025-05-02 18:36:22.420 [info] [Info  - 6:36:22 PM] (32232) BG: Indexer background runner(3) root directory: file:///c%3A/Users/Avasam/.vscode/extensions/ms-python.vscode-pylance-2025.4.102/dist (index)
2025-05-02 18:36:22.420 [info] [Info  - 6:36:22 PM] (32232) BG: Indexing(3) started
2025-05-02 18:36:23.196 [info] [Info  - 6:36:23 PM] (32232) BG: scanned(3) 133 files over 1 exec env
2025-05-02 18:36:23.450 [info] [Info  - 6:36:23 PM] (32232) BG: indexed(3) 133 files over 1 exec env
2025-05-02 18:36:23.735 [info] [Info  - 6:36:23 PM] (32232) BG: Indexing finished(3).

Pylance 2025.4.1

2025-05-02 18:41:12.354 [info] [Info  - 6:41:12 PM] Server process exited successfully
2025-05-02 18:41:12.730 [info] [Info  - 6:41:12 PM] (16312) Server root directory: file:///c%3A/Users/Avasam/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist
2025-05-02 18:41:12.731 [info] [Info  - 6:41:12 PM] (16312) Pylance language server 2025.4.1 (pyright version 1.1.398, commit 4f24eccc) starting
2025-05-02 18:41:12.735 [info] [Info  - 6:41:12 PM] (16312) Starting service instance "AutoSplit" for workspace "e:\Users\Avasam\Documents\Git\AutoSplit"
2025-05-02 18:41:12.769 [info] [Info  - 6:41:12 PM] (16312) Loading pyproject.toml file at e:\Users\Avasam\Documents\Git\AutoSplit\pyproject.toml
2025-05-02 18:41:12.772 [info] [Info  - 6:41:12 PM] (16312) Setting pythonPath for service "AutoSplit": "E:\Users\Avasam\Documents\Git\AutoSplit\.venv\Scripts\python.exe"
2025-05-02 18:41:12.772 [info] [Info  - 6:41:12 PM] (16312) No include entries specified; assuming e:\Users\Avasam\Documents\Git\AutoSplit
2025-05-02 18:41:13.187 [info] [Info  - 6:41:13 PM] (16312) Found 22 source files
2025-05-02 18:41:13.208 [info] [Info  - 6:41:13 PM] (16312) Loading pyproject.toml file at e:\Users\Avasam\Documents\Git\AutoSplit\pyproject.toml
2025-05-02 18:41:13.209 [info] [Info  - 6:41:13 PM] (16312) Setting pythonPath for service "AutoSplit": "E:\Users\Avasam\Documents\Git\AutoSplit\.venv\Scripts\python.exe"
2025-05-02 18:41:13.211 [info] [Info  - 6:41:13 PM] (16312) No include entries specified; assuming e:\Users\Avasam\Documents\Git\AutoSplit
2025-05-02 18:41:13.595 [info] [Info  - 6:41:13 PM] (16312) Found 22 source files
2025-05-02 18:41:13.827 [info] [Info  - 6:41:13 PM] (16312) BG: Priority queue background worker(2) root directory: file:///c%3A/Users/Avasam/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist
2025-05-02 18:41:13.827 [info] [Info  - 6:41:13 PM] (16312) BG: Priority queue background worker(2) started
2025-05-02 18:41:14.099 [info] [Info  - 6:41:14 PM] (16312) BG: Indexer background runner(3) root directory: file:///c%3A/Users/Avasam/.vscode/extensions/ms-python.vscode-pylance-2025.4.1/dist (index)
2025-05-02 18:41:14.099 [info] [Info  - 6:41:14 PM] (16312) BG: Indexing(3) started
2025-05-02 18:41:14.873 [info] [Info  - 6:41:14 PM] (16312) BG: scanned(3) 133 files over 1 exec env
2025-05-02 18:41:15.153 [info] [Info  - 6:41:15 PM] (16312) BG: indexed(3) 133 files over 1 exec env
2025-05-02 18:41:15.357 [info] [Info  - 6:41:15 PM] (16312) BG: Indexing finished(3).
2025-05-02 18:41:22.574 [info] (Client) The existing extension didn't exit within 10 seconds. New instance will start, but you might encounter issues.
2025-05-02 18:41:22.574 [info] (Client) Pylance async client (2025.4.1) started with python extension (2025.4.0)

@Avasam Avasam requested review from Avasam and mhammond May 4, 2025 23:32
@Avasam
Copy link
Collaborator

Avasam commented May 4, 2025

Pinging @mhammond to see if you're ok re-introducing this code or would also prefer also understanding why Pylance even fails here.
Which I couldn't replicate, and really feels like a "this should never happen". (meaning we can't test for regressions or fixes)

@Devel-Dan
Copy link
Author

yeah the only difference with our tests is that I did not use a virtual environment, which is probably the fix here, I think I've seen solutions from other people saying this, just using conda for example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants