1

Is there a clean way to resolve environment variables or %VARIABLES% in general purely with the Path class without having to use a fallback solution like os.path.expandvars or "my/path/%variable%".fortmat(**os.environ)?

The question How to use win environment variable "pathlib" to save files? doesn't provide an answer. It's exactly the opposite. It suggests using os.path.expandvars.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • 2
    Paths and environment variables really have nothing to do with each other: having environment variables automatically resolved inside paths would violate the single-responsibility principle of the `Path` class (and, yes, `os.path.expandvars` does violate that). It would even be *actively harmful* if environment variables were automatically resolved: `$` and `%` are valid characters in POSIX paths, so it would be a bug if these characters received special handling from `Path`. – Konrad Rudolph Sep 28 '22 at 08:12
  • @KonradRudolph it doesn't have to be automatically, but there should be a _convenience_ API that wouldn't require you to write `Path(os.path.expandvar(other_path))` as this is really super-ugly. – t3chb0t Sep 28 '22 at 08:15
  • I complete disagree that this is ugly: it's explicit yet concise. And this already *is* a convenience API (around manual formatting with the environment dictionary). So I actually don't see the issue with it at all. – Konrad Rudolph Sep 28 '22 at 08:17
  • 1
    In particular, paths are really nothing special in this regard: *any* string could contain environment variables, so if you want special magic to handle this, it should certainly not be tied to paths; instead, it should be present *whenever* you handle a string. – Konrad Rudolph Sep 28 '22 at 08:19
  • @KonradRudolph it is an issue becuase you need to _exit_ the `Path` context, turn it into a string (I forgot the `other_path.as_posix()`), import another module then re-enter the `Path` again to pass it to other APIs. I'd be much nicer if it worked like `some_path.expandvars() -> Path` – t3chb0t Sep 28 '22 at 08:20
  • See my previous comment: paths and environment variables logically have nothing to do with each other. If anything, this should be a function of Python's `str` type (but really, it shouldn't). – Konrad Rudolph Sep 28 '22 at 08:21
  • Actually I don't understand your previous comment: you don't have to "exit" the `Path` context, because your original value (containing environment variables) *should not be a `Path` object:* it *isn't* an actual path! It's just a string. Don't store it as a `Path` until *after* you expanded the environment variables in it. If your code is doing this differently that's a *logical error* that could lead to bugs. The fact that your code is becoming convoluted is the type system's hint that you're doing something wrong. – Konrad Rudolph Sep 28 '22 at 08:23
  • @KonradRudolph it must be stored this way as it is built dynamically from various sources and configs with several environment variables that are resolved only once right before it's going to be used. Otherwise it'd be quite inconvenient to do repeat this step everywhere I read some element of it. – t3chb0t Sep 28 '22 at 08:26
  • 1
    It definitely does not "must" be stored this way. I strongly recommend using types as they are intended instead of hacking around their limitations. In the case of `Path`, the intent is for it to store *actual paths*, not strings that can be transformed *into* paths (e.g. by expanding environment variables). If convenient for your use-case, you are free to create a *custom type* (e.g. `UnexpandedPath`) that provides your desired semantics. That way you're using the type system to your advantage instead of fighting against it. – Konrad Rudolph Sep 28 '22 at 08:30

1 Answers1

2

This comment from an answer of the linked thread clearly states that it does not provide anything equivalent to os.path.expandvars. Going through the documentation for the module (as of Python 3.10) does not make any references to environment variables.

So, no.

metatoaster
  • 17,419
  • 5
  • 55
  • 66
  • Now, that's a clean Q&A :-] I was hoping I was missing some trick, but it seems that's the way it is. – t3chb0t Sep 28 '22 at 07:57