Bartosz Karwacki
Bartosz Karwacki

Reputation: 331

Clean way to get Windows PATHs in python

I need to check if MY_PATH exists in Windows PATHS, I do not mean about PYTHONPATH.

For example, if PowerShell PATH exists. I know there is sys.path, but it returns only python paths.

Is there any clean way to check it? I wrote code that works:

import subprocess as sp
out, _ = sp.Popen(["echo", "%PATH%"], shell=True, stdout=sp.PIPE).communicate()
assert any("MY_PATH" in path.decode() for path in out.splitlines())

But is there a better way?

Upvotes: 0

Views: 62

Answers (1)

Daweo
Daweo

Reputation: 36370

You might use os.environ for that task as follows

import os
print("MY_PATH" in os.environ['PATH'])

Upvotes: 1

Related Questions