Skip to content

Instantly share code, notes, and snippets.

Last active April 19, 2023 11:52
What would you like to do?
Python interpreter info
import json
import sys
import os
import pkg_resources
def validate_path(path):
if path:
return path.split(os.pathsep)
return None
def get_working_set():
l = []
for i in pkg_resources.working_set:
l.append('{} {} {}'.format(i.key, i.version, i.location))
return l
def get_version_info():
return '{}.{}.{} {} serial={}'.format(sys.version_info.major, sys.version_info.minor, sys.version_info.micro,
sys.version_info.releaselevel, sys.version_info.serial)
def main():
d = {
'general_info': {
'sys.prefix': sys.prefix,
'sys.executable': sys.executable,
'sys.version_info': get_version_info(),
'__file__': __file__ if '__file__' in globals() else None,
'sys.path': sys.path,
'pkg_resources.working_set': get_working_set(),
'environ': {
'PATH': validate_path(os.getenv('PATH')),
'PYTHONPATH': validate_path(os.getenv('PYTHONPATH')),
'VIRTUAL_ENV': os.environ.get('VIRTUAL_ENV'),
'CONDA_PREFIX': os.environ.get('CONDA_PREFIX'),
j = json.dumps(d, indent=4)
if __name__ == '__main__':
Copy link

wstomv commented Jun 10, 2020

Why is os.environ['PATH'].split(os.pathsep) inspected twice and associated with different keys? Shouldn't the second occurrence be os.environ['PYTHONPATH'].split(os.pathsep)? Note that PYTHONPATH is not always set (and usually not needed).

Copy link

aresler commented Jun 10, 2020

What a silly mistake, thanks for pointing that out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment