Environment variables and PATH

See your environment variables

In the macOS Terminal window,

env
env | sort
env | sort | cat -n

In Microsoft Windows,

C:\Users\Myname>set

The Python script

environ.py

Running the script in IDLE on my macOS Catalina 10.15,

USER=mark
DISPLAY=/private/tmp/com.apple.launchd.mKjVnHJWIU/org.macosforge.xquartz:0
LOGNAME=mark
PATH=/usr/bin:/bin:/usr/sbin:/sbin
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.JcETotCjaJ/Listeners
HOME=/Users/mark
SHELL=/bin/bash
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
TMPDIR=/var/folders/pb/rx_csw656h95vzh_nk8ndt180000gn/T/
XPC_SERVICE_NAME=0
XPC_FLAGS=0x0
LC_CTYPE=UTF-8
PYTHONPATH=/Applications/Python 3.7/IDLE.app/Contents/Resources
DYLD_LIBRARY_PATH=/Applications/Python 3.7/IDLE.app/Contents/Frameworks
DYLD_FRAMEWORK_PATH=/Applications/Python 3.7/IDLE.app/Contents/Frameworks
__PYVENV_LAUNCHER__=/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7

Things to try

  1. Print the environment variables in alphabetical order of the name of each environment variable. In this and the following example, the .keys() is not necessary. But it reminds you that what we’re looping through is the keys of the dictionary.
    for key in sorted(os.environ.keys()):
        print(f"{key}={os.environ[key]}")
    
    DISPLAY=/private/tmp/com.apple.launchd.mKjVnHJWIU/org.macosforge.xquartz:0
    DYLD_FRAMEWORK_PATH=/Applications/Python 3.7/IDLE.app/Contents/Frameworks
    DYLD_LIBRARY_PATH=/Applications/Python 3.7/IDLE.app/Contents/Frameworks
    HOME=/Users/mark
    LC_CTYPE=UTF-8
    LOGNAME=mark
    PATH=/usr/bin:/bin:/usr/sbin:/sbin
    PYTHONPATH=/Applications/Python 3.7/IDLE.app/Contents/Resources
    SHELL=/bin/bash
    SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.JcETotCjaJ/Listeners
    TMPDIR=/var/folders/pb/rx_csw656h95vzh_nk8ndt180000gn/T/
    USER=mark
    XPC_FLAGS=0x0
    XPC_SERVICE_NAME=0
    __CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
    __PYVENV_LAUNCHER__=/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
    
  2. Print the environment variables in alphabetical order, numbered starting at 1.
  3. for i, key in enumerate(sorted(os.environ.keys()), start = 1):
        print(f"{i:2} {key}={os.environ[key]}")
    
     1 DISPLAY=/private/tmp/com.apple.launchd.mKjVnHJWIU/org.macosforge.xquartz:0
     2 DYLD_FRAMEWORK_PATH=/Applications/Python 3.7/IDLE.app/Contents/Frameworks
     3 DYLD_LIBRARY_PATH=/Applications/Python 3.7/IDLE.app/Contents/Frameworks
     4 HOME=/Users/mark
     5 LC_CTYPE=UTF-8
     6 LOGNAME=mark
     7 PATH=/usr/bin:/bin:/usr/sbin:/sbin
     8 PYTHONPATH=/Applications/Python 3.7/IDLE.app/Contents/Resources
     9 SHELL=/bin/bash
    10 SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.JcETotCjaJ/Listeners
    11 TMPDIR=/var/folders/pb/rx_csw656h95vzh_nk8ndt180000gn/T/
    12 USER=mark
    13 XPC_FLAGS=0x0
    14 XPC_SERVICE_NAME=0
    15 __CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
    16 __PYVENV_LAUNCHER__=/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
    
  4. Run the script from the macOS Terminal command line.
    python3 environ.py
     1 DISPLAY=/private/tmp/com.apple.launchd.mKjVnHJWIU/org.macosforge.xquartz:0
     2 HOME=/Users/mark
     3 LANG=en_US.UTF-8
     4 LOGNAME=mark
     5 OLDPWD=/Users/mark
     6 PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Apple/bin:/opt/X11/bin
     7 PWD=/Users/mark/python
     8 SHELL=/bin/zsh
     9 SHLVL=1
    10 SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.JcETotCjaJ/Listeners
    11 TERM=xterm-256color
    12 TERM_PROGRAM=Apple_Terminal
    13 TERM_PROGRAM_VERSION=433
    14 TERM_SESSION_ID=7EDE954C-CDD0-4EDC-B406-C60426B468B5
    15 TMPDIR=/var/folders/pb/rx_csw656h95vzh_nk8ndt180000gn/T/
    16 USER=mark
    17 XPC_FLAGS=0x0
    18 XPC_SERVICE_NAME=0
    19 _=/usr/local/bin/python3
    20 __CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
    21 __PYVENV_LAUNCHER__=/usr/local/bin/python3
    
    Run the script on the Fedora Linux box oit2.scps.nyu.edu.
    python3 environ.py
     1 HISTCONTROL=ignoredups
     2 HISTSIZE=1000
     3 HOME=/home/m/meretzkm
     4 HOSTNAME=oit2.scps.nyu.edu
     5 LANG=en_US.UTF-8
     6 LD_LIBRARY_PATH=/home/m/meretzkm/web/glut-3.7/lib/glut
     7 LESSOPEN=||/usr/bin/lesspipe.sh %s
     8 LIBGL_ALWAYS_INDIRECT=1
     9 LOGNAME=meretzkm
    10 LS_COLORS=rs=0:di=38;5;33:ln=38;5;51:mh=00:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=01;05;37;41:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;40:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.m4a=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.oga=38;5;45:*.opus=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:
    11 MAIL=/var/spool/mail/meretzkm
    12 OLDPWD=/home/m/meretzkm
    13 PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/m/meretzkm/.local/bin:/home/m/meretzkm/bin:/home/m/meretzkm/.local/glut/bin:/home/m/meretzkm/.local/gnome/bin:/home/m/meretzkm/.local/gtk/bin:/home/m/meretzkm/.local/kde/bin
    14 PWD=/home/m/meretzkm/public_html/python/dict
    15 QT_GRAPHICSSYSTEM_CHECKED=1
    16 SHELL=/bin/bash
    17 SHLVL=1
    18 SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
    19 SSH_CLIENT=66.108.88.87 64513 22
    20 SSH_CONNECTION=66.108.88.87 64513 128.122.238.18 22
    21 SSH_TTY=/dev/pts/0
    22 TERM=xterm-256color
    23 TMP=/tmp
    24 USER=meretzkm
    25 XDG_RUNTIME_DIR=/run/user/1153
    26 XDG_SESSION_ID=243392
    27 _=/usr/local/bin/python3
    

    On Microsoft Windows 10:

     1 ALLUSERSPROFILE=C:\ProgramData
     2 APPDATA=C:\Users\madan\AppData\Roaming
     3 COMMONPROGRAMFILES=C:\Program Files (x86)\Common Files
     4 COMMONPROGRAMFILES(X86)=C:\Program Files (x86)\Common Files
     5 COMMONPROGRAMW6432=C:\Program Files\Common Files
     7 COMPUTERNAME=DESKTOP-P9PKRMD
     8 COMSPEC=C:\WINDOWS\system32\cmd.exe
     9 HOME=C:\Users\madan
    10 HOMEDRIVE=C:
    11 HOMEPATH=\Users\madan
    12 LOCALAPPDATA=C:\Users\madan\AppData\Local
    13 LOGONSERVER=\\DESKTOP-P9PKRMD
    14 NUMBER_OF_PROCESSORS=4
    15 ONEDRIVE=C:\Users\madan\OneDrive
    16 OS=Windows_NT
    17 PATH=C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\madan\AppData\Local\Microsoft\WindowsApps;
    18 PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    19 PROCESSOR_ARCHITECTURE=x86
    20 PROCESSOR_ARCHITEW6432=AMD64
    21 PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
    22 PROCESSOR_LEVEL=6
    23 PROCESSOR_REVISION=4e03
    24 PROGRAMDATA=C:\ProgramData
    25 PROGRAMFILES=C:\Program Files (x86)
    26 PROGRAMFILES(X86)=C:\Program Files (x86)
    27 PROGRAMW6432=C:\Program Files
    28 PSMODULEPATH=C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
    29 PUBLIC=C:\Users\Public
    30 SESSIONNAME=Console
    31 SYSTEMDRIVE=C:
    32 SYSTEMROOT=C:\WINDOWS
    33 TEMP=C:\Users\madan\AppData\Local\Temp
    34 TMP=C:\Users\madan\AppData\Local\Temp
    35 USERDOMAIN=DESKTOP-P9PKRMD
    36 USERDOMAIN_ROAMINGPROFILE=DESKTOP-P9PKRMD
    37 USERNAME=madan
    38 USERPROFILE=C:\Users\madan
    39 WINDIR=C:\WINDOWS
    
  5. List the directories in the PATH environment variable, one per line. Do not sort them.
    import sys
    import os
    
    try:
        path = os.environ["PATH"]          #path is a string.
    except KeyError:
        print("Couldn't find the PATH environment variable.", file = sys.stderr)
        sys.exit(1)
    
    print(f'PATH = "{path}"')
    print(f'os.pathsep = "{os.pathsep}"')  #pathsep is a one-character string.
    print()
    
    directories = path.split(os.pathsep)   #directories is a list of strings.
    
    for i, directory in enumerate(directories, start = 1):
        print(f"{i:2} {directory}")
    

    In IDLE on macOS Catalina 10.15,

    PATH = "/usr/bin:/bin:/usr/sbin:/sbin"
    os.pathsep = ":"
    
     1 /usr/bin
     2 /bin
     3 /usr/sbin
     4 /sbin
    

    From the Terminal on macOS Catalina 10.15:

    PATH = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Apple/bin:/opt/X11/bin"
    os.pathsep = ":"
    
     1 /usr/local/bin
     2 /usr/bin
     3 /bin
     4 /usr/sbin
     5 /sbin
     6 /Library/Apple/usr/bin
     7 /Library/Apple/bin
     8 /opt/X11/bin
    

    From the shell window on the Fedora Linux machine oit2.spcs.nyu.edu:

    PATH = "/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/m/meretzkm/.local/bin:/home/m/meretzkm/bin:/home/m/meretzkm/.local/glut/bin:/home/m/meretzkm/.local/gnome/bin:/home/m/meretzkm/.local/gtk/bin:/home/m/meretzkm/.local/kde/bin"
    os.pathsep = ":"
    
     1 /usr/local/bin
     2 /usr/bin
     3 /usr/local/sbin
     4 /usr/sbin
     5 /home/m/meretzkm/.local/bin
     6 /home/m/meretzkm/bin
     7 /home/m/meretzkm/.local/glut/bin
     8 /home/m/meretzkm/.local/gnome/bin
     9 /home/m/meretzkm/.local/gtk/bin
    10 /home/m/meretzkm/.local/kde/bin
    

    On my Microsoft Windows 7 Home Premium (missing the first lines of output, sorry):

     1 C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
     2 C:\Program Files\Common Files\Microsoft Shared\Windows Live
     3 C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live
     4 C:\windows\system32
     5 C:\windows
     6 C:\windows\System32\Wbem
     7 C:\windows\System32\WindowsPowerShell\v1.0\
     8 C:\Program Files (x86)\Windows Live\Shared
     9 C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\
    10 C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\x64\
    11 C:\Users\Mark\Development\adt-bundle-windows-x86_64-20140702\sdk\tools
    12 C:\Users\Mark\Development\adt-bundle-windows-x86_64-20140702\sdk\platform-tools
    13 C:\Program Files (x86)\QuickTime\QTSystem\
    14 C:\Users\Mark\AppData\Local\Programs\Python\Python36-32
    15 C:\Users\Mark\python