I am attempting to start the python interpreter by typing python into my terminal. I also want to pass some command line arguments. If I try python arg1 it things I am trying to open and run a script called arg1. So my question is, how can I pass command line arguments to python in the command line interpreter mode?
4 Answers
You already are.
Your arg1 is a command line argument - to the python interpreter.
Maybe you're thinking of a command line option? (which really is just a specific type of command line argument)
Using the -h argument (python -h) will print all such options.
python -Q new, for example, will launch the interpreter with the new float division by default.
On the other hand, if you want to pass command line arguments to your script, you need to specify them after the script name: python my_script.py arg1
If you are on a UNIX based system, you can also use a shebang on your script, which will allow you to simply execute your script by name, skipping python:
#!/usr/bin/env python
#your script here
then run ./my_script.py arg1
Comments
An easy way to do it is by using the subprocess module and input variables.
An example would be:
from subprocess import call
x = raw_input('Which command line arguments? ')
call(x, shell=True)
Try it with an ls command (dir for Windows users), it should work.
2 Comments
call(x, shell=True) to actually accept arguments from raw_input. I don't think the OP is trying to run another process, but it's not entirely clear
argparsemodule, assuming you want to write your own program that takes command line arguments (it isn't clear that that is what you want to do).