[New-bugs-announce] [issue39461] os.environ does not support Path-like values, but subprocess(..., env=...) does

Antony Lee report at bugs.python.org
Mon Jan 27 05:25:58 EST 2020


New submission from Antony Lee <anntzer.lee at gmail.com>:

As of Py3.8/Linux:

    In [1]: os.environ["foo"] = Path("bar")                                                                             
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-4-2827297496cb> in <module>
    ----> 1 os.environ["foo"] = Path("bar")

    ~/miniconda3/envs/default/lib/python3.8/os.py in __setitem__(self, key, value)
        676     def __setitem__(self, key, value):
        677         key = self.encodekey(key)
    --> 678         value = self.encodevalue(value)
        679         self.putenv(key, value)
        680         self._data[key] = value

    ~/miniconda3/envs/default/lib/python3.8/os.py in encode(value)
        746         def encode(value):
        747             if not isinstance(value, str):
    --> 748                 raise TypeError("str expected, not %s" % type(value).__name__)
        749             return value.encode(encoding, 'surrogateescape')
        750         def decode(value):

    TypeError: str expected, not PosixPath

    In [2]: subprocess.run('echo "$foo"', env={**os.environ, "foo": Path("bar")}, shell=True)                           
    bar
    Out[2]: CompletedProcess(args='echo "$foo"', returncode=0)

I guess it would be nice if it was possible to set os.environ entries to Path-like values, but most importantly, it seems a bit inconsistent that doing so is not possible on os.environ, but works when setting the `env` of a subprocess call.

----------
components: Library (Lib)
messages: 360750
nosy: Antony.Lee
priority: normal
severity: normal
status: open
title: os.environ does not support Path-like values, but subprocess(..., env=...) does
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39461>
_______________________________________


More information about the New-bugs-announce mailing list