Skip to content

gh-125873: Improve error message when PYTHONHOME is invalid - #126027

Merged
ambv merged 6 commits into
python:mainfrom
refeed:gh-125873-pythonhome-wrong-better-errmsg
Oct 21, 2026
Merged

ambv merged 6 commits into
python:mainfrom
refeed:gh-125873-pythonhome-wrong-better-errmsg

Conversation

@refeed

@refeed refeed commented Oct 27, 2024 •

Copy link
Copy Markdown
Contributor

Summary

Improves the error message shown when the encodings module fails to import due to a misconfigured PYTHONHOME environment variable.

Before: Failed to import encodings module
After: Failed to import encodings module. Your environment probably has misconfigured variables! Please check PYTHONHOME otherwise refer to --help-env

The new message gives users an actionable next step instead of a bare failure notice, which was particularly unhelpful since this error almost always means PYTHONHOME is set to an invalid path.

A NEWS entry is included under Core_and_Builtins.

@ghost

ghost commented Oct 27, 2024 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app

bedevere-app Bot commented Oct 27, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Oct 27, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@ZeroIntensity

Copy link
Copy Markdown
Member

This is user-facing, so it needs a NEWS entry. Something like "Improved error message when PYTHONHOME is invalid" should work.

@refeed

refeed commented Oct 27, 2024

Copy link
Copy Markdown
Contributor Author

Hi @ZeroIntensity , thanks for that! Just added it through the heroku blurb, please let me know if there's something I need to do, thanks

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2024-10-27-22-23-57.gh-issue-125873.BG97KU.rst Outdated
@refeed
refeed force-pushed the gh-125873-pythonhome-wrong-better-errmsg branch from 6c9bdc4 to 6e83963 Compare October 27, 2024 22:37
@refeed refeed changed the title gh-125873: Add hint when failing to import encodings gh-125873: Improve error message when PYTHONHOME is invalid Oct 27, 2024
@refeed
refeed force-pushed the gh-125873-pythonhome-wrong-better-errmsg branch from 6e83963 to c4156f6 Compare October 27, 2024 22:38
ZeroIntensity
ZeroIntensity previously approved these changes Oct 27, 2024

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you! It's good to see new contributors :)

@skirpichev

Copy link
Copy Markdown
Member

I doubt that we need a special treatment just for one environment variable. Same error happens if you wrongly override other PYTHON* variables.

@skirpichev

Copy link
Copy Markdown
Member

@refeed, please avoid rebase/force-pushes/etc next time. Just add new commits while your pr being reviewed.

Comment thread Python/codecs.c Outdated
PyObject *mod = PyImport_ImportModule("encodings");
if (mod == NULL) {
return PyStatus_Error("Failed to import encodings module");
return PyStatus_Error("Failed to import encodings module. Are you sure PYTHONHOME is correct?");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we tell the user what the (wrong) value of PYTHONHOME is?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could, but that significantly complicates things, because the error message has to get put on the heap (PyStatus can't do that, AFAIK). There's also a chance it's unset in the case of embedding.

@ZeroIntensity

Copy link
Copy Markdown
Member

Eh, I disagree. I've encountered this error myself, it's common when embedding the interpreter because if PYTHONHOME is unset this error comes up. I'm happy to make this more general though (maybe instead of specifying PYTHONHOME we could just point to the env var docs).

@refeed

refeed commented Oct 28, 2024

Copy link
Copy Markdown
Contributor Author

Alright, for the env link can I use this: https://docs.python.org/3/using/cmdline.html#environment-variables ?

I'm thinking that the final error message will be:

Failed to import encodings module. Are you sure PYTHONHOME or other variable is correct? Docs on environment variables can be found here: https://docs.python.org/3/using/cmdline.html#environment-variables

I think we still have to mention the PYTHONHOME there because for most cases, I think the culprit is going to be the PYTHONHOME

What do you think?

@ZeroIntensity

Copy link
Copy Markdown
Member

If we're going to go the route of casing this for all invalid variables (I'm not fully convinced, but let's go with it for now), then I don't think actually putting a link to the docs is the right way to do it. We should sort of hint where to look in the docs, something like this should work:

Failed to import encodings module. Your environment probably has misconfigured variables!

@ZeroIntensity
ZeroIntensity dismissed their stale review October 28, 2024 23:39

There's some contention about what should happen here. I'm dismissing my review until that gets solved.

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2024-10-27-22-23-57.gh-issue-125873.BG97KU.rst Outdated
@ZeroIntensity

Copy link
Copy Markdown
Member

@skirpichev This is a more extensive message now, do you like this better?

@skirpichev

Copy link
Copy Markdown
Member

Yes, looks better. Perhaps we could add some reference to --help-env, that will list such variables?

@refeed

refeed commented Oct 29, 2024

Copy link
Copy Markdown
Contributor Author

Alright, how about this?

Failed to import encodings module. Your environment probably has misconfigured variables! Please refer to `--help-env`

@konstin

konstin commented Oct 29, 2024 •

Copy link
Copy Markdown
Contributor

I believe mentioning PYTHONHOME explicitly is important, see the examples in #125873. As a user, the error message links me to --help-env, but then --help-env doesn't provide any guidance on how to fix the problem nor does it mention the import error i just encountered.

@refeed

refeed commented Oct 29, 2024

Copy link
Copy Markdown
Contributor Author

Alright, how about this?

Failed to import encodings module. Your environment probably has misconfigured variables! Please check PYTHONHOME otherwise refer to `--help-env`

@github-actions

Copy link
Copy Markdown

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

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 13, 2026
@ambv

ambv commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

This PR is superseded by #127616, a newer open pull request for the same issue (gh-125873), and it no longer merges cleanly. Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer.

Reopening. This was incorrectly closed as duplicate.

@ambv ambv closed this Oct 20, 2026
@ambv ambv reopened this Oct 21, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 21, 2026
Co-authored-by: Muse Code (Meta Muse Spark)

# Conflicts:
#	Python/codecs.c
@ambv
ambv merged commit 81518ce into python:main Oct 21, 2026
59 of 60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time. type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants