René's URL Explorer Experiment


Title: sys._base_executable resolves symlinks too eagerly, resulting in undesirable venv from venv behaviour · Issue #128670 · python/cpython · GitHub

Open Graph Title: sys._base_executable resolves symlinks too eagerly, resulting in undesirable venv from venv behaviour · Issue #128670 · python/cpython

X Title: sys._base_executable resolves symlinks too eagerly, resulting in undesirable venv from venv behaviour · Issue #128670 · python/cpython

Description: Bug report Bug description: In #106045, the use case of putting a symlink in /usr/local/bin to a Python binary from another prefix was highlighted. For homebrew, this use case is used with the idea that the symlink is the public interfac...

Open Graph Description: Bug report Bug description: In #106045, the use case of putting a symlink in /usr/local/bin to a Python binary from another prefix was highlighted. For homebrew, this use case is used with the idea...

X Description: Bug report Bug description: In #106045, the use case of putting a symlink in /usr/local/bin to a Python binary from another prefix was highlighted. For homebrew, this use case is used with the idea...

Opengraph URL: https://github.com/python/cpython/issues/128670

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"sys._base_executable resolves symlinks too eagerly, resulting in undesirable venv from venv behaviour","articleBody":"# Bug report\n\n### Bug description:\n\nIn #106045, the use case of putting a symlink in `/usr/local/bin` to a Python binary from another prefix was highlighted. For homebrew, this use case is used with the idea that the symlink is the public interface/location, and the prefix where it is actually installed is an implementation detail (and can change over time) (more details in https://github.com/astral-sh/uv/issues/1640).\n\n#106045 seems to have now been resolved by #127974 ([I wrote a test for that](https://github.com/python/cpython/pull/127974#issuecomment-2579501759)). The originating issue proposed a solution which would fix the problem with the broken virtual environment in that case, but would actually break the aforementioned homebrew example by eagerly resolving the symlinked executable and using the resolved path to determine the `home` value in `pyvenv.cfg` - thereby exposing the \"internal\" prefix into `pyvenv.cfg`.\n\nThe problem I am reporting here is that\n * because `sys._base_executable` eagerly resolves symlinks in [`getpath.py`](https://github.com/python/cpython/blob/b2adf556747d080f04b53ba4063b627c2dbe41d1/Modules/getpath.py#L388)\n * and `venv`, `virtualenv` and `uv` all use `sys._base_executable` to determine the `home` location\n  * then when you take a virtual environment _from another virtual environment_ in the aforementioned setup, the second virtual environment's `home` location will be resolved, whereas the first will not be. Thereby bleeding the implementation detail once more.\n\nIn `uv`, the behaviour was solidified in https://github.com/astral-sh/uv/pull/8433 to avoid exposing the internal prefix in the venv. Note that `virtualenv` is (accidentally?) now exposing the internal prefix even on the first virtual environment https://github.com/pypa/virtualenv/issues/2770.\n\n\nTo reproduce this:\n\n```\n$ mkdir -p /tmp/public/bin/\n$ ln -s /usr/local/bin/python3.14 /tmp/public/bin/\n\n$ /tmp/public/bin/python3.14 -m venv /tmp/venv1\n$ /tmp/venv1/bin/python -m venv /tmp/venv2\n\n$ cat /tmp/venv1/pyvenv.cfg \nhome = /tmp/public/bin\ninclude-system-site-packages = false\nversion = 3.14.0\nexecutable = /usr/local/bin/python3.14\ncommand = /tmp/public/bin/python3.14 -m venv /tmp/venv1\n\n$ cat /tmp/venv2/pyvenv.cfg \nhome = /usr/local/bin\ninclude-system-site-packages = false\nversion = 3.14.0\nexecutable = /usr/local/bin/python3.14\ncommand = /tmp/venv1/bin/python -m venv /tmp/venv2\n```\n\nAnd observe that the second venv's home is not `/tmp/public/bin` but the \"internal detail\" one.\n\nA quick test for getpath, and a complete test for venv are included below (both failing):\n\n```diff\ndiff --git a/Lib/test/test_getpath.py b/Lib/test/test_getpath.py\nindex f86df9d0d03..1c2e2a0b3fc 100644\n--- a/Lib/test/test_getpath.py\n+++ b/Lib/test/test_getpath.py\n@@ -864,6 +864,55 @@ def test_PYTHONHOME_in_venv(self):\n         actual = getpath(ns, expected)\n         self.assertEqual(expected, actual)\n \n+    def test_venv_w_symlinked_base_executable(self):\n+        \"\"\"\n+        If we symlink the base executable, and point to it via home in pyvenv.cfg,\n+        we should have it as sys.executable (and sys.prefix should be the resolved location)\n+        \"\"\"\n+        ns = MockPosixNamespace(\n+            argv0=\"/venv/bin/python3\",\n+            PREFIX=\"/some/_internal/prefix\",\n+        )\n+        # Setup venv\n+        ns.add_known_xfile(\"/venv/bin/python3\")\n+        ns.add_known_xfile(\"/usr/local/bin/python3\")\n+        ns.add_known_xfile(\"/some/_internal/prefix/bin/python3\")\n+\n+        ns.add_known_file(\"/venv/pyvenv.cfg\", [\n+            # The published based executable location is /usr/local/bin - we don't want to\n+            # expose /some/internal/directory (this location can change under our feet)\n+            r\"home = /usr/local/bin\"\n+        ])\n+        ns.add_known_link(\"/venv/bin/python3\", \"/usr/local/bin/python3\")\n+        ns.add_known_link(\"/usr/local/bin/python3\", \"/some/_internal/prefix/bin/python3\")\n+\n+        ns.add_known_file(\"/some/_internal/prefix/lib/python9.8/os.py\")\n+        ns.add_known_dir(\"/some/_internal/prefix/lib/python9.8/lib-dynload\")\n+\n+        # Put a file completely outside of /usr/local to validate that the issue\n+        # in https://github.com/python/cpython/issues/106045 is resolved.\n+        ns.add_known_dir(\"/usr/lib/python9.8/lib-dynload\")\n+\n+        expected = dict(\n+            executable=\"/venv/bin/python3\",\n+            prefix=\"/venv\",\n+            exec_prefix=\"/venv\",\n+            base_prefix=\"/some/_internal/prefix\",\n+            base_exec_prefix=\"/some/_internal/prefix\",\n+            # It is important to maintain the link to the original executable, as this\n+            # is used when creating a new virtual environment (which should also have home\n+            # set to /usr/local/bin to avoid bleeding the internal path to the venv)\n+            base_executable=\"/usr/bin/python3\",\n+            module_search_paths_set=1,\n+            module_search_paths=[\n+                \"/some/_internal/prefix/lib/python98.zip\",\n+                \"/some/_internal/prefix/lib/python9.8\",\n+                \"/some/_internal/prefix/lib/python9.8/lib-dynload\",\n+            ],\n+        )\n+        actual = getpath(ns, expected)\n+        self.assertEqual(expected, actual)\n+\n # ******************************************************************************\n \n DEFAULT_NAMESPACE = dict(\ndiff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py\nindex 0b09010c69d..bd1b19a9c15 100644\n--- a/Lib/test/test_venv.py\n+++ b/Lib/test/test_venv.py\n@@ -756,6 +756,53 @@ def test_zippath_from_non_installed_posix(self):\n         out, err = check_output(cmd)\n         self.assertTrue(zip_landmark.encode() in out)\n \n+    @unittest.skipIf(os.name == 'nt', 'not relevant on Windows')\n+    @requireVenvCreate\n+    def test_venv_from_venv_with_symlink(self):\n+        \"\"\"\n+        Test that we can create a venv from a venv using a base Python that is\n+        a symlink, without exposing the location of the symlink in pyvenv.cfg.\n+        \"\"\"\n+        rmtree(self.env_dir)\n+        public_prefix = os.path.realpath(tempfile.mkdtemp())\n+        self.addCleanup(rmtree, public_prefix)\n+        public_bin_dir = os.path.join(public_prefix, 'bin')\n+        os.mkdir(public_bin_dir)\n+        public_exe = os.path.join(public_bin_dir, self.exe)\n+        os.symlink(sys.executable, public_exe)\n+        cmd = [public_exe,\n+               \"-m\",\n+               \"venv\",\n+               \"--without-pip\",\n+               \"--without-scm-ignore-files\",\n+               self.env_dir]\n+\n+        subprocess.check_call(cmd)\n+\n+        # Verify that we don't expose the internal prefix to the first venv config:\n+        contents = (pathlib.Path(self.env_dir) / 'pyvenv.cfg').read_text()\n+        self.assertIn(f'home = {public_bin_dir}\\n', contents)\n+\n+        # Now use the venv to make another, and assert that the internal env is\n+        # also not exposed there.\n+        second_venv = os.path.realpath(tempfile.mkdtemp())\n+        self.addCleanup(rmtree, second_venv)\n+\n+        cmd = [os.path.join(self.env_dir, 'bin', 'python3'),\n+               \"-m\",\n+               \"venv\",\n+               \"--without-pip\",\n+               \"--without-scm-ignore-files\",\n+               second_venv]\n+\n+        subprocess.check_call(cmd)\n+\n+        contents = (pathlib.Path(second_venv) / 'pyvenv.cfg').read_text()\n+        self.assertIn(f'home = {public_bin_dir}\\n', contents)\n+\n     @requireVenvCreate\n     def test_activate_shell_script_has_no_dos_newlines(self):\n         \"\"\"\n```\n\ncc @FFY00 following on from our conversation in https://github.com/python/cpython/pull/127974#issuecomment-2579408023 (I would love to get a commit for the tests, if not the fix :stuck_out_tongue_winking_eye:).\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-128913\n* gh-150999\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/pelson","@type":"Person","name":"pelson"},"datePublished":"2025-01-09T10:57:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/128670/cpython/issues/128670"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:2dcd9da5-2ed6-0610-39dc-edbfc8f5249b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id98F0:8AE8C:1CCE70A:2812C0A:6A4EC516
html-safe-nonce5275baf968b836cd111de7b8205c482bc4bf9ff7b04b46bd26aeeff2979b07ee
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OEYwOjhBRThDOjFDQ0U3MEE6MjgxMkMwQTo2QTRFQzUxNiIsInZpc2l0b3JfaWQiOiIyMTcyNzc5OTQ5NDY3MDg3NTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac88d96d11cf3e19eed898a81eda7db6b6caafa830e6e3eb30733dda24b60ea034
hovercard-subject-tagissue:2777499901
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/128670/issue_layout
twitter:imagehttps://opengraph.githubassets.com/73b91f14752a6f756e7c54bd45118daec994b9f65b4bbd39bda8dda12a0c9884/python/cpython/issues/128670
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/73b91f14752a6f756e7c54bd45118daec994b9f65b4bbd39bda8dda12a0c9884/python/cpython/issues/128670
og:image:altBug report Bug description: In #106045, the use case of putting a symlink in /usr/local/bin to a Python binary from another prefix was highlighted. For homebrew, this use case is used with the idea...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamepelson
hostnamegithub.com
expected-hostnamegithub.com
None41b6ab3ba6d20a71766ac245b5a4a94c6fc672a9cd4da7d44c1b33ab8bf6a21c
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasee6a744804e8e70f97b4d5a18a94dcc63db22f97a
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/128670#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F128670
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F128670
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/128670
Reloadhttps://github.com/python/cpython/issues/128670
Reloadhttps://github.com/python/cpython/issues/128670
Please reload this pagehttps://github.com/python/cpython/issues/128670
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/128670
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 34.8k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.6k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
sys._base_executable resolves symlinks too eagerly, resulting in undesirable venv from venv behaviourhttps://github.com/python/cpython/issues/128670#top
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-installationhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-installation%22
topic-venvRelated to the venv modulehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-venv%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/pelson
pelsonhttps://github.com/pelson
on Jan 9, 2025https://github.com/python/cpython/issues/128670#issue-2777499901
#106045https://github.com/python/cpython/issues/106045
astral-sh/uv#1640https://github.com/astral-sh/uv/issues/1640
#106045https://github.com/python/cpython/issues/106045
#127974https://github.com/python/cpython/pull/127974
I wrote a test for thathttps://github.com/python/cpython/pull/127974#issuecomment-2579501759
getpath.pyhttps://github.com/python/cpython/blob/b2adf556747d080f04b53ba4063b627c2dbe41d1/Modules/getpath.py#L388
astral-sh/uv#8433https://github.com/astral-sh/uv/pull/8433
pypa/virtualenv#2770https://github.com/pypa/virtualenv/issues/2770
@FFY00https://github.com/FFY00
#127974 (comment)https://github.com/python/cpython/pull/127974#issuecomment-2579408023
gh-128670: Use the pyvenv.cfg home location in getpath when the default executable points to the same realpath as the running executable #128913https://github.com/python/cpython/pull/128913
gh-128670: keep the public base executable when creating a venv from a venv #150999https://github.com/python/cpython/pull/150999
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-installationhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-installation%22
topic-venvRelated to the venv modulehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-venv%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.