Windows is not passing command line arguments to Python programs executed from the shell
I think I solved this. For some reason there is a SECOND place in the registry (besides that shown by the file associations stored in HKEY_CLASSES_ROOT\Python.File\shell\open\command):
[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
@="\"C:\\Python25\\python.exe\" \"%1\" %*"
This seems to be the controlling setting on my system. The registry setting above adds the "%*" to pass all arguments to python.exe (it was missing in my registry for some reason).
Command-line arguments dropped when running python file directly on Windows
This is a guess, I cannot test right now, but I believe this is what is happening:
- you type the name of your python file.
- Windows fails to run the file as a program, so…
- Windows tries to open the file, using the associated program (kindof with using
start
). - While doing so, it simply ignores other things on the line, and…
- …generates a basic command line for the python
interpreter to use, featuring just the interpreter itself and the
target file to open.
Think of what happens when you "run" a text document.
If the command line is not generated correctly, running this command should fix it (replace the path as appropriate):
ftype Python.File=C:\Path\to\python.exe "%1" %*
Notice the %*
at the end. If it's amiss, arguments will be dropped.
Can't pass zero arguments to Python program running on Windows command line
You have unneeded double quotes around %*
in your registry record. They make Windows pass an empty parameter if there are no arguments. Just remove these quotes.
Run Python scripts from Windows command line, argument not passed
To move the answer to SO (rather than the link in Jon's answer):
Modifying the following two registries so that the arguments are passed along to Python:
HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
Add %*
to the existing "C:\PythonXX\python.exe" "%1"
, so that the key now looks like: "C:\PythonXX\python.exe" "%1" %*
.
Source: http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/
command line arguments are not retrieved
Remove the argv
parameter from main's signature and just use sys.argv[1]
:
def main():
print(sys.argv[1])
You also need to call main :
if __name__ == '__main__':
main()
Execution of Python script with parameter and Windows10 doesn't work
Sorry, after searching with Google instead of hulbee, I found the answer:
Windows is not passing command line arguments to Python programs executed from the shell
[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
@="\"C:\Python25\python.exe\" \"%1\" %*"
Thanks to mckoss
To make it working for me, I'd to use the registry path:
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
and added a %*
to the parameter
Python Command Line Arguments (Windows)
try to run the script using python script.py test.txt
, you might have a broken association of the interpreter with the .py
extention.
Related Topics
Django Filefield with Upload_To Determined at Runtime
Pylint "Unable to Import" Error - How to Set Pythonpath
How to Pass a Method as a Parameter in Python
How to Check a String for Specific Characters
How to Check If There Are Duplicates in a Flat List
How to Put Individual Tags for a Matplotlib Scatter Plot
Why Do I Need 'B' to Encode a String with Base64
Differencebetween Drawing Plots Using Plot, Axes or Figure in Matplotlib
How to Get Rid of "Unnamed: 0" Column in a Pandas Dataframe Read in from CSV File
How to Remove Nan Values from a Numpy Array
How to See If There's an Available and Active Network Connection in Python
How to Access Outer Class from an Inner Class
How to Check If a String in Python Is in Ascii
How to Check Mousebuttonpress Event in Pyqt6