As you start your journey with Python, you'll eventually come across references to something called your "PATH". Specifically, you'll hear that to make Python easier to access from the command line, you need to "add Python to your PATH".
If you're new to Linux and unfamiliar with environment variables like PATH, this can sound confusing or intimidating. But it doesn't have to be!
Here's a simple breakdown of exactly what PATH is, why you'd want Python added to it, and step-by-step instructions for doing so on Linux.
Understanding the Basics
First, what exactly is PATH? Put simply, it's an environment variable that stores a set of directories on your system. When you run a command from the terminal, your system searches through the directories in PATH to locate the executable file for that command.
Without getting too technical, PATH is essentially a shortcut that makes running commands easier because you don't have to specify the full path to their executable files every time.
Why Add Python?
When you install Python on your Linux system, the main Python executable file (python3) goes to a directory like /usr/bin. This directory is usually already present in PATH by default.
However, other Python-related executables like pip or idle may be located in a directory like /usr/local/bin which could fall outside your PATH. So adding Python's install directory to PATH makes all Python commands uniformly and conveniently accessible.
Instead of having to type the full path like /usr/local/bin/pip install pandas
you can simply run pip install pandas
from any directory. This makes using Python tools much smoother.
Determining Python's Install Location
Before adding Python to PATH, you need to check exactly where Python is installed on your system.
Run the following to print the install location:
which python3
On many systems, this will output something like /usr/bin/python3
.
The bin
directory within Python's install location is what needs to go into PATH. So in this example, you would add /usr/bin
to PATH rather than the full /usr/bin/python3
path.
Adding to PATH Temporarily
If you just want to add Python to PATH for your current terminal session, you can do so with the export
command.
For example, using the install location above:
export PATH="$PATH:/usr/bin"
This appends /usr/bin
to PATH, but only for that session. Once you close the terminal, the change will be lost.
Useful for testing, but you'll want to make the change permanent to have Python consistently available.
Adding to PATH Permanently
To add Python to your PATH permanently on Linux, you'll need to edit shell startup scripts. Which one depends on your Linux shell:
Bash - Edit
.bashrc
in your home directoryZsh - Edit
.zshrc
in your home directoryFish - Edit
.config/fish/
config.fish
For example, to edit .bashrc
with nano:
nano ~/.bashrc
At the bottom of the file, add the following line, replacing the path with your Python install directory:
export PATH="$PATH:/usr/bin"
Save and close the file, then restart your terminal or source .bashrc
for this to take effect:
source ~/.bashrc
Now check if Python commands work properly from any directory, and enjoy the convenience!
Potential Issues
In some cases, you may find that even after adding Python to PATH, commands still fail to run properly from different directories.
A likely culprit is that you have multiple Python versions installed (like Python 2 and Python 3), and updating PATH is still defaulting to the wrong python
executable.
To check this, run which python
after updating PATH. If it points to the wrong Python install, you may need to update PATH with the full path like /usr/bin/python3
instead of just /usr/bin
.
This will ensure the proper Python version comes first in PATH.
Looking Ahead
While manually configuring PATH seems complex at first, it's an essential aspect of developing efficiency working with Python on Linux systems.
Learning this sets you up with a foundation for streamlining all kinds of command-line-based tasks. And soon manipulating environment variables like PATH will feel second nature!
You'll also realize just how customizable Linux-based environments are once you understand the power of your shell profile scripts.
So while it takes some initial effort to add Python to PATH properly, it pays dividends down the road in terms of unlocking additional control and convenience interacting with Python on Linux.