In your Python interpreter, type the following commands:
>>> import os, sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
Answer from elo80ka on Stack OverflowIn your Python interpreter, type the following commands:
>>> import os, sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
Also, you can club all these and use a single line command. Open cmd and enter following command
python -c "import os, sys; print(os.path.dirname(sys.executable))"
If you have Python in your environment variable then you can use the following command in cmd or powershell:
where python
or for Unix enviroment
which python
command line image :

Quick Question: How do I view Python's location in command prompt?
How do I run a Python program in the Command Prompt in Windows 7? - Stack Overflow
Python 3 for Windows REPL in cmd window?
How do you *actually* use python on your computer?
So I've been having trouble with Python and I want to know which installation I'm using (I have two user profiles on Windows, my old one and the one I made explicitly to code in, and they both have Python installed).
I tried looking it up, but it only led to this madness you see below:
Microsoft Windows [Version 10.0.19043.1348]
(c) Microsoft Corporation. All rights reserved.
C:\Users\willi>!type python
'!type' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\willi>type python
The system cannot find the file specified.
C:\Users\willi>type Python
The system cannot find the file specified.
C:\Users\willi>Python type
Python: can't open file 'C:\Users\willi\type': [Errno 2] No such file or directory
C:\Users\willi>Python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
>>> .quit
File "<stdin>", line 1
.quit
^
SyntaxError: invalid syntax
>>> quit
Use quit() or Ctrl-Z plus Return to exit
>>> quit()
C:\Users\willi>python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> !type Python
File "<stdin>", line 1
!type Python
^
SyntaxError: invalid syntax
>>> type
<class 'type'>
>>> type python
File "<stdin>", line 1
type python
^
SyntaxError: invalid syntax
>>> type Python
File "<stdin>", line 1
type Python
^
SyntaxError: invalid syntax
>>> type(Python)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Python' is not defined
>>>How do I view Python's location in command prompt?
You need to add C:\Python27 to your system PATH variable, not a new variable named "python".
Find the system PATH environment variable, and append to it a ; (which is the delimiter) and the path to the directory containing python.exe (e.g. C:\Python27). See below for exact steps.
The PATH environment variable lists all the locations that Windows (and cmd.exe) will check when given the name of a command, e.g. "python" (it also uses the PATHEXT variable for a list of executable file extensions to try). The first executable file it finds on the PATH with that name is the one it starts.
Note that after changing this variable, there is no need to restart Windows, but only new instances of cmd.exe will have the updated PATH. You can type set PATH at the command prompt to see what the current value is.
Exact steps for adding Python to the path on Windows 7+:
- Computer -> System Properties (or Win+Break) -> Advanced System Settings
- Click the
Environment variables...button (in the Advanced tab) - Edit PATH and append
;C:\Python27to the end (substitute your Python version) - Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
Assuming you have Python2.7 installed
Goto the Start Menu
Right Click "Computer"
Select "Properties"
A dialog should pop up with a link on the left called "Advanced system settings". Click it.
In the System Properties dialog, click the button called "Environment Variables".
In the Environment Variables dialog look for "Path" under the System Variables window.
Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows.
Click Ok and close the dialogs.
Now open up a new command prompt and type "python"
It should work.
I haven't used Windows in decades. I was recently helping a blind friend install Python 3 on his Windows box. He used the download from python.org, but when he opened cmd, and typed python, it popped open a new GUI / IDE window instead of giving him the familiar ">>>" within the already opened cmd window. What's the magic for opening the REPL right there in the window he's in?
Given that you're most familiar with Unix, I'm not sure if you are comfortable with Regedit and the Windows registry.
So let's get you a simple well-regarded tool to handle this task without asking you to manually edit the Windows registry.
First download FileTypesMan from NirSoft. It's written by Nir Sofer, who is a well-known and trusted developer. It's not open-source (unfortunately), but Nir has been publishing free tools for over 15 years with an unblemished record.
Now follow these simple steps within FileTypesMan:
- Find the
.pyfile type in the top pane and click on it. - In the bottom pane, find the
openaction (Namecolumn) and double-click it. - In the ensuing dialog box, change the command line to
"C:\Windows\py.exe" "%1" - In that same dialog box, ensure the
Default Actioncheckbox is checked and theDisabledcheckbox is not checked, then click OK to close the dialog box.
Voilà. You're done.
You can now close FileTypesMan.
Depending on the installed version of Windows, you may need to restart the shell. If you don't know how to do that programmatically, just log off, and log back in. Or, if you prefer, reboot.
As Gerhard mentioned in the comments, there is a significant security risk to allowing scripts to run directly like this, but you mentioned you understand the risks and the tradeoffs are worth it to you. I simply recap those comments here in this answer to help people in the future in the event that the comments get deleted.
...I want to just type
"blah"to executeblah.py
- You can try:1. Add the path to the
Python.exebinary2. Add eXtensions
.pyand.pywto thePathExtvariable3. Run your files
> Your_py_Script > Your_pyw_Script
- How to set the
Pathand environment variables in Windows - What is the
PathExtEnvironment Variable in Windows?
I took an intro to python class, and I don’t understand even just the basics of this. Like, do you open a window, type in the code, and then press a button? Like, if I want to write a program, what applications am I using?
Thanks 🙏