2

I'm interested in writing a python binding or wrapper for an existing command line utility that I use on Linux, so that I can access its features in my python programs. Is there a standard approach to doing this that someone could point me to?

At the moment, I have wrapped the command line executable in a subprocess.Popen call, which works but feels quite brittle, and I'd like to make the integration between the two sides much more stable so that it works in places other than my own computer!

1
  • There shouldn't be any problem with that method working on other computers, assuming that the utility is installed in the same place across computers. If not, then that's a problem that's out of the scope of Python bindings. Commented Nov 4, 2010 at 20:39

2 Answers 2

5

If you must use a command line interface, then subprocess.Popen is your best bet. Remember that you can use shell=True to let it pick the path variables, you can use os.path.join to use OS-dependent path separators etc.

If, however, your command line utility has shared libraries, look at ctypes, which allows you to connect directly to those libraries and expose functionality directly.

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

Comments

1

One way would be to

  1. re-factor your command line utility so that command line handling is separated and the actual functionality is exposed as shared archive.

  2. Then you could expose those function using cython.

  3. Write your complete command line utility in python that exploits those functions.

This makes distribution hard though.

What you are doing is still the best way.

3 Comments

You should first check that someone else hasn't already done some of those steps for you - particularly step 1
Unfortunately the command line utility isn't mine, it's just one that I use a lot, and would like to use in my applications. I'd like to contribute a binding for Python if possible, but just not sure how to go about doing that in general.
@Richard J: Bindings are better created using the library approach. cython, ctypes etc. But you do not have access then you will have to rely on the approach used for external command execution - subprocess.Popen.

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.