Title: Avoid loading `sys` and `platform` modules into namespace when running PYTHONSTARTUP in integrated terminal · Issue #25904 · microsoft/vscode-python · GitHub
Open Graph Title: Avoid loading `sys` and `platform` modules into namespace when running PYTHONSTARTUP in integrated terminal · Issue #25904 · microsoft/vscode-python
X Title: Avoid loading `sys` and `platform` modules into namespace when running PYTHONSTARTUP in integrated terminal · Issue #25904 · microsoft/vscode-python
Description: The following way is the recommended method to run PYTHONSTARTUP in a console, the type of which inherits from code.InteractiveConsole: if path := os.getenv('PYTHONSTARTUP'): with tokenize.open(path) as file: sys.audit('cpython.run_start...
Open Graph Description: The following way is the recommended method to run PYTHONSTARTUP in a console, the type of which inherits from code.InteractiveConsole: if path := os.getenv('PYTHONSTARTUP'): with tokenize.open(pat...
X Description: The following way is the recommended method to run PYTHONSTARTUP in a console, the type of which inherits from code.InteractiveConsole: if path := os.getenv('PYTHONSTARTUP'): with tokenize....
Opengraph URL: https://github.com/microsoft/vscode-python/issues/25904
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Avoid loading `sys` and `platform` modules into namespace when running PYTHONSTARTUP in integrated terminal","articleBody":"\u003c!-- ⚠️⚠️ Do Not Delete This! feature_request_template ⚠️⚠️ -->\n\u003c!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ -->\n\u003c!-- Please search existing issues to avoid creating duplicates. -->\nThe following way is the recommended method to run PYTHONSTARTUP in a console, the type of which inherits from `code.InteractiveConsole`:\n\n```python\nif path := os.getenv('PYTHONSTARTUP'):\n with tokenize.open(path) as file:\n sys.audit('cpython.run_startup', path)\n exec(compile(file.read(), path, 'exec'), console.locals)\n```\n\nbut this loads the `platform` and `sys` modules into the locals dictionary (`readline` on non-windows as well), polluting the namespace and making for a strange user experience (e.g. when one prints the contents of globals during debugging or when forgetting to import modules works anyways)\n\nFrom [`pythonrc.py`](https://github.com/microsoft/vscode-python/blob/main/python_files/pythonrc.py):\n\n```python\nimport platform\nimport sys\n\nif sys.platform != \"win32\":\n import readline\n\noriginal_ps1 = \">>> \"\nis_wsl = \"microsoft-standard-WSL\" in platform.release()\n\n\nclass REPLHooks:\n def __init__(self):\n self.global_exit = None\n self.failure_flag = False\n self.original_excepthook = sys.excepthook\n self.original_displayhook = sys.displayhook\n sys.excepthook = self.my_excepthook\n sys.displayhook = self.my_displayhook\n\n def my_displayhook(self, value):\n if value is None:\n self.failure_flag = False\n\n self.original_displayhook(value)\n\n def my_excepthook(self, type_, value, traceback):\n self.global_exit = value\n self.failure_flag = True\n\n self.original_excepthook(type_, value, traceback)\n\n\ndef get_last_command():\n # Get the last history item\n last_command = \"\"\n if sys.platform != \"win32\":\n last_command = readline.get_history_item(readline.get_current_history_length())\n\n return last_command\n\n\nclass PS1:\n hooks = REPLHooks()\n sys.excepthook = hooks.my_excepthook\n sys.displayhook = hooks.my_displayhook\n\n # str will get called for every prompt with exit code to show success/failure\n def __str__(self):\n exit_code = int(bool(self.hooks.failure_flag))\n self.hooks.failure_flag = False\n # Guide following official VS Code doc for shell integration sequence:\n result = \"\"\n # For non-windows allow recent_command history.\n if sys.platform != \"win32\":\n result = \"{soh}{command_executed}{command_line}{command_finished}{prompt_started}{stx}{prompt}{soh}{command_start}{stx}\".format(\n soh=\"\\001\",\n stx=\"\\002\",\n command_executed=\"\\x1b]633;C\\x07\",\n command_line=\"\\x1b]633;E;\" + str(get_last_command()) + \"\\x07\",\n command_finished=\"\\x1b]633;D;\" + str(exit_code) + \"\\x07\",\n prompt_started=\"\\x1b]633;A\\x07\",\n prompt=original_ps1,\n command_start=\"\\x1b]633;B\\x07\",\n )\n else:\n result = \"{command_finished}{prompt_started}{prompt}{command_start}{command_executed}\".format(\n command_finished=\"\\x1b]633;D;\" + str(exit_code) + \"\\x07\",\n prompt_started=\"\\x1b]633;A\\x07\",\n prompt=original_ps1,\n command_start=\"\\x1b]633;B\\x07\",\n command_executed=\"\\x1b]633;C\\x07\",\n )\n\n # result = f\"{chr(27)}]633;D;{exit_code}{chr(7)}{chr(27)}]633;A{chr(7)}{original_ps1}{chr(27)}]633;B{chr(7)}{chr(27)}]633;C{chr(7)}\"\n\n return result\n\n def __repr__(self):\n return \"\u003cCustom PS1 for VS Code Python Shell Integration>\"\n\n\nif sys.platform != \"win32\" and (not is_wsl):\n sys.ps1 = PS1()\n\nif sys.platform == \"darwin\":\n print(\"Cmd click to launch VS Code Native REPL\")\nelse:\n print(\"Ctrl click to launch VS Code Native REPL\")\n```\n\u003c!-- Describe the feature you'd like. -->\nI propose to add the following at the end of that file:\n\n```python\ndel platform, sys\nif sys.platform != 'win32': del readline\n```","author":{"url":"https://github.com/jonathandung","@type":"Person","name":"jonathandung"},"datePublished":"2026-04-09T12:35:59Z","url":"https://github.com/microsoft/vscode-python/issues/25904"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:691f1734-41cc-fafa-2050-c3dcae08b9ac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C9C4:30751:1BBD82:2730D1:6AFDFBED |
| html-safe-nonce | 1a587ff9af64df639528bb4a5a967f8fdea5a60fb00b6adb5784fa415f3248c3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOUM0OjMwNzUxOjFCQkQ4MjoyNzMwRDE6NkFGREZCRUQiLCJ2aXNpdG9yX2lkIjoiMzk3OTE1MjUyODU0NDQ5NjYyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c314f0e56495eb5103d056eadcf98a5b267fabb4392c19f7aac2e5c9607bd4cf |
| hovercard-subject-tag | issue:4232233143 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/vscode-python/25904/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e3bdf0e2e1f5b7680b300b1fb120b46bc63cdc8f5ae15c97a3ce596658e0d975/microsoft/vscode-python/issues/25904 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e3bdf0e2e1f5b7680b300b1fb120b46bc63cdc8f5ae15c97a3ce596658e0d975/microsoft/vscode-python/issues/25904 |
| og:image:alt | The following way is the recommended method to run PYTHONSTARTUP in a console, the type of which inherits from code.InteractiveConsole: if path := os.getenv('PYTHONSTARTUP'): with tokenize.open(pat... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jonathandung |
| hostname | github.com |
| expected-hostname | github.com |
| None | ee3343385a977757dd145b28fabf4a119780f2615a5a36c5a89f66c9b3b5616d |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/vscode-python git https://github.com/microsoft/vscode-python.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 106462765 |
| octolytics-dimension-repository_nwo | microsoft/vscode-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 49946255 |
| octolytics-dimension-repository_parent_nwo | DonJayamanne/pythonVSCode |
| octolytics-dimension-repository_network_root_id | 49946255 |
| octolytics-dimension-repository_network_root_nwo | DonJayamanne/pythonVSCode |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 43ae91995f6e2d5ff7a491f3de438f1f723adeaa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width