Windows가 셸에서 실행되는 Python 프로그램에 명령 줄 인수를 전달하지 않습니다.
Windows 명령 셸에서 실행 가능한 명령으로 직접 실행하려고하면 명령 줄 인수를 Python 프로그램에 전달하는 데 문제가 있습니다. 예를 들어이 프로그램 (test.py)이있는 경우 :
import sys
print "Args: %r" % sys.argv[1:]
그리고 실행 :
>test foo
Args: []
비교하자면:
>python test.py foo
Args: ['foo']
내 구성은 다음과 같습니다.
PATH=...;C:\python25;...
PATHEXT=...;.PY;....
>assoc .py
.py=Python.File
>ftype | grep Python
Python.CompiledFile="C:\Python25\python.exe" "%1" %*
Python.File="C:\Python25\python.exe" "%1" %*
Python.NoConFile="C:\Python25\pythonw.exe" "%1" %*
나는 이것을 해결했다고 생각한다. 어떤 이유로 레지스트리에 SECOND 위치가 있습니다 (HKEY_CLASSES_ROOT \ Python.File \ shell \ open \ command에 저장된 파일 연결에 의해 표시되는 위치 외에) :
[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
@="\"C:\\Python25\\python.exe\" \"%1\" %*"
이것은 내 시스템의 제어 설정 인 것 같습니다. 위의 레지스트리 설정은 "% *"를 추가하여 모든 인수를 python.exe에 전달합니다 (어떤 이유로 내 레지스트리에 누락되었습니다).
내 설정은 또 다른 레지스트리 키인 HKEY_CLASSES_ROOT\py_auto_file
. 언급 된 다른 키도 존재했지만 Windows는 어떤 이유로이 키를 사용했습니다.
Windows 7의 Python 3.3의 경우 내 설정은 다른 레지스트리 키에있었습니다. 인수를 전달하기 위해 변경 한 핵심은
HKEY_USERS\S-1-5-21-3922133726-554333396-2662258059-1000_Classes\py_auto_file\shell\open\command
이었다 "C:\Python\Python33\python.exe" "%1"
. 나는 %*
그것에 덧붙였다 . 이제 키 값은 "C:\Python\Python33\python.exe" "%1" %*
입니다.
값 "C:\Python\Python33\python.exe" "%1"
이 있는 다른 키가 여러 개 (적어도 5 개 이상) 있었지만이 키를 변경하여 작동했습니다.
나를 위해 작동하게하려면 레지스트리 경로를 사용해야했습니다.
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
그리고 추가 %*
다음은 Python 3.6, 2.7 및 Anaconda3에서 수정할 .reg 파일입니다.
python-3.6.0.reg
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.py]
@="Python.File"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\.pyc]
@="Python.CompiledFile"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\.pyw]
@="Python.NoConFile"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\py_auto_file]
[HKEY_CLASSES_ROOT\py_auto_file\DefaultIcon]
@="C:\\Python36\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\py_auto_file\shell\open\command]
@="\"C:\\Python36\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.File]
@="Python File"
[HKEY_CLASSES_ROOT\Python.File\DefaultIcon]
@="C:\\Python36\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
@="\"C:\\Python36\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.CompiledFile]
@="Compiled Python File"
[HKEY_CLASSES_ROOT\Python.CompiledFile\DefaultIcon]
@="C:\\Python36\\DLLs\\pyc.ico"
[HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command]
@="\"C:\\Python36\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.NoConFile]
@="Python File (no console)"
[HKEY_CLASSES_ROOT\Python.NoConFile\DefaultIcon]
@="C:\\Python36\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command]
@="\"C:\\Python36\\python.exe\" \"%1\" %*"
python-2.7.0.reg
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.py]
@="Python.File"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\.pyc]
@="Python.CompiledFile"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\.pyw]
@="Python.NoConFile"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\py_auto_file]
[HKEY_CLASSES_ROOT\py_auto_file\DefaultIcon]
@="C:\\Python27\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\py_auto_file\shell\open\command]
@="\"C:\\Python27\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.File]
@="Python File"
[HKEY_CLASSES_ROOT\Python.File\DefaultIcon]
@="C:\\Python27\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
@="\"C:\\Python27\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.CompiledFile]
@="Compiled Python File"
[HKEY_CLASSES_ROOT\Python.CompiledFile\DefaultIcon]
@="C:\\Python27\\DLLs\\pyc.ico"
[HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command]
@="\"C:\\Python27\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.NoConFile]
@="Python File (no console)"
[HKEY_CLASSES_ROOT\Python.NoConFile\DefaultIcon]
@="C:\\Python27\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command]
@="\"C:\\Python27\\python.exe\" \"%1\" %*"
ananconda3.reg (사용자 이름 변경)
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.py]
@="Python.File"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\.pyc]
@="Python.CompiledFile"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\.pyw]
@="Python.NoConFile"
"Content Type"="text/plain"
[HKEY_CLASSES_ROOT\py_auto_file]
[HKEY_CLASSES_ROOT\py_auto_file\DefaultIcon]
@="C:\\Users\\username\\Anaconda3\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\py_auto_file\shell\open\command]
@="\"C:\\Users\\username\\Anaconda3\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.File]
@="Python File"
[HKEY_CLASSES_ROOT\Python.File\DefaultIcon]
@="C:\\Users\\username\\Anaconda3\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
@="\"C:\\Users\\username\\Anaconda3\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.CompiledFile]
@="Compiled Python File"
[HKEY_CLASSES_ROOT\Python.CompiledFile\DefaultIcon]
@="C:\\Users\\username\\Anaconda3\\DLLs\\pyc.ico"
[HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command]
@="\"C:\\Users\\username\\Anaconda3\\python.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Python.NoConFile]
@="Python File (no console)"
[HKEY_CLASSES_ROOT\Python.NoConFile\DefaultIcon]
@="C:\\Users\\username\\Anaconda3\\DLLs\\py.ico"
[HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command]
@="\"C:\\Users\\username\\Anaconda3\\python.exe\" \"%1\" %*"
흥미 롭군. Python 2.6 및 Windows XP (5.1.2600)를 사용하여 여기에서 작동합니다.
C:\Documents and Settings\hbrown>python test.py foo
['test.py', 'foo']
C:\Documents and Settings\hbrown>test.py foo
['C:\\Documents and Settings\\hbrown\\test.py', 'foo']
C:\Documents and Settings\hbrown>test foo
['C:\\Documents and Settings\\hbrown\\test.py', 'foo']
C:\Documents and Settings\hbrown>type test.py
import sys
print sys.argv
C:\Documents and Settings\hbrown>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY
C:\Documents and Settings\hbrown>assoc .py
.py=Python.File
Your program associations for .py
files might be messed up. Just re-associate .py
files with your python executable.
Right click a .py
file > Open with
> Choose default program ...
> [find C:\PythonXY\python.exe]
I checked all registry keys with python.exe
and py_auto_file
and made them point to my current python installation including th %*
at the end that passes arguments. They were quite a few:
HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command:
- org: "C:\miniconda3\python.exe" "%1" "%*"
- changed: "C:\Python35\python.exe" "%1" "%*"
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
- org: "C:\Program Files\Sublime Text 3\sublime_text.exe" "%1"
- changed: "C:\Python35\python.exe" "%1" "%*"
HKEY_CURRENT_USER\Software\Classes\py_auto_file\shell\open\command
- org: "C:\Python35\python.exe" "%1" "%*"
HKEY_USERS\S-1-5-21-2621213409-1291422344-4183577876-2165\Software\Classes\py_auto_file\shell\open\command
- org: "C:\Python35\python.exe" "%1" "%*"
HKEY_USERS\S-1-5-21-2621213409-1291422344-4183577876-2165_Classes\py_auto_file\shell\open\command
- org: "C:\Python35\python.exe" "%1" "%*"
HKEY_CLASSES_ROOT\Applications\pythonw.exe\shell\open\command
- org: "C:\Python34\pythonw.exe" "%1"
- changed: "C:\Python35\pythonw.exe" "%1" "%*"
HKEY_CURRENT_USER\Software\Classes\Applications\python.exe\shell\open\command
- org: "C:\Python35\python.exe" "%1" "%*"
But that didn't do the job for me. I had to change my default python application as well.
As one can see I have 3 Python versions installed. It is impossible to see which is which here so I tried all three of them as my default python application. Eventually I was able to get my script arguments with one of these three.
By looking through the Windows registry, I found all the places where anything like Python36\pythonw.exe "%1" %*
appears.
When I type python app.py args
at the command prompt, everything works properly.
When I use just the app name (app.py args
) Windows opens app.py in Python, but the app fails when it tries to access argv[1], because len(argv) is 1.
Apparently Windows knows enough to pass a py file to Python, but I can't figure out from looking at registry entries how it constructs the command. It appears to be using "%1"
rather than "%1" %*
.
If fixed this on my Windows 10 system by editing the following registry keys:
Computer\HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
Computer\HKEY_CLASSES_ROOT\Python.File\Shell\Open\Command
Computer\HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
to this value:
"C:\Python27\python.exe" "%1" %*
'your programing' 카테고리의 다른 글
한 위치에서 다른 위치로 폴더 구조 (sans 파일) 복사 (0) | 2020.10.06 |
---|---|
비밀번호 필드의 첫 글자에 대해 기본적으로 대문자로 설정되는 iPhone 브라우저 (0) | 2020.10.06 |
Android의 클래스 R은 무엇입니까? (0) | 2020.10.05 |
Haskell (때때로)이 "Best Imperative Language"로 불리는 이유는 무엇입니까? (0) | 2020.10.05 |
사용하지 않는 줄을 주석 처리 한 후 Switch-case가 컴파일되지 않습니다. (0) | 2020.10.05 |