Finding Where Python Is Installed (when it isn’t the default dir)

Find Where Python Is Installed On Windows Linux Mac

Python’s interpreter, libraries, and scripts can reside in multiple locations on a system. So, finding where Python is installed helps with:

  • Locating the python executable to launch the interpreter
  • Editing module files under the Python install’s site-packages
  • Modifying scripts under the scripts folder of a Python environment
  • Switching between multiple Python versions using virtual environments
  • Troubleshooting issues related to Python installations

The methods for finding Python’s install location vary across operating systems. In detail, we’ll explore platform-specific techniques on Windows, Linux/Unix, and macOS. Let’s jump right in

Finding Python Location on Windows

There are several easy methods to find where Python is installed on Windows. Using the where command or checking Python’s sys path returns the location.

Also read: Pipenv: The New Packaging Tool For Python

Using the where Command

The easiest way is to use the where command within the Command Prompt (CMD). Simply open CMD and type:

where python

Here’s an example output showing the path to the Python interpreter:

C:\Users\gurpreet\AppData\Local\Programs\Python\Python310\python.exe

So for the sample product data table we imported earlier, we can launch Python from this location:

C:\Users\gurpreet\AppData\Local\Programs\Python\Python310\python.exe

import pandas as pd
print(pd.read_csv('products.csv'))

The where command also works in PowerShell:

PS C:\> where python 
C:\Users\gurpreet\AppData\Local\Programs\Python\Python310\python.exe

On Windows, Python gets added to your system’s PATH environment variable. So running python launches the interpreter from any location.

But where python returns the actual install location on disk.

Also read: [Fix] Bash: Python3: command not found When Installing discord.py on Windows

Checking Python’s sys.executable

Another method is using Python’s built-in sys module to print the path to the currently running executable:

import sys
print(sys.executable)

Output:

C:\Users\gurpreet\AppData\Local\Programs\Python\Python310\python.exe  

You can also directly run this oneliner from CMD or PowerShell instead of launching Python first:

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

So, if you have multiple Python versions installed (eg. Python 2.7, 3.7, 3.10 etc), sys.executable points to the specific executable.

This helps when switching between Python installs using virtual environments.

Additional Ways of Finding Python Path on Windows

Some other methods for locating where Python is installed on Windows include:

  • Searching for python.exe using Windows search
  • Running pip list to see packages installed in Python’s site-packages
  • Checking the start menu Python folder’s location
  • Using Get-Command python in PowerShell

So in summary, where pythonsys.executable, and Windows search makes finding Python installs easy on Windows.

Next, let’s look at Linux/Unix systems.

Finding Python on Linux/Unix

On Linux and Unix-based operating systems like Ubuntu, Debian, CentOS etc, there are simple terminal commands for finding where Python is installed.

The which, type -a, and readlink commands come in handy here.

Using the which Command

Most Linux/Unix systems have Python pre-installed and available globally via the shell.

You can find where it’s located by using which python:

which python3
/usr/bin/python3

This returns the path to the global Python executable in use.

For our product data example, we can run:

/usr/bin/python3

import pandas as pd
print(pd.read_csv('products.csv')) 

To find all installed Python versions, use:

which -a python

/usr/bin/python
/usr/bin/python3  
/usr/local/bin/python
/home/preet/virtualenvs/analytics/bin/python

So which python helps locate not just the active Python interpreter, but also all available versions on your Linux/Unix system.

Using type -a

Another option is using the type command with the -a flag:

type -a python
python is /usr/bin/python
python is /usr/bin/python3
python is /usr/local/bin/python 
python is /home/preet/virtualenvs/analytics/bin/python

This displays all commands matching the name python across directories in your $PATH.

Sometimes Python executables are symbolically linked in the /usr/bin or /usr/local/bin folders to their actual location.

For example:

which python3
/usr/bin/python3

We can find where this symlink points to using readlink:

readlink -f /usr/bin/python3
/usr/local/lib/python3.8/bin/python3

This shows the actual install location to be /usr/local/lib/python3.8/.

In summary, using whichtype, and readlink allows locating both global and virtual environment Python installs on Linux and Unix.

Finding Python on macOS

On macOS, Python generally gets installed into the /Library/ or ~/Library/ folders.

The Finder app provides an easy way to find these locations.

You can also use terminal commands like which and sys.executable covered earlier.

Let’s go through the Finder approach first.

Using Finder

To find where Python is installed on your Mac:

1. Launch Finder and hit Command + Shift + G

This opens the Go to Folder dialog box.

2. Enter paths like /Library/Frameworks/Python.framework or ~/Library/Python

Image 10
Using Command + Shift + G to find Python

The /Library path contains Python versions installed system-wide for all users. This includes major versions like:

/Library/Frameworks/Python.framework/Versions/3.11
/Library/Frameworks/Python.framework/Versions/2.7  

While the ~/Library path has Python installs scoped to your user account. Typically, you’ll find virtual environments here:

~/Library/Python/3.11/bin 

3. Drill down the folders to see the bin/ directory containing python3.

So using Finder provides a graphical way to explore Python install locations on macOS.

Additional Ways of Finding Python

As on Linux/Unix, which python finds the active Python version:

Image 11
which python3
/usr/local/bin/python3 

And sys.executable prints the currently running one:

python3 -c "import sys; print(sys.executable)"
/Users/preet/virtualenvs/analytics/bin/python

These terminal commands work the same on macOS as on Linux.

In summary, Finder and shell commands both provide easy methods for finding Python on macOS.

Quick Reference of All Methods By OS

Here’s a quick comparison table summarizing the different techniques across operating systems:

Operating SystemMethodExample
Windowswhere pythonwhere python
Windowssys.executablepython -c “import sys; print(sys.executable)”
WindowsGet-CommandGet-Command python
Linux/Unixwhich pythonwhich python3
Linux/Unixtype -a pythontype -a python
Linux/Unixreadlink -freadlink -f /usr/bin/python
macOSFinder (Ctrl + Shift + G)/Library/Frameworks/Python.framework
macOSwhich pythonwhich python3
Cross-platformsys.executablepython -c “import sys; print(sys.executable)”

This covers some easy, go-to approaches for finding Python installs across different OS environments.

Summary

Finding where Python is installed is essential for managing multiple versions and virtual environments.

We learned platform-specific techniques for locating Python on Windows, Linux/Unix and macOS systems:

  • Windows – Using where python, PowerShell’s Get-Command, and sys.executable
  • Linux/Unix – Leveraging the whichtype -a, and readlink terminal commands
  • macOS – Finding Python through Finder and shell commands like which

These methods help pinpoint the exact folder containing Python installs.

Knowing the install location allows running Python or its packages, modifying installed scripts, switching between environments, and debugging issues, if any.

Whether you just started with Python or have been using it for a while, finding where Python lives on your OS is an important troubleshooting skill.

So next time you need to locate Python, use this guide to find its path across Windows, Linux and macOS platforms.

References: StackOverflow