Add Python to Path in Linux

This article will cover four things: explain what PATH is in Linux, how to add Python to PATH, work on an example to demonstrate concepts, and lastly, for your convenience, discuss removing a directory from PATH.

What is PATH in Linux

PATH is an environment variable that contains a list of paths separated by a colon on Linux and a semi-colon in Windows.

Programs use the PATH variable to locate binaries (“executables”). For example, when you start Python from the terminal, the program will search for Python binaries in predefined directories. Among places to search are paths provided in PATH.

You can view the directories in PATH by running the following command on the terminal.

echo $PATH

Most executable binaries are located at bin/, /usr/bin, /usr/local/bin, and /local/bin. These paths are already on the PATH (as shown above). That means most programs (including Python – see Figure below) should easily be started from the terminal using the executable’s name.

Adding a path into the PATH variable in Linux

If you’re using bash, add this line to your .bashrc located in the home directory

export PATH="$PATH:/path/to/add"

For example, the following line adds /usr/.local/bin/python to the PATH.

export PATH="$PATH:/usr/.local/bin/python"

Save the .bashrc file and run the following command to refresh your bash session.

source ~/.bashrc

Note: You can run this command to accomplish all the above:

echo export PATH=/home/kiprono/.local/bin:$PATH >> ~/.bashrc && source ~/.bashrc

Alternatively, you can add a path directly from the shell. Depending on the shell type, a directory can be added by running any of the following commands on the terminal:

On bash:

export PATH="$PATH:/path/to/add"

On csh shell:

PATH "$PATH:/path/to/add"

On sh shell:

PATH="$PATH:/path/to/add"

Example

In this example, we start by installing Python 3.11 on the Desktop – on a directory that is not in PATH.

Immediately after installing this Python, I got this warning:

WARNING: The scripts pip3.10 and pip3.11 are installed in '/home/kiprono/Desktop/python311/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Python 3.11 is installed in the bin folder. We need to add that folder into PATH; then, we can simply run “python3.11” to wake up this Python. We can do that by running the following:

export PATH=/home/kiprono/Desktop/python311/bin:$PATH

Removing a directory from the PATH

You can run this command on the terminal to remove a folder from PATH.

PATH=$(REMOVE_PART="/path/to/remove" sh -c 'echo ":$PATH:" | sed "[email protected]:$REMOVE_PART:@:@g;[email protected]^:\(.*\):\[email protected]\[email protected]"')

Note: You may also have to remove the path from the .bashrc file to have a directory deleted from the PATH completely.

Conclusion

PATH is an environment variable containing a list of paths searched when looking for program executables. Adding Python to PATH means adding the location to the Python binary into the environment. This is done by running the following command on the terminal export PATH=/path/to/add:$PATH.