简体   繁体   中英

command line arguments are not retrieved

Note: I've already tried every solution on stackoverflow!

I'm using windows 10 and python 3.5. I'm getting this error:

Traceback (most recent call last):
File "geiger_plot.py", line 236, in <module>
    main()
TypeError: main() missing 1 required positional argument: 'argv'

when I try to execute the script using every imaginable commands:

geiger_plot.py test
python geiger_plot.py test
"C:\Program Files\Python35\python.exe" geiger_plot.py test
"C:\Program Files\Python35\python.exe" C:\Users\Chaosuser\Desktop\GeigerLog\geiger_plot.py test test test

I already fixed the registry entries for python command line argument:

[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
@="\"C:\\Program Files\\Python35\\python.exe\" \"%1\" %*"

[HKEY_CLASSES_ROOT\Applications\pythonw.exe\shell\open\command]
@="\"C:\\Program Files\\Python35\\pythonw.exe\" \"%1\" %*"

(This is from an exported .reg - that's why there are thousands of backslashes.) In the script itself I made everything right as far as I know. It's a script I want to execute from another script using an argument to pass it a file path ( logfile ). That always gave me errors, so I tried to run it from a shell directly.

import sys

def main(argv):
    sys.argv
    print(sys.argv)
    logfile = str(sys.argv[1])

It just doesn't even get there to give me errors about my use of sys.argv !

What is wrong?

SOLUTION was to remove argv:

def main(argv):
    print(sys.argv)
to
def main():
    print(sys.argv)

Man... python is made so easy its hard again!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM