Search code examples
python-3.xcommand-linecommand-line-argumentsinterpretersys

Using sys.argv in Python 3 with Python interpreter


I’m trying to figure out how to use sys.argv in Python 3.6, but can’t figure out how to make it work using the Python interpreter (I’m not even 100% sure I’m actually using the interpreter, a bit confused around the terminology with interpreter, shell, terminal etc.)

Question 1: to access the Python interpreter, can I simply type $ python into the Terminal (I’m on a Mac)? If not, how do I access it?

Seems that when I go to find what I believe to be the interpreter in my files (I’ve downloaded Python via Anaconda), I find a program called “pythonw”, and starting this launches the Terminal, with what looks to be the Python interpreter already running. Is this the interpreter? The code chunk below is what is printed in a Terminal window when I run the "pythonw" program:

Last login: Tue Aug  7 18:26:37 on ttys001
Users-MacBook-Air:~ Username$ /anaconda3/bin/pythonw ; exit;
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:14:23) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Question 2: Let’s assume that I have the Python interpreter running for the sake of argument. Assume also that I have the following script/module saved as test.py.

import sys 
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))

If I simply import this module in the command line of the interpreter, I get the printout:

Number of arguments: 1 arguments. Argument List: ['']

But how do I actually supply the module with arguments in the command line?

I've been looking around on internet, all of them showing this way of doing it, but it does not work.

Question 3: can the sys.argv only be used when arguments are written in the command line of the interpreter, or is there a way to supply a module with arguments in Spyder for instance?

Thank you for taking the time to read through it all, would make me so happy if I could get an answer to this! Been struggling for days now without being able to grasp it.


Solution

  • The Python interpreter is just a piece of code which translates and runs Python code. You can interact with it in different ways. The most straightforward is probably to put some Python code in a file, and pass that as the first argument to python:

    bash$ cat <<\: >./myscript.py
    from sys import argv
    print(len(argv), argv[1:])
    :
    bash$ # in real life you would use an editor instead to create this file
    bash$ python ./myscript.py one two three
    4 ['one', 'two', 'three']
    

    If you don't want to put the script in a file, perhaps because you just need to check something quickly, you can also pass Python a -c command-line option where the option argument is a string containing your Python code, and any non-option arguments are exposed to that code in sys.argv as before:

    bash$ python -c 'from sys import argv; print(len(argv), argv[1:])' more like this
    4 ['more', 'like', 'this']
    

    (Single quotes probably make the most sense with Bash. Some other shells use other conventions to wrap a piece of longer text as a single string; in particular, Windows works differently.)

    In both of these cases, the Python interpreter was started with a program to execute; it interpreted and executed that Python program, and then it quit. If you want to talk to Python more directly in an interactive Read-Eval-Print-Loop (which is commonly abbreviated REPL) that's what happens when you type just python:

    bash$ python
    Python 3.5.1 (default, Dec 26 2015, 18:08:53)
    [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 1+2
    3
    >>> 
    

    As you can see, anything you type at the >>> prompt gets read, evaluated, and printed, and Python loops back to the >>> to show that it's ready to do it again. (If you type something incomplete, the prompt changes to .... It will sometimes take a bit of puzzling to figure out what's missing - it could be indentation or a closing parenthesis to go with an opening parenthesis you typed on a previous line, for example.)

    There is nothing per se which prevents you from assigning a value to sys.argv yourself:

    >>> import sys
    >>> sys.argv = ['ick', 'poo', 'ew']
    

    At this point, you can import the script file you created above, and it will display the arguments after the first;

    >>> import myscript
    3, ['poo', 'ew']
    

    You'll notice that the code ignores the first element of sys.argv which usually contains the name of the script itself (or -c if you used python -c '...').

    ... but the common way to talk to a module you import is to find its main function and call it with explicit parameters. So if you have a script otherscript.py and inspect its contents, it probably contains something like the following somewhere near the end:

    def main():
        import sys
        return internal_something(*sys.argv[1:])
    
    if __name__ == '__main__':
        main()
    

    and so you would probably instead simply

    >>> import otherscript
    >>> otherscript.internal_something('ick', 'poo')
    

    Your very first script doesn't need to have this structure, but it's a common enough arrangement that you should get used to seeing it; and in fact, one of the reasons we do this is so that we can import code without having it start running immediately. The if __name__ == '__main__' condition specifically evaluates to False when you import the file which contains this code, so you can control how it behaves under import vs when you python myscript.py directly.

    Returning to your question, let's still examine how to do this from a typical IDE.

    An IDE usually shields you from these things, and simply allows you to edit a file and show what happens when the IDE runs the Python interpreter on the code in the file. Of course, behind the scenes, the IDE does something quite similar to python filename.py when you hit the Execute button (or however it presents this; a function key or menu item perhaps).

    A way to simulate what we did above is to edit two files in the IDE. Given myscript.py from above, the second file could be called something like iderun.py and contain the same code we submitted to the REPL above.

    import sys
    sys.argv = ['easter egg!', 'ick', 'poo', 'ew']
    import myscript