-1

If I have a certain function in my shell script, can I call that function from an external python script?

HackCode
  • 1,837
  • 6
  • 35
  • 66
  • Please make your question clearer. What is "my shell script" and how do you run it? How do you run the "external Python script" (external to what?) It's not at all clear what you want to do; it would help a lot to include some sample code. – rici Mar 23 '15 at 15:49

1 Answers1

1

Here is some reference material as I not exactly sure what you are trying to do:

Running Shell Commands

You can use the Python subprocess module:

from subprocess import call
call(["ls", "-l"])

Or if you are trying to run a specific function from inside your shell script it would be wiser to recode that function in Python. A similar SO question can be found here:

How to call a shell script function/variable from python?

Community
  • 1
  • 1
logic
  • 1,739
  • 3
  • 16
  • 22
  • 1
    How does this address the question of invoking shell functions? – rici Mar 23 '15 at 15:49
  • @rici There literally is no way you can execute a specific shell script function from Python. You'd have to implement the function in Python itself and then use the subprocess module to execute it. – logic Mar 23 '15 at 15:54
  • That's not actually quite true. If the shell is bash and the function is exported to the environment, you can run it by executing `bash -c the_function`. But anyway, your comment at least addresses the OP, and I suggest you include it in your answer. – rici Mar 23 '15 at 15:56