无法访问 Python 3.6 中的命令行参数; python启动器问题?
Unable to access commandline arguments in Python 3.6; python launcher the issue?
我最近终于将我的默认 python 从 2.7 移动到 3.6。
奇怪的是,我无法让 sys.argv 正常工作。
短节目testargs.py:
#!python3
import sys
print("This is the name of the script: ", sys.argv[0])
print("Number of arguments: ", len(sys.argv))
print("The arguments are: " , str(sys.argv))
当我 运行 它带有参数 ("abcdefg") 时,这些参数不会出现在 sys.argv:
C:\test>testargs.py abcdefg
This is the name of the script: C:\test\testargs.py
Number of arguments: 1
The arguments are: ['C:\test\testargs.py']
我对此完全感到困惑。有什么想法吗?
我正在使用 Python 启动器;这是个问题吗?
当我专门调用一个或另一个 Python 解释器时,似乎没问题:
C:\test>c:\Python27\python.exe testargs.py abcdefg
('This is the name of the script: ', 'testargs.py')
('Number of arguments: ', 2)
('The arguments are: ', "['testargs.py', 'abcdefg']")
C:\test>c:\Python36\python.exe testargs.py abcdefg
This is the name of the script: testargs.py
Number of arguments: 2
The arguments are: ['testargs.py', 'abcdefg']
这似乎是 Python 中一个长期存在的难以重现的错误:https://bugs.python.org/issue7936。错误报告包括一个注册表编辑,据说可以解决这个问题,但我不愿意乱搞;所以我会忍受它,直到我完全离开 Python 2.
我最近终于将我的默认 python 从 2.7 移动到 3.6。
奇怪的是,我无法让 sys.argv 正常工作。
短节目testargs.py:
#!python3
import sys
print("This is the name of the script: ", sys.argv[0])
print("Number of arguments: ", len(sys.argv))
print("The arguments are: " , str(sys.argv))
当我 运行 它带有参数 ("abcdefg") 时,这些参数不会出现在 sys.argv:
C:\test>testargs.py abcdefg
This is the name of the script: C:\test\testargs.py
Number of arguments: 1
The arguments are: ['C:\test\testargs.py']
我对此完全感到困惑。有什么想法吗?
我正在使用 Python 启动器;这是个问题吗?
当我专门调用一个或另一个 Python 解释器时,似乎没问题:
C:\test>c:\Python27\python.exe testargs.py abcdefg
('This is the name of the script: ', 'testargs.py')
('Number of arguments: ', 2)
('The arguments are: ', "['testargs.py', 'abcdefg']")
C:\test>c:\Python36\python.exe testargs.py abcdefg
This is the name of the script: testargs.py
Number of arguments: 2
The arguments are: ['testargs.py', 'abcdefg']
这似乎是 Python 中一个长期存在的难以重现的错误:https://bugs.python.org/issue7936。错误报告包括一个注册表编辑,据说可以解决这个问题,但我不愿意乱搞;所以我会忍受它,直到我完全离开 Python 2.