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+:

  1. Computer -> System Properties (or Win+Break) -> Advanced System Settings
  2. Click the Environment variables... button (in the Advanced tab)
  3. Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
  4. Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
Answer from Cameron on Stack Overflow
🌐
Real Python
realpython.com › run-python-scripts
How to Run Your Python Scripts and Code – Real Python
February 25, 2026 - You can run Python scripts from the command line using python script.py, directly by making files executable with shebangs on Unix systems, or through IDEs and code editors. Python also supports interactive execution through the standard REPL ...
🌐
Python
docs.python.org › 3 › faq › windows.html
Python on Windows FAQ — Python 3.14.6 documentation
When you want to end your interactive Python session, call the exit() function or hold the Ctrl key down while you enter a Z, then hit the “Enter” key to get back to your Windows command prompt. You may also find that you have a Start-menu entry such as Start ‣ Programs ‣ Python 3.x ‣ Python (command line) that results in you seeing the >>> prompt in a new window. If so, the window will disappear after you call the exit() function or enter the Ctrl-Z character; Windows is running a single “python” command in the window, and closes it when you terminate the interpreter.
Discussions

Running Python scripts through the Windows Command Line - Stack Overflow
In his book he offers an example ... the Windows shell. In the example, he calls is as follows: ... to run a script that I've written and placed in my desired directory. My issue is that I can not call ... in my command line the same way he does in the book. He's mentioned something about a PYTHONPATH Environment Variable, however, this variable isn't present on my ... More on stackoverflow.com
🌐 stackoverflow.com
Run a Python file in Command Prompt
How to run a Python file in Windows Command Prompt using the imput command? More on discuss.python.org
🌐 discuss.python.org
4
0
December 21, 2023
How do you *actually* use python on your computer?
Usually people will use an Integrated Development Environment (IDE) to program. This is not exclusive to Python. Practically all popular programming languages have an IDE that supports said language. In the case of Python, probably the most popular IDE is PyCharm, by JetBrains. Python doesn't need an IDE to run, though. The Python interpreter can be ran using any shell. If you don't know what a shell is: the most popular in Linux is bash (often just called the terminal), and Windows uses the command prompt (currently being phased out/revamped) or the PowerShell. These are just programs that search for and run programs supplied by user input (text). Whether you're running an IDE or a shell: running the interpreter is done more or less the same way: the interpreter is simply a program that is executed. It receives input (in the form of python code), and those instructions are interpreted to make the computer behave a certain way (store a variable in memory, run a loop, jump to a function, etc.). More on reddit.com
🌐 r/learnprogramming
116
687
February 22, 2020
How to send commands to Windows command line with Python?
Sounds like something that should just be a batch file. More on reddit.com
🌐 r/learnpython
14
3
November 4, 2020
Top answer
1 of 16
234

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+:

  1. Computer -> System Properties (or Win+Break) -> Advanced System Settings
  2. Click the Environment variables... button (in the Advanced tab)
  3. Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
  4. Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
2 of 16
66

Assuming you have Python2.7 installed

  1. Goto the Start Menu

  2. Right Click "Computer"

  3. Select "Properties"

  4. A dialog should pop up with a link on the left called "Advanced system settings". Click it.

  5. In the System Properties dialog, click the button called "Environment Variables".

  6. In the Environment Variables dialog look for "Path" under the System Variables window.

  7. Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows.

  8. Click Ok and close the dialogs.

  9. Now open up a new command prompt and type "python"

It should work.

🌐
Microsoft Learn
learn.microsoft.com › en-us › windows › dev-environment › python
Python on Windows for beginners | Microsoft Learn
April 15, 2026 - To help new users find a good installation of Python, we added a shortcut to Windows that will take you directly to the latest version of the community's package published in the Microsoft Store. This package can be installed easily, without administrator permissions, and will replace the default python and python3 commands with the real ones. Running the shortcut executable with any command-line arguments will return an error code to indicate that Python was not installed.
Top answer
1 of 2
1

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:

  1. Find the .py file type in the top pane and click on it.
  2. In the bottom pane, find the open action (Name column) and double-click it.
  3. In the ensuing dialog box, change the command line to "C:\Windows\py.exe" "%1"
  4. In that same dialog box, ensure the Default Action checkbox is checked and the Disabled checkbox 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.

