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

前端 未结 11 784
猫巷女王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:49

    Command-line arguments have been added to IDLE in Python 3.7.4+. To auto-detect (any and older) versions of IDLE, and prompt for command-line argument values, you may paste (something like) this into the beginning of your code:

    #! /usr/bin/env python3
    
    import sys
    
    def ok(x=None):
          sys.argv.extend(e.get().split())
          root.destroy()
    
    
    if 'idlelib.rpc' in sys.modules:
    
          import tkinter as tk
    
          root = tk.Tk()
          tk.Label(root, text="Command-line Arguments:").pack()
    
          e = tk.Entry(root)
          e.pack(padx=5)
    
          tk.Button(root, text="OK", command=ok,
                    default=tk.ACTIVE).pack(pady=5)
    
          root.bind("<Return>", ok)
          root.bind("<Escape>", lambda x: root.destroy())
    
          e.focus()
          root.wait_window()
    

    You would follow that with your regular code. ie. print(sys.argv)

    Note that with IDLE in Python 3.7.4+, when using the Run... Customized command, it is NOT necessary to import sys to access argv.

    If used in python 2.6/2.7 then be sure to capitalize: import Tkinter as tk

    For this example I've tried to strike a happy balance between features & brevity. Feel free to add or take away features, as needed!

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

    import sys

    sys.argv = [sys.argv[0], '-arg1', 'val1', '-arg2', 'val2']

    //If you're passing command line for 'help' or 'verbose' you can say as:

    sys.argv = [sys.argv[0], '-h']

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

    Answer from veganaiZe produces a KeyError outside IDLE with python 3.6.3. This can be solved by replacing if sys.modules['idlelib']: by if 'idlelib' in sys.modules: as below.

    import argparse
    # Check if we are using IDLE
    if 'idlelib' in sys.modules:
        # IDLE is present ==> we are in test mode
        print("""====== TEST MODE =======""")
        args = parser.parse_args([list of args])
    else:
        # It's command line, this is production mode.
        args = parser.parse_args()
    
    0 讨论(0)
  • 2020-12-02 13:53

    Based on the post by danben, here is my solution that works in IDLE:

    try:  
        sys.argv = ['fibo3_5.py', '30']  
        fibonacci(int(sys.argv[1]))  
    except:  
        print(str('Then try some other way.'))  
    
    0 讨论(0)
  • 2020-12-02 13:58

    Auto-detect IDLE and Prompt for Command-line Arguments

    #! /usr/bin/env python3
    
    import sys
    
    # Prompt user for (optional) command line arguments, when run from IDLE:
    if 'idlelib' in sys.modules: sys.argv.extend(input("Args: ").split())
    


    Change "input" to "raw_input" for Python2.

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