Calling Python from a C++ Program for Distribution

Calling python from a c++ program for distribution

Boost has a python interface library which could help you.

Boost.Python

How do I run a Python script from C#?

The reason it isn't working is because you have UseShellExecute = false.

If you don't use the shell, you will have to supply the complete path to the python executable as FileName, and build the Arguments string to supply both your script and the file you want to read.

Also note, that you can't RedirectStandardOutput unless UseShellExecute = false.

I'm not quite sure how the argument string should be formatted for python, but you will need something like this:

private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "my/full/path/to/python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using(Process process = Process.Start(start))
{
using(StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}

Call python function in C code

Reading the Python manual,

#include <Python.h>

int main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("import addition\naddition.add(42, 42)\n");
Py_Finalize();
return 0;
}

might do what you want.

How to distribute a Python program with external libraries

For convenient, you could try something like pyinstaller.
It will package all of needed module into one folder or or one executable as you like. And it can run in all platforms.

The simple command to make a directory contains an executable file and all needed library is

$pyinstaller --onedir --name=directory_name --distpath="path_to_put_that_directory" "path to your main_program.py"

You can change --onedir into --onefile to make that folder into an one executable file which has all the thing it need to run inside.

How can I distribute python programs?

The normal way of distributing Python applications is with distutils. It's made both for distributing library type python modules, and python applications, although I don't know how it works on Windows. You would on Windows have to install Python separately if you use distutils, in any case.

I'd probably recommend that you distribute it with disutils for Linux, and Py2exe or something similar for Windows. For OS X I don't know. If it's an end user application you would probably want an disk image type of thing, I don't know how to do that. But read this post for more information on the user experience of it. For an application made for programmers you are probably OK with a distutils type install on OS X too.

Execute and wait for results C program and Python script

Use subprocess module.

import os
import signal
import subprocess
import sys

params = [...]
for param for params:
proc = subprocess.Popen(['/path/to/CProg', param.., param..])
subprocess.call([sys.executable, 'B.py', param.., param...])
os.kill(proc.pid, signal.SIGINT)
proc.wait()

Calling C/C++ from Python?

You should have a look at Boost.Python. Here is the short introduction taken from their website:

The Boost Python Library is a framework for interfacing Python and
C++. It allows you to quickly and seamlessly expose C++ classes
functions and objects to Python, and vice-versa, using no special
tools -- just your C++ compiler. It is designed to wrap C++ interfaces
non-intrusively, so that you should not have to change the C++ code at
all in order to wrap it, making Boost.Python ideal for exposing
3rd-party libraries to Python. The library's use of advanced
metaprogramming techniques simplifies its syntax for users, so that
wrapping code takes on the look of a kind of declarative interface
definition language (IDL).



Related Topics



Leave a reply



Submit