This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add descriptive error message when environment variable not detected
Type: enhancement Stage:
Components: Versions: Python 3.11
process
Status: open Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: astrosticks, eryksun, iritkatriel
Priority: normal Keywords:

Created on 2021-05-29 19:13 by astrosticks, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 23717 astrosticks, 2021-05-29 19:13
Messages (6)
msg394735 - (view) Author: David Gene (astrosticks) * Date: 2021-05-29 19:13
Using os.environ[KEY] with a non-existent environment variable key only gives a simple KeyError, which may be fine for a developer to understand, but if a user of a Python program were to come across it, it may not indicate what they needed to do to avoid the crash. I would raising the keyError with a message such as "Environment variable '{}' not set".
msg412114 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-30 00:23
It's not necessarily true that the environment variable is not set just because the key is not in os.environ. In this example my del did not change the environment variable:

>>> import os
>>> os.environ['TMPDIR']
'/var/folders/kf/0v7kz3ps62dg11v9rq0sz35m0000gn/T/'
>>> del os.environ['TMPDIR']
>>> os.environ['TMPDIR']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen os>", line 678, in __getitem__
KeyError: 'TMPDIR'
msg412115 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-30 00:24
> if a user of a Python program were to come across it, it may not indicate what they needed to do to avoid the crash. 

The user of a program should not see this exception. The program should translate it to an error that would make sense to the user.
msg412116 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-30 00:25
I vote to reject this proposal. Unless another core dev disagrees, I will close this issue.
msg412126 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-01-30 04:50
This is just a point of clarification.

> my del did not change the environment variable

os.environ defines the __delitem__ method to call C unsetenv(). Thus `del os.environ[varname]` does unset the environment variable, at least at the level of the C runtime.
msg412132 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-30 07:58
> os.environ defines the __delitem__ method to call C unsetenv(). Thus `del os.environ[varname]` does unset the environment variable, at least at the level of the C runtime.


For the current process, yes. But it's not that what the user needs to do to fix the problem is to define an environment variable and rerun the program.
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88430
2022-01-30 07:58:02iritkatrielsetmessages: + msg412132
2022-01-30 04:50:58eryksunsetnosy: + eryksun
messages: + msg412126
2022-01-30 00:25:40iritkatrielsetstatus: pending -> open

messages: + msg412116
2022-01-30 00:24:30iritkatrielsetstatus: open -> pending
resolution: rejected
messages: + msg412115
2022-01-30 00:23:04iritkatrielsetnosy: + iritkatriel
messages: + msg412114
2021-05-29 19:13:51astrostickscreate