DEV Community

Cover image for Environment Variables
Sibusiso Dlamini
Sibusiso Dlamini

Posted on

Environment Variables

There comes a time in every programmers life where they come across a problem. A problem which they begin to solve with great enthusiasm but soon find themselves tearing hair out of their heads. A problem for which they scour the internet to for the solution only to find nothing but a similarly phrased stack overflow question asked in 2007 that has been left unanswered.

I recently came across a problem and luckily it wasn't that bad but it in my opinion it came pretty close. It caused me a few headaches. Headaches that I hope you can avoid which is the reason for sharing my experience. This problem is not even that difficult to wrap you head around. Perhaps it is such a mundane topic that no one really seems to talk about it enough and you don't really find it in textbooks so I thought why not do a piece on my personal encounter with Environment variables. First things first.

What are they?

Environment variables are variables that describe your computers environment. They contain information about your computer such as

  • the location of your home directory
  • the location of your temp directory
  • the username of the user that is currently signed in
  • what language you are using (English UK, English US, French etc)
  • where your executable are stored (PATH)

These variables are like normal programming variables. You can echo them in the terminal if you think I'm lying. They can even be accessed by programs that you run. The format of how to access environment variables will differ between programming languages of course.

Disclaimer! This Tutorial is meant for windows. I know that Linux and MacOS do have environment variables but accessing and changing them are different

If you want to see these variables that I am referring to, you will have to navigate through a couple of windows. Start by pressing win + r and you should see this window appear...

Start window

Enter "Sysdm.cpl" and you should be taken to a window that is that is on the "Advanced" tab.

Advanced Tab window

If not then simply click on the "Advanced" tab and proceed click on the the button with the caption "Environment variables"

Environment variables window

What can environment variables be used for?

If you have every worked with API keys then you should know that you should not share your key with anyone. If you write your API key in your code and upload it to a public platform like Github, there is nothing stopping others from simply copying and pasting your key to use it for themselves. To avoid this we can set up an API key environment variable and access within your script which can then use it to make requests.

There are other use cases but I never really understood them enough to mention. The main point being that these variables serve a purpose.

The PATH variable!

For now I'm really only concerned with the PATH environment variable. It is the variable that contains a list of directories that contain executable files. The PATH variable is what enables us to run programs regardless of our current working directory. This is why you can simply type commands such as where, xcopy, setx, or tasklist without having to navigate into their folders in which they are located run them. If you have Python or NodeJS your machine simply type where npm or where pip to see where your package manager is located. You will notice that it have an .exe extension. In other words, these commands are programs.

To view the PATH variable simply enter PATH into the terminal. With or without capitals, does not really matter. You should still get the same similiar output, that looks something like this.

Path

What does PATH do?

When you enter any command in the terminal, Windows will search each folder in your PATH to see if the command corresponds to the name of any executable file within these folders. If the command is found, great! otherwise you should see this error message

Alt Text

This is the problem that I've been talking about.

My problem πŸ™„πŸ€¦β€β™‚οΈ

I have actually dealt with this problem on more than one occasion. Once when I first installed the python interpreter and the other time while installing JDK(Java develop kit) and that time I had a rather unpleasant experience. You see, when you install python, the installation wizard gives you the opportunity to decide whether you want to add the interpreter and package manager to your PATH or not. On the other hand the JDK wizard doesn't give you an option. You just click next, next, next, finished. Then when you try and compile programs with javac and run them java. You get surprised with the "'java' is not recognized as an internal or external command" messaage.

The solution

The problem is that the java compiler and run time environment or javac.exe and java.exe respectively, are not within your current directory or within any of the folders listed in the path so I needed to either navigate to the JDK folder or Add the folder containing the commands/executables to the PATH.

Setting an enviroment variable

For those who live in the terminal, there is a way to set the environment variables via cmd. If you do so, I advise that you make a backup of your current/original path.

NOTE: If you set an environment variable using the set command in a terminal session. It will only apply to that session. Any programs executed in that session will have access to that particular instance of environment variables. Meaning: if you make changes to any environment variable in the terminal and open up a new terminal. The second terminal session will contain the original environment variables. To set an environment variable permanently use the setx command.

To change the PATH using a GUI navigate to same menu I mentioned earlier where you go to see view environment variables and select the PATH variable and click edit. You should be taken to this window.

Where you can now and simply enter the path/directory to whatever command or program that you want to add to the PATH.

Why would you ever need to know this?

Well perhaps you are a newbie and want to take your first steps towards learning a programming language by setting it up on your machine. Often times this will require you an installation of some sort of development kit or interpreter onto your PATH. In the case that the installer does not add itself to your PATH by default then it would be useful to know how to add it to the path manually. You can imagine what an obstacle this would be if you didn't even know what an environment variable was to begin with.

What I learned! πŸ€“

This problem that I've been referring to is the type of problem that can persist for days depending on your determination and your resources. I wanted to put this out there to help out that poor soul that has been thumped by this issue. I doubt the right person will find it, which in some cases is a good thing.

Don't get me wrong, I don't wish the strain I went through trying to get over this hurdle on another person, but these type of issues, in some way, is a rite of passage. It makes you a better programmer. The googling, stack overflowing, reading docs, getting an idea hitting a wall and doing it all over again. The harder the challenge you solve the more you feel like there exists no obstacles that you cannot overcome. The lower the lows the higher the highs. This is why I have developed this mindset of

If I don't know how to do it. Give me time and eventually I will it done.

Conclusion πŸ“

Environment variables have caused some of the worst complications that I have come across. It is only second to dual booting linux. That one is a real monster and I plan on tackling it, some day... As for now I'm swamped. Off topic. Our country just lifted a level of restriction which could possibly mean that I can go back to campus. I'm so excited πŸ˜„. That's my win for this week.

Let me know what are one of the worst mental obstacles you've had to face when it comes to programming. If you made it this far, thanks for the read. As always Stay safe and Happy CodingπŸ’»!

Top comments (0)