2 of 2
1

...I want to just type "blah" to execute blah.py
- You can try:

1. Add the path to the Python.exe binary

2. Add eXtensions .py and .pyw to the PathExt variable

3. Run your files

> Your_py_Script
> Your_pyw_Script

  • How to set the Path and environment variables in Windows
  • What is the PathExt Environment Variable in Windows?
🌐
Python
docs.python.org › 3 › using › windows.html
4. Using Python on Windows — Python 3.14.6 documentation
As many Python scripts written to work on Unix will already have this line, you should find these scripts can be used by the launcher without modification. If you are writing a new script on Windows which you hope will be useful on Unix, you should use one of the shebang lines starting with /usr. Any of the above virtual commands can have <ALIAS> replaced by an alias from an installed runtime...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-use-cmd-for-python-in-windows-10
How to use CMD for Python in Windows 10 - GeeksforGeeks
June 21, 2025 - Now close the Environment menus by clicking ok and congratulations, we have set up the Command Prompt for Python. Now check whether it works. Open Command Prompt and type "python" and hit enter. You will see a python version and now you can ...
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › python › how to run a python file from the command prompt (cmd)
How to Run a Python File from the Command Prompt (CMD)
October 8, 2025 - You can do this easily by pressing the Windows key, typing cmd, and clicking Command Prompt. It's at the top of the Start menu. Doing so will open Command Prompt. ... Switch to your Python file's directory.
🌐
LearnPython.com
learnpython.com › blog › run-python-script-windows
How to Run a Python Script in Windows | LearnPython.com
Once the command prompt is open, simply type python and hit Enter again. When you’re in an interactive session, every Python statement is executed immediately and any output is displayed directly beneath.
🌐
Oxylabs
oxylabs.io › blog › how-to-run-python-on-windows
How to Run Python on Windows: Step-by-Step Instructions
To run a Python code on Windows, open Command Prompt (cmd.exe), navigate to your program's directory using the cd command, type python filename.py, and press Enter.
🌐
Python.org
discuss.python.org › python help
Run a Python file in Command Prompt - Python Help - Discussions on Python.org
December 21, 2023 - How to run a Python file in Windows Command Prompt using the imput command?
🌐
Reddit
reddit.com › r/learnprogramming › how do you *actually* use python on your computer?
r/learnprogramming on Reddit: How do you *actually* use python on your computer?
February 22, 2020 -

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 🙏

🌐
Adafruit
learn.adafruit.com › using-python-on-windows-10 › command-line-python
Command Line Python | Using Python on Windows 10 | Adafruit Learning System
June 3, 2019 - If you have not used Python before ... Store to download the components. Follow the previous page if it does so. Typing the word idle in the command prompt runs the IDLE Python editor, just like installing the app on the previous page. Rather than showing up in the command prompt window, a new IDLE ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-run-a-python-script
How to Run a Python Script - GeeksforGeeks
Run the following lines one by one in the interactive mode. a = 1 b = 3 if a > b: print("a is Greater") else: print("b is Greater") ... Note: To exit from this mode, press ‘Ctrl+Z’ and then press ‘Enter’ or type ‘exit()’ and then press Enter. Running Python scripts on Windows via the command line provides a direct and efficient way to execute code.
Published   October 2, 2025
🌐
Quora
quora.com › How-do-I-run-Python-in-my-command-prompt
How to run Python in my command prompt - Quora
Answer (1 of 2): I will show you how to do it in windows 10 cmd. Firstly you need python installed…you can get it in Welcome to Python.org After you have installed it go to its locations…that would probably be on C drive or any other you might have installed it in.
🌐
Reddit
reddit.com › r/learnpython › how to send commands to windows command line with python?
r/learnpython on Reddit: How to send commands to Windows command line with Python?
November 4, 2020 -

