Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows

problem

In Python2.7 on Windows environment For example, if you have the following test.py

import sys

print(len(sys.argv))

Even if I execute it from the command prompt with arguments, sys.argv is always 1.

> test.py a b c
1 // <= NG:Should be 4

There is no problem if you execute it from the python command.

> python test.py a b c
4 // <= OK

Workaround

It was listed as an issue at https://bugs.python.org/issue7936. In conclusion, mess with the registry.

Launch regedit and go to HKEY_CURRENT_USER \ Software \ Classes \ Applications \ python.exe \ shell \ open \ command

スクリーンショット 2016-03-22 午後5.57.40.png

If this value is " C: \ Python27 \ python.exe ""% 1 ", modify it to " C: \ Python27 \ python.exe ""% 1 "% *.

This should allow sys.argv to be passed correctly.

> test.py a b c
4 // OK

Even so, it was a bug reported 6 years ago, so I really wanted it to be fixed.

Recommended Posts

Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows
Workaround for the problem that? Is displayed when checking the version when using non-standard perl | python with pkg version
[Windows] The problem that an error occurs when opening a file other than CP932 (Shift-JIS) encoded in Python has been solved for the time being.
A python script for Mac that zips without garbled characters on Windows
Are Php / Ruby / Python that only runs when the file is called directly
Intuitive explanation that does not rely on mathematical formulas for the Monty Hall problem and simulation with Python
Create an exe file that works in a Windows environment without Python with PyInstaller
[Python] Leave only the elements that start with a specific character string in the array
What's in that variable (when running a Python script)
Python program that looks for the same file name
Solved the problem that the image was not displayed in ROMol when loaded with PandasTools.LoadSDF.
There is a pattern that the program did not stop when using Python threading
When a local variable with the same name as a global variable is defined in the function
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
When reading an image with SimpleITK, there is a problem if there is Japanese in the path
Thorough efficiency improvement! The name is not the only "short circuit evaluation" that has a cool name.
[VLC] How to deal with the problem that it is not in the foreground during playback
I replaced the Windows PowerShell cookbook with a python script.
Test.py is not reflected on the web server in Python3.
Let's create a script that registers with Ideone.com in Python.
Write a script in Shell and Python to notify you in Slack when the process is finished
How to deal with the problem that the current directory moves when Python is executed from Atom
A story that didn't work when I tried to log in with the Python requests module
How is the progress? Let's get on with the boom ?? in Python
Process the contents of the file in order with a shell script
Play a sound in Python assuming that the keyboard is a piano keyboard
Japanese file enumeration with Python2 system on Windows (5C problem countermeasure)
Read a file in Python with a relative path from the program
[Golang] "package exec is not in GOROOT" when executing the test
Build Python environment on Ubuntu (when pip is not the default)
Solve the subset sum problem with a full search in Python
A memo for when pip3 is installed with python2.7 for some reason
[Python] I made a bot that tells me the current temperature when I enter a place name on LINE
A script that retrieves tweets with Python, saves them in an external file, and performs morphological analysis.
The NVM Checksum Is Not Valid, a solution to the problem that Intel's wired LAN is not recognized on Linux
Replace the directory name and the file name in the directory together with a Linux command.
A memo organized by renaming the file names in the folder with python
Extract lines that match the conditions from a text file with python
Read a Python # .txt file for a super beginner in Python with a working .py
A useful note when using Python for the first time in a while
Rock-paper-scissors with Python Let's run on a Windows local server for beginners
I created a script to check if English is entered in the specified position of the JSON file in Python.
Prepare a development environment that is portable and easy to duplicate without polluting the environment with Python embeddable (Windows)
How to deal with "^ [[A ^ [[B ^ [[C ^ [[D"] when you press the arrow keys when executing python on mac
"Cython" tutorial to make Python explosive: Handling when a function on the C ++ side is passed by reference.
Run the Python interpreter in a script
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
Word count that counts only words that start with a capital letter in python
A solution to the problem that the Python version in Conda cannot be changed
Install Docker on CentOS8. The problem that the name could not be resolved was resolved.
A Python script that reads a SQL file, executes BigQuery and saves the csv
A python script that draws a band diagram from the VASP output file EIGENVAL
Output timing is incorrect when standard (error) output is converted to a file in Python
Separately install a version of Python that is not pre-installed on your Mac
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
[Python] Solution to the problem that elements are linked when copying a list
Turn multiple lists with a for statement at the same time in Python
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
I tried searching for files under the folder with Python by file name
I made a Python program for Raspberry Pi that operates Omron's environmental sensor in the mode with data storage
[Python] Get the files in a folder with Python