I've created a library in Python. I can use it on Linux or Windows. I would like to be able to make calls into it from most any non-Python script / program as well. Ideally, I'd like a cross platform option if possible, but Linux is the most important for now. Is there anyway to achieve that other than via a command prompt interface and making system calls / ShellExec style uses of the script? If this were all Windows based programming, for instance, I could create a dll... It would be awesome though if I could directly use the library somehow from say php in Linux, and C++ in Windows, etc. Any ideas?
Asked
Active
Viewed 519 times
2 Answers
1
Since you said you don't want to use the command-line interface, the logical answer for cross-platform access to a service is to use a web API. And, by doing it that way, the consumer of the API can be running on a different OS than the provider.
I've set up APIs before following this blog post. There's a more complete list of your python options here.

John Hazen
- 1,296
- 1
- 8
- 19
-
I think this is an excellent solution in general, and upvoted you for it. It avoids a command-line interface, avoids sub processes, and is cross platform! I didn't include the details of my immediate use case, but basically this specific library I'm developing involves a chain of private server-to-server secure https posts (and other web protocols). I want a client end which provides a library service that insulates it from posts, ssl certs, http codes etc. So, this solution will be kind of redundant and silly for that purpose. (Not your fault I didn't include all those details up front!) – BuvinJ Mar 19 '16 at 20:02
-
Hmm, I thought of https://docs.python.org/2/extending/embedding.html but you said "make calls into it from most any ... script/program". Embedded python only works if you're writing the client as well. The other option would be to have a directory that you watch, and have the client put requests in it, and then look for the response file. For unix only, there are named pipes. – John Hazen Mar 19 '16 at 20:31
-
Another good suggestion. I considered that if I only wanted to achieve this in Windows, I could probably embed Python in a C++ dll. I believe a dll can be made compatible for all the other MS technologies, e.g. C#, VB, etc. That doesn't help on Linux though, unless there is a Linux equivalent of a dll? – BuvinJ Mar 19 '16 at 22:17
-
Using the file system as a middleman would be another option - or a database. I've used virtually of these methods you and @armatita have suggested at one time or another. None is as "clean" as using a library / dll directly of course. – BuvinJ Mar 19 '16 at 22:21
0
Making calls is a broad description and the solution depends on if you want to give and receive output from those calls. If you do than you can follow a pretty generic solution between PHP and Python using JSON. For php: NOTE: Not my code, see original here.
// This is the data you want to pass to Python
$data = array('as', 'df', 'gh');
// Execute the python script with the JSON data
$result = shell_exec('python /path/to/myScript.py ' . escapeshellarg(json_encode($data)));
// Decode the result
$resultData = json_decode($result, true);
// This will contain: array('status' => 'Yes!')
var_dump($resultData);
And for Python:
import sys, json
# Load the data that PHP sent us
try:
data = json.loads(sys.argv[1])
except:
print "ERROR"
sys.exit(1)
# Generate some data to send to PHP
result = {'status': 'Yes!'}
# Send it to stdout (to PHP)
print json.dumps(result)
Obviously if you don't need this using system or popen in PHP might be enough. Also check:
-
Thanks. I do want to pass data in both directions. The trouble with your answer is that it does in fact use system calls / shellex, which is the immediate solution which came to my mind, but is the one I was looking for an alternate to. Of interest, though, one of your links contained a link to http://www.csh.rit.edu/~jon/projects/pip/ which is in interesting possibility for python/php at least (although not a "universal" solution). – BuvinJ Mar 19 '16 at 19:47
-
It has to happen at some point unless you have some way of converting Python syntax into php or as in the link you've posted somehow make the extension open the Python interpreter (which, I'm guessing, will be new type of nightmare once you'll want to start loading libraries into your python libs). In any case the link seems to be update and the project active. Why don't you just contact the guy whose making it and ask if it would work for what you are trying to do? – armatita Mar 19 '16 at 20:52
-
I may do that. Thanks. And, yes I do want to use other Python libs. That's kind of at the heart of this. Python lets you perform so many powerful tasks with so little code, I want to "spread" that power out to other client languages - ideally via a simple universal interface. – BuvinJ Mar 19 '16 at 22:28
-
Than I think (but ask the guy) that tool is a worse solution than just using external processes. Remember that you can make a clean api for calling functions in Python using console arguments (hide the subprocess stuff). If it's well done the user won't ever know it's being called as an external process. – armatita Mar 19 '16 at 22:34