When running a python script in IDLE, is there a way to pass in command line arguments (args)?

前端 未结 11 783
猫巷女王i
猫巷女王i 2020-12-02 13:08

I\'m testing some python code that parses command line input. Is there a way to pass this input in through IDLE? Currently I\'m saving in the IDLE editor and running from a

相关标签:
11条回答
  • 2020-12-02 13:33

    This code works great for me, I can use "F5" in IDLE and then call again from the interactive prompt:

    def mainf(*m_args):
        # Overrides argv when testing (interactive or below)
        if m_args:
            sys.argv = ["testing mainf"] + list(m_args)
    

    ...

    if __name__ == "__main__":
        if False: # not testing?
            sys.exit(mainf())
        else:
            # Test/sample invocations (can test multiple in one run)
            mainf("--foo=bar1", "--option2=val2")
            mainf("--foo=bar2")
    
    0 讨论(0)
  • 2020-12-02 13:36

    Here are a couple of ways that I can think of:

    1) You can call your "main" function directly on the IDLE console with arguments if you want.

    2) You can add a test line in front of your main function call which supplies an array of arguments (or create a unit test which does the same thing), or set sys.argv directly.

    3) You can run python in interactive mode on the console and pass in arguments:

    C:\> python.exe -i some.py arg1 arg2
    
    0 讨论(0)
  • 2020-12-02 13:38

    In a pinch, Seth's #2 worked....

    2) You can add a test line in front of your main function call which supplies an array of arguments (or create a unit test which does the same thing), or set sys.argv directly.

    For example...

    sys.argv = ["wordcount.py", "--count", "small.txt"]
    
    0 讨论(0)
  • 2020-12-02 13:38

    Visual Studio 2015 has an addon for Python. You can supply arguments with that. VS 2015 is now free.

    0 讨论(0)
  • 2020-12-02 13:41

    It doesn't seem like IDLE provides a way to do this through the GUI, but you could do something like:

    idle.py -r scriptname.py arg1 arg2 arg3
    

    You can also set sys.argv manually, like:

    try:
        __file__
    except:
        sys.argv = [sys.argv[0], 'argument1', 'argument2', 'argument2']
    

    (Credit http://wayneandlayne.com/2009/04/14/using-command-line-arguments-in-python-in-idle/)

    0 讨论(0)
  • 2020-12-02 13:46

    There seems like as many ways to do this as users. Me being a noob, I just tested for arguments (how many). When the idle starts from windows explorer, it has just one argument (... that is len(sys.argv) returns 1) unless you started the IDLE with parameters. IDLE is just a bat file on Windows ... that points to idle.py; on linux, I don't use idle.

    What I tend to do is on the startup ...

    if len(sys.argv) == 1
        sys.argv = [sys.argv[0], arg1, arg2, arg3....] <---- default arguments here
    

    I realize that is using a sledge hammer but if you are just bringing up the IDLE by clicking it in the default install, it will work. Most of what I do is call the python from another language, so the only time it makes any difference is when I'm testing.

    It is easy for a noob like me to understand.

    0 讨论(0)
提交回复
热议问题