11

Hello fellow software developers.

I want to distribute a C program which is scriptable by embedding the Python interpreter.
The C program uses Py_Initialize, PyImport_Import and so on to accomplish Python embedding.

I'm looking for a solution where I distribute only the following components:

  • my program executable and its libraries
  • the Python library (dll/so)
  • a ZIP-file containing all necessary Python modules and libraries.

How can I accomplish this? Is there a step-by-step recipe for that?

The solution should be suitable for both Windows and Linux.

Thanks in advance.

Rex Logan
  • 26,248
  • 10
  • 35
  • 48
Robert
  • 878
  • 6
  • 14
  • I deleted my answer since it wasn't applicable. You might want to edit the question so it's clear that you're embedding Python in a C application. – FogleBird Mar 22 '10 at 18:40
  • 2
    Possibly related: http://stackoverflow.com/questions/1387906/c-with-python-embedding-crash-if-python-not-installed – Daniel Pryden Mar 22 '10 at 19:19

7 Answers7

6

Have you looked at Python's official documentation : Embedding Python into another application?

There's also this really nice PDF by IBM : Embed Python scripting in C application.

You should be able to do what you want using those two resources.

Laurent Parenteau
  • 2,516
  • 20
  • 31
6

I simply tested my executable on a computer which hasn't Python installed and it worked.

When you link Python to your executable (no matter if dynamically or statically) your executable already gains basic Python language functionality (operators, methods, basic structures like string, list, tuple, dict, etc.) WITHOUT any other dependancy.

Then I let Python's setup.py compile a Python source distribution via python setup.py sdist --format=zip which gave me a ZIP file I named pylib-2.6.4.zip.

My further steps were:

char pycmd[1000]; // temporary buffer for forged Python script lines
...
Py_NoSiteFlag=1;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(directoryWhereMyOwnPythonScriptsReside);
Py_InitializeEx(0);

// forge Python command to set the lookup path
// add the zipped Python distribution library to the search path as well
snprintf(
    pycmd,
    sizeof(pycmd),
    "import sys; sys.path = ['%s/pylib-2.6.4.zip','%s']",
    applicationDirectory,
    directoryWhereMyOwnPythonScriptsReside
);

// ... and execute
PyRun_SimpleString(pycmd);

// now all succeeding Python import calls should be able to
// find the other modules, especially those in the zipped library

...
Robert
  • 878
  • 6
  • 14
1

I think here is the answer you want Unable to get python embedded to work with zip'd library

Basically, you need:

Py_NoSiteFlag=1;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(".");
Py_InitializeEx(0);
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path = ['.','python27.zip','python27.zip/DLLs','python27.zip/Lib','python27.zip/site-packages']");

in your c/c++ code for loading the python standard library.

And in your python27.zip, all .py source code are located at python27.zip/Lib as described in the sys.path variable.

Hope this helps.

Community
  • 1
  • 1
liangfu
  • 117
  • 3
1

Did you take a look at Portable Python ? No need to install anything. Just copy the included files to use the interpreter.

Edit : This is a Windows only solution.

Pierre-Jean Coudert
  • 9,109
  • 10
  • 50
  • 59
  • Portable Python was also a find when I did my research. But it's not what I'm looking for. There should not be any other dependancy. – Robert Mar 22 '10 at 18:48
1

Have you looked at Embedding Python in Another Application in the Python documentation?

Once you have that, you can use an import hook (see PEP 302) to have your embedded Python code load modules from whatever place you choose. If you have everything in one zipfile, though, you probably just need to make it the only entry on sys.path.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
0

There's a program called py2exe. I don't know if it's only available for Windows. Also, the latest version that I used does not wrap everything up into one .exe file. It creates a bunch of stuff that has to be distributed - a zip file, etc..

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
  • The title seems to be misleading - Python is embedded. I don't want to have the Python code embedded in the executable file - the executable shall become scriptable by being linked to Python. py2exe is therefore not the solution. – Robert Mar 29 '10 at 19:26
0

You can compile to one .exe file using pyinstaller

pip install pyinstaller

pyinstaller --onefile urPythonScriptName.py