GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-124241: try reading `/proc/self/exe` to determine `sys.executable` by FFY00 · Pull Request #145486 · python/cpython

Conversation

@FFY00

@FFY00 FFY00 commented Mar 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
$ bash -c "exec -a echo $(pwd)/python -c 'import sys; print(sys.executable)'"
/home/anubis/git/cpython/python
  • Issue: sys.executable is sometimes wrong #124241

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@FFY00
pythonGH-124241: try reading /proc/self/exe to determine sys.executable
6ecb062
Signed-off-by: Filipe Laíns <[email protected]>
@bedevere-app bedevere-app Bot mentioned this pull request Mar 3, 2026
sys.executable is sometimes wrong #124241
Open
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 3, 2026
FFY00
FFY00 commented Mar 3, 2026
View reviewed changes
Comment thread Modules/getpath.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner reviewed Mar 4, 2026
View reviewed changes
Comment thread Modules/getpath.py Outdated
if not executable and os_name == 'posix':
# On Linux, try resolving the executable path via procfs
try:
executable = realpath('/proc/self/exe')

@vstinner vstinner Mar 4, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

realpath() returns /proc/self/exe unmodified if it cannot resolve the symlink. I would prefer using readlink() here. But it requires more work, since Modules/getpath.c doesn't implement it here. I can help implementing readlink() in getpath.c if you want.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@FFY00 FFY00 Mar 4, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Right, using readlink would also be my preference, I just went for this implementation for simplicity. We could move this to progname_to_dict (computes real_executable), but we should still try to honor program_name if it points to the same path.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
FFY00 added 3 commits March 4, 2026 18:24
@FFY00
Use readlink to read /proc/self/exe
065bed6
Signed-off-by: Filipe Laíns <[email protected]>
@FFY00
Add missing real_executable check
74c5658
Signed-off-by: Filipe Laíns <[email protected]>
@FFY00
Add test_bad_argv0_posix
e9b4131
Signed-off-by: Filipe Laíns <[email protected]>
@FFY00
FFY00 requested a review from vstinner March 4, 2026 18:38
@FFY00
Don't set real_executable if readlink returns an empty string
b70fd54
Signed-off-by: Filipe Laíns <[email protected]>
@FFY00

FFY00 commented Mar 4, 2026

Copy link
Copy Markdown
Member Author

Well, this is kind of messy. It seems that several tests rely on being able to feed real_executable a fake path. Since this is then used to search for the standard library directories, we cannot easily modify the behavior without causing breakage for some embedding users.

I'm gonna see if I can find a way to do this without breaking the module path calculation.

gpshead reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead

gpshead commented Apr 11, 2026

Copy link
Copy Markdown
Member

Indeed. sys.executable cannot be assumed to exist or simply be set to /proc/self/exe -> 's symlink target for such reasons.

There may be no meaningful executable, and the actual executed binary if it even exists on a filesystem that /proc/self/exe can point to, does not have to be a Python interpreter or behave like one.

It is common to see two behaviors in embedding scenarios, either sys.executable = None or something like sys.executable = "/<non-existant-path and explanation>" made up by the embedder such that any code which tries to use the string winds up with an explanation of what's wrong in the error message. Especially when the embedder has fully embedded the standard library in a hermetic executable manner.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead
gpshead marked this pull request as draft April 11, 2026 21:48
@bedevere-app bedevere-app Bot removed the awaiting core review label Apr 11, 2026
@github-actions

github-actions Bot commented May 17, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 17, 2026
@Lekensteyn Lekensteyn mentioned this pull request Jul 19, 2026
sys.executable can be a relative path contrary to documented behavior [3.11 regression] #154160
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner Awaiting requested review from vstinner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@FFY00 @gpshead @vstinner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.