What the heck is an Environment Variable?
You may have heard someone mention setting your environment variables. Maybe you were troubleshooting getting your Python interpreter to run your script when you first started learning Python. So, what are these things called environment variables? Well, I’ll try to explain what they are in a way that makes sense.
The easiest way to think about environment variables is that it is a specific variable that can be referenced by code to get a file system location. This location may vary from one person’s file system to another. For instance, if you wanted to know the location of the myMagicApplication.exe executable, you need some way to determine where it was installed. Because you can choose where to install it during installation, you can’t just reference the default location in you program, because it will vary from one machine to another. But with an environment variable, you could reference a static variable that will always point to the installation location. This is what environment variables do.
There are different kinds of Environment Variables
There are default environment variable which are created by your operating system that reference common places in you files system. This could be your user profile home directory, or the “Program Files” directory in Windows, the system root and others. In Windows, you also have two different types of Environment variables: user and system variables. System variables are variables which are accessible by any user on the machine, where as user variables are only accessible to the logged in user.
If there are duplicate variable which conflict between a user and system variable, the system variable is always used first. Variables can also contain multiple paths and are evaluated in the order in which they occur. For instance, say you have two different executable applications with the same name, myApp.exe and they are located in two different directories. Both of these directories are listed in your PATH environment variable and you try to run myApp.exe from the command line without running it while in either of their directories. It will run the first instance of myApp.exe that is finds by name based on the environment variables. I recently ran into this while trying to run the nodeJs executable node.exe while also haveing Microsoft HPC Manager installed, which also contains a node.exe executable. Both directories were listed in my PATH environment variable, and because HPC Manager was installed first, it’s directory was listed first. After changing the order to put the nodeJs executable first, it launched it as I wanted.
Why do I need to know about Environment Variables?
You usually find yourself playing around and adjusting environment variables is when you are running programs from the command line. The PATH environment variable is usually the most common one that you will configure. When type a command to run, the command line will first look in the directory you are in to run the command you typed. If it does not exist, it will the start to check each of the directories in the PATH variable to find a matching application. Once it finds the first occurrence of an application that matches that name, it will launch it. If it doesn’t find it, the command line will tell you that it couldn’t find it.
Usually when you install certain programs, such as Python, Java, nodeJs, MSSQL and a myriad of other, it will add the executable directory to the system PATH variable. Sometimes during the installation it will ask you if you want to add it. In addition, there are default location which are listed by default by you OS. for executable, if you are running Robocopy.exe from Windows, the default PATH variable includes the location to the C:\Windows\system32 directory, which contain multiple executables which are part of the Windows operating system.
Using Environment Variables
When you are working on your own applications, it may be necessary to reference a part of the file system which your program accesses frequently. But what if this location is different from machine to machine and you allow the user to change it? Well, you can create a new environment variable that you can reference in your application, and it won’t matter what it is set to, or changed in the future. You can reference this custom environment variable throughout your code, and regardless of where it is located, you will always be able to access it. By creating a variable that references the base path to where you want to access, you can then access any directory structure that is a child of the root path in your environment variable.