2

I'm using similar approach to call python function from my shell script:

python -c 'import foo; print foo.hello()'

But I don't know how in this case I can pass arguments to python script and also is it possible to call function with parameters from command line?

3 Answers 3

7
python -c 'import foo, sys; print foo.hello(); print(sys.argv[1])' "This is a test"

or

echo "Wham" | python -c 'print(raw_input(""));'

There's also argparse (py3 link) that could be used to capture arguments, such as the -c which also can be found at sys.argv[0].

A second library do exist but is discuraged, called getopt.getopt.

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

2 Comments

You shouldn't suggest getopt. That module is mainly for C programmers that are too lazy to learn anything new. In fact its documentation starts with: "Users who are unfamiliar with the C getopt() function or who would like to write less code and get better help and error messages should consider using the argparse module instead."
@Bakuriu You got a point. Switched the reccomendation but kept the reference towards getopt just to not overlook any options. But did discurage the user from using it.
6

You don't want to do that in shell script.

Try this. Create a file named "hello.py" and put the following code in the file (assuming you are on unix system):

#!/usr/bin/env python

print "Hello World"

and in your shell script, write something lke this

#!/bin/sh
python hello.py

and you should see Hello World in the terminal.

That's how you should invoke a script in shell/bash.

To the main question: how do you pass arguments?

Take this simple example:

#!/usr/bin/env python
import sys

def hello(name):
    print "Hello, " + name

if __name__ == "__main__":
    if len(sys.argv) > 1:
        hello(sys.argv[1])
    else:
        raise SystemExit("usage:  python hello.py <name>")

We expect the len of the argument to be at least two. Like shell programming, the first one (index 0) is always the file name.

Now modify the shell script to include the second argument (name) and see what happen.

haven't tested my code yet but conceptually that's how you should go about

edit:

If you just have a line or two simple python code, sure, -c works fine and is neat. But if you need more complex logic, please put the code into a module (.py file).

1 Comment

Instead of raising SystemExit it's probably more readable to just call sys.exit('the message').
2

You need to create one .py file. And after you call it this way :

python file.py argv1 argv2

And after in your file, you have sys.argv list, who give you list of argvs.

4 Comments

I don't see why this should be downvoted. It's much better than doing -c.
I don't know. I say the same thing as you ... But your answer is more detailed ...
Yeah took me a while to finish writing lol. Hence I upvoted you because I think the main issue is readability and usability :)
maybe just because it's not exactly what the OP wants

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.