Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a python file named and I can successfully execute it through the terminal using the following commands:

chmod +x

This works just fine if I have simple print phrases and so on.

What should I do if I have defined a function in and I want to call it with parameters that I want directly from terminal? How do I pass the arguments to the function call?

share|improve this question

You can also use the sys module. Here is an example :

import sys

first_arg = sys.argv[1]
second_arg = sys.argv[2]

def greetings(word1=first_arg, word2=second_arg):
    print("{} {}".format(word1, word2))

if __name__ == "__main__":
    greetings("Bonjour", "monde")

It has the behavior your are looking for :

$ python Hello world
Hello world
Bonjour monde
share|improve this answer

Python provides more than one way to parse arguments. The best choice is using the argparse module, which has many features you can use.

So you have to parse arguments in your code and try to catch and fetch the arguments inside your code.

You can't just pass arguments through terminal without parsing them from your code.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.