Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Adding Python to PATH on Linux

00:00 When you’re working on Linux, there are a few different ways of installing Python. One is that it already comes pre-installed most of the time, so you can just use that if you want to.

00:11 You can use your distro’s package manager, like apt, for Ubuntu to install it, and you can build it from source code. Note that there’s no official python.org installer for it because there’s too many distributions of Linux to make that feasible.

00:26 Some other tools you could use would be Anaconda, uv, pyenv, or Docker. And most of the time you don’t need to add anything to PATH to make sure that Python works. It should be there already because most of these options are going to install into locations that are already on your PATH, like /usr/bin.

00:49 So when might you want to update PATH? If you happen to have a custom install location that’s not already on your PATH, which I wasn’t actually able to get working on my computer. Then if you wanted to remove an automatically added PATH location, like if you used Anaconda, it’ll install into a location in your home folder and add it to PATH.

01:15 But if you wanted to remove it, you could do that. In the next lesson, we’ll actually look at how to remove items from PATH. Right now we’re looking at just adding them.

01:25 Another common use case would be if you have global Python tools like pipx or black, and you want to run these commands from the command line, they’re going to be installed into a folder in your home directory under ~/.local/bin.

01:41 But that’s not added to PATH by default. So those commands aren’t going to work out of the box. That’s a good case for adding an item to PATH.

01:51 That’s what I’m going to demo in this example. I do have pipx installed. I installed it using pip, but the command isn’t found. Not yet at least.

02:03 If you did have something installed somewhere, but you weren’t quite sure where and you wanted to add that location to PATH, you could use the find command to search for it. So for example, this command is going to search inside of /usr and /home locations, search for executables with anything that has /bin/python in the name.

02:30 So let’s try it out. sudo find /usr /home, then -executable

02:43 -path. And then the regular expression is inside of quotes, */bin/python*, where the star is a wildcard. So it does give a bunch of results.

02:59 And you can check that these are the executable that you want to add. If I just copy and paste one of these paths to a version of Python inside of a bin folder, that’s just python and then a version number, okay, we don’t want -config or anything.

03:21 Paste that and run it and that should open up a Python console. So that is in fact an executable that you might want to be pointing to. Now these ones are already on my PATH, /usr/local/bin, /usr/bin.

03:37 So that’s why in the next part, we’re going to add that pipx location to PATH instead of a Python one. So once you have your directory that you want to add to PATH, you’re going to need to find a dotfile that runs at the beginning of when you open up a shell terminal.

03:57 You’re going to edit that dotfile to update PATH. And the file you’re looking for is going to have one of these names. Starting from the top, you can look for them.

04:07 And the first one that you find is the one that you can use.

04:13 You’re going to search in your home directory.

04:16 So cd ~ to make sure you’re in your home directory, then ls -a to search for all of the files and folders there. -a shows also the hidden ones, so ones that start with a dot.

04:30 And I do have .profile here. So that’s the one I’m going to use. Make sure to use your dotfile that you found when you’re following along with these commands.

04:43 To see the contents of it, you can use the cat command. So cat .profile to see what’s inside. And there are already some items that are modifying PATH in here.

04:57 In the next lesson, I’ll show you how to edit the file, like go into it and remove things and add things. For now, you can just use this command to just add an item to the bottom without opening it up in an editor.

05:13 So to do that, we’re going to use this command and I’ll explain later what it’s doing.

05:22 echo export PATH equals quote. Inside of those quotes, we’re going to add the new path. So this one is going to be $HOME to get the location of the HOME variable that is our home location.

05:43 Then /.local/bin. That’s the new item that we want to add.

05:50 Then colon $PATH. That’s going to add the existing value of PATH to the end so that we’re not overriding the whole value of PATH. We’re just adding a new item to the beginning.

06:02 Then two right angle brackets or greater than symbols, then .profile.

06:10 So what this is doing is it’s adding this line export PATH equals the new PATH value.

06:17 Instead of running that, we want to echo it, which is basically print out the text and then put it into the .profile file.

06:29 Enter, that’s going to add it to the bottom. So cat .profile will display the contents and it’s now added to the bottom with all of the variables expanded.

06:41 And then you can say source .profile to run it and execute it.

06:50 And now pipx, the location was added to PATH and it works as a command. So that’s how to add an item to PATH in Linux.

07:02 In the next lesson, we’ll look at how to modify PATH in both Mac and Linux because they’re very similar. And we’ll do this by editing those dotfiles from the command line and understanding how the rest of PATH gets formed, like how /usr/bin gets added to PATH automatically.

Become a Member to join the conversation.