How to find the full path of the Python interpreter?
Last Updated :
28 May, 2024
Method 1: Using the sys
Module (Windows, Ubuntu, macOS)
The sys
module provides access to variables and functions that interact with the Python interpreter. The sys.executable
attribute gives the absolute path of the Python interpreter.
Steps:
- Import the
sys
module. - Print the path.
Example:
Python
import sys
# Print the full path of the Python interpreter
print(sys.executable)
Output/usr/local/bin/python3
Explanation:
sys.executable
returns a string representing the path to the Python interpreter binary.
Method 2: Using the os
Module (Windows, Ubuntu, macOS)
The os
module allows you to interact with the operating system. You can use it to get the real path of the Python interpreter.
Steps:
- Import the
os
and sys
modules. - Get and print the real path.
Example:
Python
import os
import sys
# Get the real path of the Python interpreter
interpreter_path = os.path.realpath(sys.executable)
print(interpreter_path)
Output/usr/local/bin/python3.7
Explanation:
os.path.realpath()
returns the canonical path, resolving any symbolic links.
Method 3: Using the pathlib
Module (Windows, Ubuntu, macOS)
The pathlib
module provides an object-oriented approach to handling filesystem paths.
Steps:
- Import the
pathlib
and sys
modules. - Get and print the path.
Example:
Python
from pathlib import Path
import sys
# Get the path of the Python interpreter
interpreter_path = Path(sys.executable)
print(interpreter_path)
Output/usr/local/bin/python3
Explanation:
Path(sys.executable)
converts the string path to a Path
object, providing more functionality for path operations.
Method 4: Using the which
Command (Ubuntu, macOS)
On Unix-based systems like Ubuntu and macOS, you can use the which
command to find the path of the Python interpreter.
Steps:
- Open Terminal.
- Type the command.
Example:
which python
# or for Python 3
which python3
Explanation:
- The
which
command returns the path of the executable that would run if the command was entered.
Method 5: Using the where
Command (Windows)
On Windows, you can use the where
command in the Command Prompt to find the path of the Python interpreter.
Steps:
- Open Command Prompt.
- Type the command.
Example:
where python
Explanation:
- The
where
command displays the locations of executables that match the given name.
Method 6: Using the Get-Command
Command (Windows PowerShell)
On Windows, you can use PowerShell to find the path of the Python interpreter using the Get-Command
cmdlet.
Steps:
- Open PowerShell.
- Type the command.
Example:
Get-Command python
# or for Python 3
Get-Command python3
Explanation:
- The
Get-Command
cmdlet retrieves all commands installed on the system, including their paths.
Similar Reads
How to import a Python module given the full path? The Python module is a file consisting of Python code with a set of functions, classes, and variable definitions. The module makes the code reusable and easy to understand. The program that needs to use the module should import that particular module. In this article, we will discuss how to import a
5 min read
Find path to the given file using Python We can get the location (path) of the running script file .py with __file__. __file__ is useful for reading other files and it gives the current location of the running file. It differs in versions. In Python 3.8 and earlier, __file__ returns the path specified when executing the Python command. We
5 min read
How to remove all .pyc files in Python? In this article, we are going to see how to remove all .pyc files in Python. What is a .pyc file? A *.pyc file is created by the Python interpreter when a *.py file is imported into any other python file. The *.pyc file contains the "compiled bytecode" of the imported module/program so that the "tra
2 min read
What is Python Interpreter Well if you are new to Python you have come across the word interpreters but before knowing about Python interpreters, we need to what is an interpreter, what it will do, the advantages and disadvantages of the interpreter, and so on. Well in this handy article, we will cover the Importance of Inter
4 min read
How to add Python to Windows PATH? Python is a great language! However, it doesnât come pre-installed with Windows. Hence we download it to interpret the Python code that we write. But wait, windows donât know where you have installed the Python so when trying to any Python code, you will get an error. We will be using Windows 10 and
2 min read