How to create an application which embeds and runs Python code without local Python installation?


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.

Asked By: Robert



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.

Answered By: Pierre-Jean Coudert

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.

Answered By: Daniel Pryden

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.

Answered By: Laurent Parenteau

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 compile a Python source distribution via python sdist --format=zip which gave me a ZIP file I named

My further steps were:

char pycmd[1000]; // temporary buffer for forged Python script lines

// forge Python command to set the lookup path
// add the zipped Python distribution library to the search path as well
    "import sys; sys.path = ['%s/','%s']",

// ... and execute

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

Answered By: Robert

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..

Answered By: Jive Dadson

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

Basically, you need:

PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path = ['.','','','','']");

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

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

Hope this helps.

Answered By: liangfu

You can compile to one .exe file using pyinstaller

pip install pyinstaller

pyinstaller --onefile
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.