0

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?

2
  • Read up on the argparse module, 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). Commented Feb 19, 2015 at 14:57
  • 1
    It's not clear if you're having trouble trying to pass arguments to the python interpreter, to your script, or having trouble accessing them inside a script Commented Feb 19, 2015 at 15:17

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Your example is not passing any command line arguments - it's just executing a program without any. You'd have to use 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
You are completely right. I had tested it only with IDLE. I already edited it. Thanks
1

You can access the command line arguments using sys.argv

import sys
print sys.argv

% python scrpt.py a b c d

outputs: ['scrpt.py', 'a', 'b', 'c', 'd']

Comments

1

Use - to mark the end of python's interpreter options:

python --interpreter-option-1 --interpreter-option-2 - --my-custom-option option-value my-custom-argument

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.