Loading video player…

Understanding What PATH Is

00:00 What is the PATH environment variable?

00:03 Just like your Python programs can have variables to store different values, your operating system also keeps track of its own variables, which we call environment variables.

00:13 You might have a USER one to store the currently logged-in user or HOME that holds the user’s home directory.

00:22 PATH is an environment variable that stores a big string, which contains a list of paths to folders. Those folders contain executables, like python.exe, and these are basically programs that are run when a command with the same name is invoked.

00:38 So what does PATH look like? On my Mac, I can type this command, echo $PATH, into the command line or terminal and view the contents of PATH.

00:49 And then I get this big wall of text. And it can look overwhelming, but it’s really just a long list of strings, of paths. Since I’m on a Mac, my folder paths are separated by colons, and if this were Windows, it would use semicolons as a delimiter.

01:05 Now let’s make it easier to read by turning those colons into newline characters using this little command here. Now you can more easily see that it’s just a bunch of paths to folders, including ones called like Python.framework or /usr/bin.

01:25 So some important notes about PATH are, again, it uses colons or semicolons as a delimiter, depending on your operating system. All of the paths must be for directories, and you shouldn’t have files or executables being pointed to directly.

01:41 Also, subdirectories inside of the PATH directories are not searched, so you can’t just add your root directory to PATH and have it look everywhere.

01:52 And what happens when you run python in the command line? Your terminal will search the folders inside of PATH in order and look for an executable called python or python.exe. If it’s found, it’ll run it, and if it’s not found in any of the paths, then it returns an error.

02:15 So you might get something like this, command not found, python. Or on Windows, it might open up the Windows Store and prompt you to install Python.

02:25 So what are some of the potential problems you might come across when trying to run Python? One is that Python’s directory is not even on PATH, so you’d get command not found, and then you’d have to add it to PATH.

02:39 It could be that the order of directories on PATH is wrong, so when you use the python command, it actually runs a different version than what you want it to, because that’s what was found first.

02:50 It could be that you have a corrupted executable on PATH, like maybe during the installation process, your computer shut down, and python.exe doesn’t work for some reason.

03:02 And if that was the first one to be found, then it’s going to fail. So maybe you need to remove that path from your PATH environment variable.

03:11 And finally, it’s possible that the python executable is not on PATH, but some other equivalent command is. So this is a common point of confusion for beginner Python developers, because tutorials will often use the basic python command.

03:27 But, confusingly, depending on your operating system and how you installed Python, it’s often the case that on Unix systems, you should actually be using the python3 command, and on Windows, you should be using the py command. And these are on your PATH, just not the basic python one.

03:47 So, in the next lesson, you’ll see how you can run Python on Windows if the basic python command doesn’t work.

Become a Member to join the conversation.