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.

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

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

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

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

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:

On csh shell:

On sh shell:

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:

Removing a directory from the PATH

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

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.