Windows normally installs Python on one of the two locations:
C:\Python39
C:\Users\YourUser\AppData\Local\Programs\Python\Python39
For me, it’s the latter. For you, it may be different—this article shows you how to check for yourself! π
For your convenience, I’ve made a short gif that shows how I rushed through the code on my Windows machine:
Before you start, you may want to ask yourself the following question:
- Do you have access to a Python interpreter/shell?
- Do you have access to the command-line or PowerShell?
- Do you have neither?
Do You Have Access to a Python Shell?
To get the installation path in a human-readable format, run the following two lines in your Python shell:
- Import the
os
andsys
libraries with the command:import os, sys
- Print the path to the shell using the command:
print(os.path.dirname(sys.executable))
This is shown in the following code snippet:
import os, sys print(os.path.dirname(sys.executable))
The output on my computer is the following installation path:
'C:\Users\xcent\AppData\Local\Programs\Python\Python39'
You can copy it to the clipboard and use it wherever you need it.
An alternative way is shorter but generates an output that is less desirable with double front-slash path delimiters:
import sys
print(sys.executable)
You can see the output in a Python shell on my computer:
import sys print(sys.executable) 'C:\\Users\\xcent\\AppData\\Local\\Programs\\Python\\Python39\\pythonw.exe'
Do You Have Access to the Command-Line or Powershell?
To get the Python installation path under Windows using the command line or PowerShell, you can pack the Python code in a concise and easy-to-copy one-liner command:
python -c "import os, sys; print(os.path.dirname(sys.executable))"
If Python is regularly installed, you can run the python
command from any directory in your PowerShell which makes it extra convenient.
Alternatively, you can check your Python installation path in your Windows command-line by using the simple two-word command “where Python
“. Here’s how this works on my Windows machine:
where python
This gives me the following output:
C:\Users\xcent\AppData\Local\Microsoft\WindowsApps\python.exe
You’ll learn an additional trick next that allows you to check your Python installation path without access to the Python interpreter or the windows command line.
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners
Get Python Installation Path Without Shell
To get the Python installation path on Windows without using either the Python interpreter or the command line, check the Windows registry that contains a key for each installed Python version. Both variants are possible:
HKLM\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
HKCU\SOFTWARE\Python\PythonCore\versionnumber\InstallPath
If you have a 64-bit Windows version, you can find the Python path under the Wow6432Node
key:
HKLM\SOFTWARE\Wow6432Node\Python\PythonCore\versionnumber\InstallPath
You can learn how to use these variants in your code on this post. An even more thorough discussion is provided on the official Python wiki here.