Ben Chuanlong Du's Blog

It is never too late to learn.

Dealing With Environment Variables in Python

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

  1. os.environ is a dict-like object containing environment variables as keys and their values as values. You can query the value of an environment variable using os.environ[ENV_VAR_NAME]. And you can set the value of an environemnt variable using os.environ[ENV_VAR_NAME] = val.

  2. You should use os.pathexpanduser("~") instead of os.getenv('HOME') to get the home directory of the current user in Python. os.getenv('HOME') only works on Linux/Unix.

In [1]:
import os
In [2]:
os.environ
Out[2]:
environ{'GITPOD_WORKSPACE_CLASS_INFO': '{"id":"g1-standard","category":"GENERAL PURPOSE","displayName":"Standard","description":"Up to 4 vCPU, 8GB memory, 30GB disk","powerups":1,"isDefault":true,"deprecated":false}',
        'GITPOD_WORKSPACE_CONTEXT_URL': 'https://github.com/legendu-net/blog',
        'GITPOD_REPO_ROOTS': '/workspace/blog',
        'LANGUAGE': 'en_US:en',
        'USER': 'gitpod',
        'JAVA_TOOL_OPTIONS': ' -Xmx3489m',
        'GIT_EDITOR': '/ide/bin/remote-cli/gitpod-code --wait',
        'GITPOD_PREVENT_METADATA_ACCESS': 'true',
        'HOSTNAME': 'legendunet-blog-zs52zub3k4w',
        'GITPOD_ANALYTICS_SEGMENT_KEY': 'bUY8IRdJ42KjLOBS9LoIHMYFBD8rSzjU',
        'THEIA_WEBVIEW_EXTERNAL_ENDPOINT': 'webview-{{hostname}}',
        'HOME': '/home/gitpod',
        'VSX_REGISTRY_URL': 'https://open-vsx.gitpod.io',
        'GITPOD_TASKS': '[{"command":". /scripts/gitpod.sh"}]',
        'WORKSPACEKIT_WRAP_NETNS': 'true',
        'VISUAL': '/ide/bin/remote-cli/gitpod-code',
        'SUPERVISOR_ADDR': 'localhost:22999',
        'GITPOD_THEIA_PORT': '23000',
        'GITPOD_IDE_ALIAS': 'code',
        'THEIA_MINI_BROWSER_HOST_PATTERN': 'browser-{{hostname}}',
        'GITPOD_ANALYTICS_WRITER': 'segment',
        'GITPOD_MEMORY': '3489',
        'GITPOD_WORKSPACE_CONTEXT': '{"isFile":false,"path":"","title":"legendu-net/blog - master","ref":"master","refType":"branch","revision":"7d52e66edf645a7c9ce51870924ec7d3abd8e90d","repository":{"cloneUrl":"https://github.com/legendu-net/blog.git","host":"github.com","name":"blog","owner":"legendu-net","private":false},"normalizedContextURL":"https://github.com/legendu-net/blog","checkoutLocation":"blog"}',
        'GITPOD_GIT_USER_EMAIL': 'legendu.net@outlook.com',
        'GITPOD_HOST': 'https://gitpod.io',
        'GP_OPEN_EDITOR': '/ide/bin/remote-cli/gitpod-code',
        'PIP_USER': 'no',
        'GITPOD_OWNER_ID': '5e66c014-459b-4dcb-93f0-6d5b36f3d59f',
        'RUSTUP_HOME': '/usr/local/rustup',
        'GITPOD_WORKSPACE_URL': 'https://legendunet-blog-zs52zub3k4w.ws-us80.gitpod.io',
        'PATH': '/home/gitpod/.local/bin:/home/gitpod/.SpaceVim/bin:/home/gitpod/.vim/bin:/usr/local/go/bin:/usr/local/cargo/bin:/bin:/ide/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/usr/lib/jvm/java-11-openjdk-amd64/bin:/home/gitpod/.pyenv/shims',
        'GITPOD_GIT_USER_NAME': 'Ben Du',
        'THEIA_WORKSPACE_ROOT': '/workspace/blog',
        'GITPOD_INSTANCE_ID': '76d73400-f56c-46ae-b6aa-ae8227afa166',
        'GP_EXTERNAL_BROWSER': '/ide/bin/remote-cli/gitpod-code --openExternal',
        'LANG': 'en_US.UTF-8',
        'GITPOD_CLI_APITOKEN': 'Jm2Gkzj4bAx9GZeHLZzNAI8IsPJW5eUL',
        'JAVA_HOME': '/usr/lib/jvm/java-11-openjdk-amd64',
        'LC_ALL': 'en_US.UTF-8',
        'PWD': '/',
        'GITPOD_WORKSPACE_CLUSTER_HOST': 'ws-us80.gitpod.io',
        'GITPOD_WORKSPACE_CLASS': 'g1-standard',
        'M2_HOME': '/usr/share/maven',
        'GITPOD_WORKSPACE_ID': 'legendunet-blog-zs52zub3k4w',
        'GITPOD_REPO_ROOT': '/workspace/blog',
        'GP_PREVIEW_BROWSER': '/ide/bin/remote-cli/gitpod-code --preview',
        'GITPOD_INTERVAL': '30000',
        'THEIA_RATELIMIT_LOG': '50',
        'EDITOR': '/ide/bin/remote-cli/gitpod-code',
        'VSCODE_HANDLES_SIGPIPE': 'true',
        'SHLVL': '0',
        '_': '/ide/node',
        'VSCODE_AMD_ENTRYPOINT': 'vs/workbench/api/node/extensionHostProcess',
        'VSCODE_HANDLES_UNCAUGHT_ERRORS': 'true',
        'VSCODE_NLS_CONFIG': '{"locale":"en","availableLanguages":{}}',
        'GITPOD_CODE_HOST': 'https://gitpod.io',
        'BROWSER': '/ide/bin/helpers/browser.sh',
        'VSCODE_CWD': '/',
        'ELECTRON_RUN_AS_NODE': '1',
        'VSCODE_IPC_HOOK_CLI': '/tmp/vscode-ipc-7cbcc44b-6f13-4976-952c-9d9e2d607ea5.sock',
        'PYTHONUNBUFFERED': '1',
        'PYTHONIOENCODING': 'utf-8',
        'TERM': 'xterm-color',
        'CLICOLOR': '1',
        'FORCE_COLOR': '1',
        'CLICOLOR_FORCE': '1',
        'PAGER': 'cat',
        'GIT_PAGER': 'cat',
        'MPLBACKEND': 'module://matplotlib_inline.backend_inline',
        'PYDEVD_IPYTHON_COMPATIBLE_DEBUGGING': '1'}
In [3]:
os.environ["PATH"]
Out[3]:
'/home/gitpod/.local/bin:/home/gitpod/.SpaceVim/bin:/home/gitpod/.vim/bin:/usr/local/go/bin:/usr/local/cargo/bin:/bin:/ide/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/usr/lib/jvm/java-11-openjdk-amd64/bin:/home/gitpod/.pyenv/shims'
In [4]:
os.environ["PIP_USER"]
Out[4]:
'no'
In [5]:
os.environ["PIP_USER"] = "yes"
os.environ["PIP_USER"]
Out[5]:
'yes'
In [ ]:
 

Comments