Check Where Python is Installed

This article will show how to locate Python in our system. We will discuss platform-independent methods (works on any OS) and those that are platform specific. Each of the methods has something interesting to show.

Method 1: Locating Python using sys library (platform-independent)

In sys, finding where Python has been installed needs only two lines.

import sys
print(sys.executable)

That will display the location of the Python executable/binary. You can run those lines in a script or even on the command line.

Linux

Figure 1: Locating Python3.9. Here we wake the interpreter and then use sys.executable to locate the binary.

Note: This method will show you the location of the Python you are currently running. In the Figure above, I started Python3.9, which has the binary in /usr/bin as shown in the output.

We can also run the two lines in a one-liner in the command line.

python -c "import sys; print(sys.executable)"

Output:

/usr/bin/python
python3.11 -c "import sys; print(sys.executable)"

Output:

/usr/local/bin/python3.11

Windows

python -c "import sys; print(sys.executable)"

Output (Windows 11 PowerShell and Command Prompt):

C:\Python27\python.exe
python3 -c "import sys; print(sys.executable)"

Output (Windows 11 PowerShell and Command Prompt):

C:\Users\kipro\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe

I have Python 3.9 and 3.11 installed on Linux and Python 2.7, 3.9, and 3.10 on Windows. What if you don’t even know the Python versions installed? Methods 2 through 4 should get you the answer to that question.

Method 2: Using whereis and which commands (Linux and macOS)

The whereis command is used to locate source/binary and manuals. Let’s see how whereis work.

From the Figure above, what we are after are the binaries. They are located at bin/and local/bin. The inbuilt Python is in bin/ (python3.9), whereas the Python installed from the source is mostly located at the local/bin (python3.11).

We therefore have the following Python locations: /usr/bin/python, /usr/bin/python3.9 and /usr/local/bin/python3.11. Confused with the first path? The first two paths point to the same binary. You can see that by listing files with the word starting with “python” in /usr/bin

cd /usr/bin && ls -la python*

Notice that python points (through symbolic links) to python3, which points to python3.9. This means that /usr/bin/python and /usr/bin/python3 refer to the binary in /usr/bin/python3.9.

As shown, whereis as the advantage of showing all installed Python versions. Let’s now see which command.

The which command is a utility that takes a command or a list of commands and searches the path for the binary/executable file that would be run had these command(s) been invoked.

As shown in the Figure above, which command locates Python correctly. We have already explained that the first three results point to the same Python binary.

The command shows us what is actually executed when a given command is run. For example, running the shorthand python3.11 is equivalent to running /usr/local/bin/python3.11 (python3.11 binary).

Method 3: Locating Python using the where command (Windows users)

The where command in Windows works like whereis in Linux and macOS. It locates all the executable files for the command or list of commands issued. We can use “where” on Command Prompt (It won’t work in Windows PowerShell) to locate Python installed as follows.

I have three versions of Python installed: 2.7, 3.9, and 3.10 on Windows, located on the paths shown in the Figure. The last path points to the default Python (3.10).

Method 4: Locating Python Manually (Windows users)

In this method, we need to search for “python” from the Start Menu, select “Apps,” look for executables of our interest on the list then use “Open file location” to locate the executable file (see Figure below).

Note: This method can help you locate all Python executable files, including those in virtual environments.

Conclusion

The use of sys library should be sufficient in most cases to locate specific Python installed.

If we want to get the locations for all Python versions installed, we need to explore the use of the whereis command for Linux and macOS; and where for Windows. We can also manually search for Python in Windows.