Determining the Install Path of Python from the Command Line

How To Get Python Installed Path From Command Line

Knowing where your files are located in your local system is always essential. It helps you to easily access folders and manipulate them via various applications or commands. You can always change the path of your installed applications through more than one method in Python.

An absolute or relative path of a particular file or folder in your computer will give you an idea of the file hierarchy system. It is the location of a text, photo or video stored inside your laptop or desktop.

We must know the path to the Python GUI to write our programs. Otherwise, it becomes difficult to install modules or libraries in the future. If our Python is stored in the D drive and the modules are stored in C, it might cause problems when we try to import them and use them in our programs.

Adding Python to the PATH Environment Variable

You must have seen in tutorials regarding Python installation, it is always advised to add Python to PATH. What does this mean? It means that you are making Python commands accessible from your command prompt. It allows you to access the python shell from your command prompt.

This is important for future use because you will be using the PIP(the full form is pip installs packages) which is a package installer for Python. Hence, if Python is added to your PATH then it becomes easier and more accessible via the command prompt for future installations.

Method 1: Locating Python in Windows Using the ‘where’ Command

In windows, go to your search bar, and write cmd or command prompt in it. Or, press windows key + R, and open “cmd”.

The Command Prompt
The Command Prompt

Now we’ll run the following command in our command prompt to find the location of Python in our computer.

where python

Now press “Enter”. The keyword “where” will give you the absolute path of Python as shown below.

C:\Users\SHREYA\AppData\Local\Programs\Python\Python311\python.exe
C:\Users\SHREYA\AppData\Local\Microsoft\WindowsApps\python.exe
Using The Where Keyword
Using the ‘where’ Keyword

Method 2: Locating Python in Windows Using the ‘–list-paths’ Option

There is another way to locate your Python installation files using the command line. You can use the list keyword to locate Python. Run the following command in your command prompt. This command also tells you about the version installed.

py --list-paths

The output would be:

 -V:3.11 *        C:\Users\SHREYA\AppData\Local\Programs\Python\Python311\python.exe
Using The List Keyword
Using The List Keyword

Method 3: Locating Python in Windows Using the ‘python’ Command

In windows, this is the most simple way to find out information of Python. Just write the word python in your command prompt and hit “Enter” as shown below.

python

The output would be something as follows:

Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Using The Python Word
Using The Python Word

Similar: Extract File Name from Path for Any OS/Path Format.

Method 4: Locating Python in Windows Using the ‘os’ and ‘sys’ Modules

The python os module and the systems module(sys) are modules that are in built and comes preinstalled with the default Python installation. They help us interact with the operating system via Python code. You can also import them and run commands to find the location of the Python executable file on your computer in the following way:

python
>>>import os
>>>import sys
>>>print(os.path.dirname(sys.executable))

The output will give you the location of the Python.exe file in your system.

C:\Users\SHREYA\AppData\Local\Programs\Python\Python311
Using The O And Sys Modules From Python Itself
Using The O And Sys Modules From Python Itself

Also check out: How to List Files in a Directory Using Python?

Locating Python on a Linux System

On your Linux system, open the terminal and type in:

which python or which python3

This will give you the current active location of Python on your Linux system. It is mostly found in the user/bin/ directory.

Locating Python on a macOS System

On your mac, click on the launchpad icon and search for the terminal. In your Terminal, type the following:

which python

This will give you the absolute path of Python on your mac. It is mainly stored in /Library/Frameworks/Python/Versions

Conclusion: Locate the Python Installation Path Effortlessly

In this article, we have seen how important it is to add Python to PATH to make it accessible through our command prompt. On Windows, there are 4 different methods via which you can find the location where your Python is present. On other operating systems such as Linux and macOS, there are also ways to determine the installed Python path. All of these methods are straightforward and can help you locate the Python installation directory for future package or module installations.