Find where python is installed (if it isn't default dir)
Unix/Linux/Mac:
For Python 2.x:
Windows:
Using a Python script or interactive session:
These lines of code return the path to the Python executable or binary. The sys.executable
command will give the full path inside any Python environment.
Advanced insights with the sys module
Beyond the Python executable, sys module returns more detailed information:
- sys.exec_prefix provides the installation path of the standard libraries of Python.
- sys.path contains all locations (directories) where python finds its modules.
Try this:
Note: The command-line tools like which
or where
can fail if Python's binary is not included in the system PATH
.
Dealing with multiple Python installations
Python can pose a challenge when found in virtual environments or instances with multiple versions installed. Use these commands:
Unix/Linux/Mac:
This lists all locations for an executable named python3
.
Windows: Using PowerShell:
For virtual environments, remember to enable it first. Then run sys.executable
to get the accurate path.
Common traps you might fall into
While locating Python installation there can be few common obstacles:
- Python 2 vs Python 3: Your commands may end up returning the location of undesired version if both Python 2 and 3 are installed.
- Different shells: Since different shells have different
PATH
configurations, yourwhich
orwhere
output may vary. - Symbolic links:
which
can sometimes return a path that is a symbolic link, not the actual installation directory.
Expert level: Unmasking Python
The hidden corners of Python installations can be reached with some expert-level commands:
Absolute Pythonistic path
Readlink command for Unix users can help you unmask the symbolic links and determine the absolute path to the Python executable as follows:
Python and your shell
Bash users can find the history of executed python commands with:
PS: It's like going on a treasure hunt in your own terminal, who knows what you might find there!
Varsity vars
figuring out your environment variables can also provide some interesting clues. Specifically PATH
, PYTHONHOME
, and PYTHONPATH
.
Try these:
Was this article helpful?