logo

How to Reference Environment Variables in Julia 📂Julia

How to Reference Environment Variables in Julia

Overview

This document explains how to reference environment variables in Julia1.

Code

Base.ENV
Base.ENV["JULIA_NUM_THREADS"]

As you can see, accessing environment variables does not require loading any separate package; you can directly access them through Base.ENV. Since they are read as a dictionary, using the name of the desired environment variable as a key will return the environment variable as a string. The results of executing the above two lines of code are as follows.

julia> Base.ENV
Base.EnvDict with 62 entries:
  "ALLUSERSPROFILE"              => "C:\\ProgramData"
  "APPDATA"                      => "C:\\Users\\rmsms\\AppData\\Roaming"
  "CHROME_CRASHPAD_PIPE_NAME"    => "\\\\.\\pipe\\crashpad_14984_WLSYYXMTXMJWXZQG"
  "COMMONPROGRAMFILES"           => "C:\\Program Files\\Common Files"
  "COMMONPROGRAMFILES(X86)"      => "C:\\Program Files (x86)\\Common Files"
  "COMMONPROGRAMW6432"           => "C:\\Program Files\\Common Files"
  "COMPUTERNAME"                 => "SICKRIGHT"
  "COMSPEC"                      => "C:\\WINDOWS\\system32\\cmd.exe"
  "CUDA_PATH"                    => "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.5"
  "CUDA_PATH_V11_5"              => "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.5"
  "DRIVERDATA"                   => "C:\\Windows\\System32\\Drivers\\DriverData"
  "GOPATH"                       => "C:\\Users\\rmsms\\go"
  "HOMEDRIVE"                    => "C:"
  "HOMEPATH"                     => "\\Users\\rmsms"
  "JULIA_NUM_THREADS"            => "16"
  "LOCALAPPDATA"                 => "C:\\Users\\rmsms\\AppData\\Local"
  "LOGONSERVER"                  => "\\\\SICKRIGHT"
  "NAVER"                        => "e=2.718281"
  "NUMBER_OF_PROCESSORS"         => "16"
  "NVCUDASAMPLES11_5_ROOT"       => "C:\\ProgramData\\NVIDIA Corporation\\CUDA Samples\\v11.5"
  "NVCUDASAMPLES_ROOT"           => "C:\\ProgramData\\NVIDIA Corporation\\CUDA Samples\\v11.5"
  "NVTOOLSEXT_PATH"              => "C:\\Program Files\\NVIDIA Corporation\\NvToolsExt\\"
  "ONEDRIVE"                     => "C:\\Users\\rmsms\\OneDrive - knu.ac.kr"
  "ONEDRIVECOMMERCIAL"           => "C:\\Users\\rmsms\\OneDrive - knu.ac.kr"
  "ONEDRIVECONSUMER"             => "C:\\Users\\rmsms\\OneDrive"
  "OPENBLAS_MAIN_FREE"           => "1"
  "OPENBLAS_NUM_THREADS"         => "8"
  ⋮                              => ⋮

julia> Base.ENV["JULIA_NUM_THREADS"]
"16"

Environment

  • OS: Windows
  • julia: v1.7.0