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.
Running Python scripts through the Windows Command Line - Stack Overflow
Run a Python file in Command Prompt
How do you *actually* use python on your computer?
How to send commands to Windows command line with Python?
Videos
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.
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?
From the book (p. 44, 4th Ed):
Finally, remember to give the full path to your script if it lives in a different directory from the one in which you are working.
For your situation, this means using
C:\User\Example> python C:\User\Example\my_scripts\script1.py
You could write a batch file that looks for the script in a predefined directory:
@echo off
setlocal
PATH=C:\User\Example\Python36;%PATH%
SCRIPT_DIR=C:\User\Example\my_scripts
python %SCRIPT_DIR\%*
it should use (") in your path to your script. for example :
python "C:\Users\Acer\palm-recognizition\src\predict.py"
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 🙏
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!
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 =)
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.
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.)
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