Hi! I have a list of commands to run in Windows command line sequentially. I want to write a Python script that automates this process. The program should do something like this:

-Open Windows cmd.exe

-Input a command defined in the Python script e.g. "syteminfo"

-Once that command is done executing keep the terminal open

-Input a second command defined in the program e.g. "powercfg /batteryreport"

-Keep going until all the commands have been executed

-Display an all done message

Thanks in advance!

Top answer
1 of 3
1

For specific python files from projects that have a virtual environment (which should be all of them, at this point) that you want to invoke anywhere, any time, you can create a .bat file that runs the python from your virtual environment (full path), with the full path for your script as argument. Then just have that .bat file live in a dir that's in your PATH (so you generally want a dedicated folder for that so you can put as many .bat files in there as you need to run distinct, known, python scripts)

For example, if you have a D:\Temp\test.py and your virtual environment lives in D:\Temp\venv, then you create a .bat file containing:

D:\Temp\venv\Scripts\python.exe D:\Temp\test.py`

And you just make sure that your user's PATH includes the folder that .bat file lives in.

If, however, you want to be able to execute arbitrary python scripts by just double-clicking them, or typing the (full path) name in ps/cmd, then you need to hit up Settings, get to Choose default apps by file types, find the .py extension, and then tell Windows to use Python, rather than whatever code editor is presumably currently set to open those files when you double-click/run them.

Done, any .py file you run without explicitly invokine it as argument to a python interpreter will now run with whatever Windows thinks "pyhon" is. Which you can find out using the instructions mentioned in https://stackoverflow.com/a/647798/740553

Which might not be the version you want at all, which is why virtual environments are a thing =)

2 of 3
0

Solution for Windows and Linux

So you would need python installed and added to path (Which it does by default so no need to worry). Then you would need to take the directory where your .py file is and add it to the path (Make sure to add the dir/folder where the file is not the file itself).

Windows Solution

For windows it pretty simple, look for a application called Edit the system environment variables, open it and double click the text that says path. then press new and add the path of the parent dir of the file.

Linux Solution

For Linux its also pretty simple, all you need to do is open your terminal and run the following command nano .bashrc then go to the bottom of the file and paste the following export PATH="/Directory1:$PATH" and replace Directory1 with the dir you need.

Final Steps (For all Operating systems)

Now just run python your_file_name.py or python3 your_file_name.py and replace your_file_name with the name of the file that you are running.

🌐
Python
docs.python.org › 3.3 › using › windows.html
3. Using Python on Windows — Python 3.3.7 documentation
September 19, 2017 - At the “Customize Python 3.3” screen, an option called “Add python.exe to search path” can be enabled to have the installer place your installation into the %PATH%. This allows you to type python to run the interpreter. Thus, you can also execute your scripts with command line options, ...
Top answer
1 of 7
106

The newer subprocess.check_output and similar commands are supposed to replace os.system. See this page for details. While I can't test this on Windows (because I don't have access to any Windows machines), the following should work:

Copyfrom subprocess import check_output
check_output("dir C:", shell=True)

check_output returns a string of the output from your command. Alternatively, subprocess.call just runs the command and returns the status of the command (usually 0 if everything is okay).

Also note that, in python 3, that string output is now bytes output. If you want to change this into a string, you need something like

Copyfrom subprocess import check_output
check_output("dir C:", shell=True).decode()

If necessary, you can tell it the kind of encoding your program outputs. The default is utf-8, which typically works fine, but other standard options are here.

Also note that @bluescorpion says in the comments that Windows 10 needs a trailing backslash, as in check_output("dir C:\\", shell=True). The double backslash is needed because \ is a special character in python, so it has to be escaped. (Also note that even prefixing the string with r doesn't help if \ is the very last character of the string — r"dir C:\" is a syntax error, though r"dir C:\ " is not.)

2 of 7
20

You would use the os module system method.

You just put in the string form of the command, the return value is the windows enrivonment variable COMSPEC

For example:

os.system('python') opens up the windows command prompt and runs the python interpreter