why is python argparse giving the help for imported module rather than current module?

user22866

i have these two simple files: this is tone.py:

import argparse
import os
import sys
root , pyfilename = os.path.split(os.path.abspath(__file__))

try:
    parser = argparse.ArgumentParser()
    parser.add_argument('--argone', help='argument one',default= "one" )
    parser.add_argument('--argtwo', help='argument two',default= "two" )
    parser.add_argument('--argthree', help='argument three',default= "three" )
    parser.add_argument('--argfour', help='argument three',default=False )
    args = parser.parse_args()
except Exception as e:
    print "crapped"

class One():
    pass

if __name__ == "__main__" :
    while True:
        print (args.argone)
        print (args.argtwo)
        print (args.argthree)

and this is ttwo.py:

import argparse
import os
import sys
root , pyfilename = os.path.split(os.path.abspath(__file__))

try:
    from tone  import One
except Exception as e:
    print "cant import module coz: %s ; so i'm exiting"%e
    sys.exit()


try:
    import defaults
    parser = argparse.ArgumentParser()
    parser.add_argument('--arga', help='arga',default= "a" )
    parser.add_argument('--argb', help='argb',default= "b" )
    parser.add_argument('--argc', help='argc',default= "c" )
    parser.add_argument('--argd', help='targd',default=False )
    args = parser.parse_args()
except Exception as e:
    print "crapped"


if __name__ == "__main__" :
    print (args.arga)
    print (args.argb)
    print (args.argc)

now if i run tone.py with the help switch i get what i expect:

$ python  tone.py -h 
usage: tone.py [-h] [--argone ARGONE] [--argtwo ARGTWO] [--argthree ARGTHREE]
               [--argfour ARGFOUR]

optional arguments:
  -h, --help           show this help message and exit
  --argone ARGONE      argument one
  --argtwo ARGTWO      argument two
  --argthree ARGTHREE  argument three
  --argfour ARGFOUR    argument three

but the second one gives unexpected results:

$ python  ttwo.py -h 
usage: ttwo.py [-h] [--argone ARGONE] [--argtwo ARGTWO] [--argthree ARGTHREE]
               [--argfour ARGFOUR]

optional arguments:
  -h, --help           show this help message and exit
  --argone ARGONE      argument one
  --argtwo ARGTWO      argument two
  --argthree ARGTHREE  argument three
  --argfour ARGFOUR    argument three

this is the help for the first module. what happened? and how do i fix it?

Martijn Pieters

Your tone module defines an argument parser at the module level and prints the help message as it parses your command line arguments

If you only want the parsing to take place if tone is being run as a script, move the parser.parse_args() call to your __main__ test block:

if __name__ == '__main__':
    # run as a script, not imported as a module
    args = parser.parse_args()

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to locate a module being imported in python

분류에서Dev

Why installing package and module not same in Python?

분류에서Dev

why "this" points to class rather than class object?

분류에서Dev

Why isn't IPython giving me a full traceback for a module I've written?

분류에서Dev

Establish which class implementation is imported to module

분류에서Dev

Why the dnd module is not supported?

분류에서Dev

Why the other languages rather than java is not platform independent?

분류에서Dev

Why Hard disk labeling starts with C rather than A?

분류에서Dev

Why Java supports arrays of arrays, rather than multidimensional arrays?

분류에서Dev

NameError when trying to use module that has been imported

분류에서Dev

Python looping [Module]

분류에서Dev

Python - missing module

분류에서Dev

Pause plugin module Python

분류에서Dev

Python Module Package with no methods

분류에서Dev

Python: importing a module with parameters?

분류에서Dev

Why a public class needs to be imported in package other than it's own?

분류에서Dev

Python3 on Ubuntu giving errors on help() command

분류에서Dev

Why python3 pip module found the python2 libraries?

분류에서Dev

Python decimal module not being consistent

분류에서Dev

Writing to a file from a python module

분류에서Dev

Python Module Only Found With .py

분류에서Dev

How to find the path of a module in python

분류에서Dev

Import module in python3

분류에서Dev

Python module imports Visual Studio

분류에서Dev

Python No module named, even though the module does appear

분류에서Dev

Why is it bad practice to use from module import *?

분류에서Dev

How to get the current battery statistics using a kernel module?

분류에서Dev

Why does the knockout binding pass the span element's id rather than the anchor element's id?

분류에서Dev

Unity2D - Why does the object rotate around the X/Y axis rather than the Z

Related 관련 기사

  1. 1

    How to locate a module being imported in python

  2. 2

    Why installing package and module not same in Python?

  3. 3

    why "this" points to class rather than class object?

  4. 4

    Why isn't IPython giving me a full traceback for a module I've written?

  5. 5

    Establish which class implementation is imported to module

  6. 6

    Why the dnd module is not supported?

  7. 7

    Why the other languages rather than java is not platform independent?

  8. 8

    Why Hard disk labeling starts with C rather than A?

  9. 9

    Why Java supports arrays of arrays, rather than multidimensional arrays?

  10. 10

    NameError when trying to use module that has been imported

  11. 11

    Python looping [Module]

  12. 12

    Python - missing module

  13. 13

    Pause plugin module Python

  14. 14

    Python Module Package with no methods

  15. 15

    Python: importing a module with parameters?

  16. 16

    Why a public class needs to be imported in package other than it's own?

  17. 17

    Python3 on Ubuntu giving errors on help() command

  18. 18

    Why python3 pip module found the python2 libraries?

  19. 19

    Python decimal module not being consistent

  20. 20

    Writing to a file from a python module

  21. 21

    Python Module Only Found With .py

  22. 22

    How to find the path of a module in python

  23. 23

    Import module in python3

  24. 24

    Python module imports Visual Studio

  25. 25

    Python No module named, even though the module does appear

  26. 26

    Why is it bad practice to use from module import *?

  27. 27

    How to get the current battery statistics using a kernel module?

  28. 28

    Why does the knockout binding pass the span element's id rather than the anchor element's id?

  29. 29

    Unity2D - Why does the object rotate around the X/Y axis rather than the Z

뜨겁다태그

보관