Python’ı Başka Bir Uygulamaya Gömme

Önceki bölümlerde Python’un nasıl genişletileceği, yani Python’a bir C fonksiyonları kitaplığı ekleyerek Python’un işlevselliğinin nasıl genişletileceği tartışıldı. Bunu ayrıca tam tersi şekilde de yapmak mümkündür: Python’u içine gömerek C/C++ uygulamanızı zenginleştirin. Gömme işlemi, uygulamanıza bazı işlevlerini C veya C++ yerine Python’da uygulama yeteneği sağlar. Bu birçok amaç için kullanılabilir; bir örnek olarak kullanıcıların Python’da bazı komut dosyaları yazarak uygulamayı ihtiyaçlarına göre uyarlamalarına izin vermek olabilir. Bazı işlevler Python’da daha kolay yazılabilecekse kendiniz de kullanabilirsiniz.

Python’u gömmek, onu genişletmeye benzer, ancak tam olarak değil. Aralarındaki farksa Python’u genişlettiğinizde, uygulamanın ana programının hala Python yorumlayıcısı olması; Python’u gömerseniz, ana programın Python ile hiçbir ilgisi olmayabilmesidir — bunun yerine, uygulamanın bazı bölümleri bazı Python kodlarını çalıştırmak için zaman zaman Python yorumlayıcısını çağırır.

Yani Python’u gömüyorsanız, kendi ana programınızı sağlıyorsunuz demektir. Bu ana programın yapması gereken şeylerden biri Python yorumlayıcısını başlatmaktır. En azından Py_Initialize() fonksiyonunu çağırmalısınız. Python’a komut satırı argümanlarını iletmek için opsiyonel çağrılar vardır. Daha sonra uygulamanın herhangi bir yerinden yorumlayıcıyı çağırabilirsiniz.

Yorumlayıcıyı çağırmanın birkaç farklı yolu vardır: Python deyimlerini içeren bir dizeyi PyRun_SimpleString() öğesine veya bir stdio dosya işaretçisini ve bir dosya adını (yalnızca hata mesajlarında tanımlama için) PyRun_SimpleFile() ‘a iletebilirsiniz. Python nesnelerini oluşturmak ve kullanmak için önceki bölümlerde açıklanan alt düzey işlemleri de çağırabilirsiniz.

Ayrıca bakınız

Python/C API reference manual

Python’un C arayüzünün detayları bu kılavuzda verilmiştir. Çok sayıda gerekli bilgi burada bulunabilir.

Çok Üst Düzey Gömme

Python’u gömmenin en basit şekli, çok yüksek seviyeli arayüzün kullanılmasıdır. Bu arabirim, uygulamayla doğrudan etkileşime girmeye gerek kalmadan bir Python betiği yürütmeyi amaçlamaktadır. Bu örnek olarak bir dosya üzerinde bazı işlemler gerçekleştirmek için kullanılabilir.

#define PY_SSIZE_T_CLEAN
#include <Python.h>

int
main(int argc, char *argv[])
{
    PyStatus status;
    PyConfig config;
    PyConfig_InitPythonConfig(&config);

    /* optional but recommended */
    status = PyConfig_SetBytesString(&config, &config.program_name, argv[0]);
    if (PyStatus_Exception(status)) {
        goto exception;
    }

    status = Py_InitializeFromConfig(&config);
    if (PyStatus_Exception(status)) {
        goto exception;
    }
    PyConfig_Clear(&config);

    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    return 0;

  exception:
     PyConfig_Clear(&config);
     Py_ExitStatusException(status);
}

Not

#define PY_SSIZE_T_CLEAN was used to indicate that Py_ssize_t should be used in some APIs instead of int. It is not necessary since Python 3.13, but we keep it here for backward compatibility. See Strings and buffers for a description of this macro.

Setting PyConfig.program_name should be called before Py_InitializeFromConfig() to inform the interpreter about paths to Python run-time libraries. Next, the Python interpreter is initialized with Py_InitializeFromConfig(), followed by the execution of a hard-coded Python script that prints the date and time. Afterwards, the Py_FinalizeEx() call shuts the interpreter down, followed by the end of the program. In a real program, you may want to get the Python script from another source, perhaps a text-editor routine, a file, or a database. Getting the Python code from a file can better be done by using the PyRun_SimpleFile() function, which saves you the trouble of allocating memory space and loading the file contents.

Çok Yüksek Düzeyde Gömmenin Ötesinde: Genel Bir Bakış

Yüksek seviyeli arayüz size uygulamanızdan rastgele Python kodu parçalarını yürütme yeteneği verir, ancak veri değerleri alışverişi en hafif tabirle oldukça zahmetlidir. Bunu istiyorsanız, daha düşük seviyeli aramalar kullanmalısınız. Daha fazla C kodu yazmak zorunda kalma pahasına neredeyse her şeyi başarabilirsiniz.

Farklı amaçlara rağmen Python’u genişletmek ve Python’u gömmek tamamen aynı aktivitedir. Önceki bölümlerde tartışılan konuların çoğu hala geçerlidir. Bunu göstermek için Python’dan C’ye uzantı kodunun gerçekten ne yaptığını düşünün:

  1. Veri değerlerini Python’dan C’ye çevirin,

  2. Çevrilen değerleri kullanarak bir C rutinine bir fonksiyon çağrısı yapın, ve

  3. Çağrıdaki veri değerlerini C’den Python’a çevirin.

Python’u yerleştirirken, arayüz kodu şunları yapar:

  1. Veri değerlerini C’den Python’a çevirin,

  2. Çevrilen değerleri kullanarak bir Python arabirim rutinine bir fonksiyon çağrısı gerçekleştirin ve

  3. Çağrıdaki veri değerlerini C’den Python’a dönüştürün.

Gördüğünüz gibi, veri dönüştürme adımları, diller arası aktarımın farklı yönüne uyum sağlamak için basitçe değiştirilir. Tek fark, her iki veri dönüşümü arasında çağırdığınız rutindir. Uzatırken C rutini çağırırsınız, gömerken Python rutini çağırırsınız.

Bu bölüm, verilerin Python’dan C’ye nasıl dönüştürüleceğini ve bunun tam tersini tartışmayacaktır. Ayrıca, referansların doğru kullanımı ve hataların ele alınmasının anlaşıldığı varsayılmaktadır. Bu hususlar, yorumlayıcının genişletilmesinden farklı olmadığı için, gerekli bilgiler için önceki bölümlere başvurabilirsiniz.

Saf Gömme

İlk program, bir Python betiğinde bir fonksiyonu çalıştırmayı amaçlar. Çok yüksek seviyeli arayüzle ilgili bölümde olduğu gibi, Python yorumlayıcısı uygulama ile doğrudan etkileşime girmez (ancak bu bir sonraki bölümde değişecektir).

Python betiğinde tanımlanan bir işlevi çalıştırma kodu:

#define PY_SSIZE_T_CLEAN
#include <Python.h>

int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyUnicode_DecodeFSDefault(argv[1]);
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyLong_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return 1;
                }
                /* pValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
            }
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            if (pValue != NULL) {
                printf("Result of call: %ld\n", PyLong_AsLong(pValue));
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
                fprintf(stderr,"Call failed\n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    if (Py_FinalizeEx() < 0) {
        return 120;
    }
    return 0;
}

Bu kod, argv[1] kullanarak bir Python betiği yükler ve argv[2] içinde adlandırılan fonksiyonu çağırır. Tamsayı argümanları, argv dizisinin diğer değerleridir. Bu programı derler, ve bağlarsanız (bitmiş yürütülebilir dosyayı call olarak adlandıralım) ve onu aşağıdaki gibi bir Python betiğini çalıştırmak için kullanırsanız:

def multiply(a,b):
    print("Will compute", a, "times", b)
    c = 0
    for i in range(0, a):
        c = c + b
    return c

o zaman sonuç olmalıdır:

$ call multiply multiply 3 2
Will compute 3 times 2
Result of call: 6

Program işlevselliği açısından oldukça büyük olmasına rağmen, kodun çoğu Python ve C arasında veri dönüştürme ve hata raporlama içindir. Python’u gömmekle ilgili ilginç kısım şununla başlar

Py_Initialize();
pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);

After initializing the interpreter, the script is loaded using PyImport_Import(). This routine needs a Python string as its argument, which is constructed using the PyUnicode_DecodeFSDefault() data conversion routine.

pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */

if (pFunc && PyCallable_Check(pFunc)) {
    ...
}
Py_XDECREF(pFunc);

Komut dosyası yüklendikten sonra, aradığımız ad PyObject_GetAttrString() kullanılarak alınır. Ad varsa ve döndürülen nesne çağrılabilirse bunun bir fonksiyon olduğunu güvenle varsayabilirsiniz. Program daha sonra normal olarak bir dizi argüman oluşturarak devam eder. Python işlevine yapılan çağrı şu şekilde yapılır:

pValue = PyObject_CallObject(pFunc, pArgs);

Fonksiyon döndürüldüğünde, pValue ya NULL olur ya da fonksiyonun dönüş değerine bir başvuru içerir. Değeri inceledikten sonra referans bıraktığınızdan emin olun.

Gömülü Python’u Genişletme

Şimdiye kadar, gömülü Python yorumlayıcısının uygulamanın kendisinden işlevselliğe erişimi yoktu. Python API, gömülü yorumlayıcıyı genişleterek buna izin verir. Yani, gömülü yorumlayıcı, uygulama tarafından sağlanan rutinlerle genişletilir. Kulağa karmaşık gelse de, o kadar da kötü değil. Uygulamanın Python yorumlayıcısını başlattığını bir süreliğine unutun. Bunun yerine, uygulamayı bir dizi altyordam olarak düşünün ve tıpkı normal bir Python uzantısı yazacağınız gibi, Python’un bu rutinlere erişmesini sağlayan bir tutkal kodu yazın. Örneğin:

static int numargs=0;

/* Return the number of arguments of the application command line */
static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
    if(!PyArg_ParseTuple(args, ":numargs"))
        return NULL;
    return PyLong_FromLong(numargs);
}

static PyMethodDef emb_module_methods[] = {
    {"numargs", emb_numargs, METH_VARARGS,
     "Return the number of arguments received by the process."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef emb_module = {
    .m_base = PyModuleDef_HEAD_INIT,
    .m_name = "emb",
    .m_size = 0,
    .m_methods = emb_module_methods,
};

static PyObject*
PyInit_emb(void)
{
    return PyModuleDef_Init(&emb_module);
}

Yukarıdaki kodu main() fonksiyonunun hemen üstüne ekleyin. Ayrıca, Py_Initialize(): çağrısından önce aşağıdaki iki ifadeyi ekleyin:

numargs = argc;
PyImport_AppendInittab("emb", &PyInit_emb);

These two lines initialize the numargs variable, and make the emb.numargs() function accessible to the embedded Python interpreter. With these extensions, the Python script can do things like

import emb
print("Number of arguments", emb.numargs())

Gerçek bir uygulamada, yöntemler uygulamanın API’sini Python’a gösterir.

Python’u C++’a Gömmek

Python’u bir C++ programına yerleştirmek de mümkündür; bunun tam olarak nasıl yapılacağı, kullanılan C++ sisteminin ayrıntılarına bağlı olacaktır; genel olarak ana programı C++ ile yazmanız ve programınızı derlemek ve bağlamak için C++ derleyicisini kullanmanız gerekecektir. Python’un kendisini C++ kullanarak yeniden derlemeye gerek yoktur.

Unix benzeri sistemler altında Derleme ve Bağlama

Python yorumlayıcısını uygulamanıza gömmek için derleyicinize (ve bağlayıcınıza) iletilecek doğru bayrakları bulmak mutlaka önemli değildir, özellikle Python’un C dinamik uzantıları buna karşı bağlatılı olan (.so dosyaları) olarak uygulanan kütüphane modüllerini yüklemesi gerektiğinden.

Gerekli derleyici ve bağlayıcı bayraklarını bulmak için, yükleme işleminin bir parçası olarak oluşturulan pythonX.Y-config betiğini çalıştırabilirsiniz (bir python3-config betiği de mevcut olabilir). Bu komut dosyası, size doğrudan yardımcı olacak birkaç seçeneğe sahiptir:

  • pythonX.Y-config --cflags derleme sırasında size önerilen bayrakları verecektir:

    $ /opt/bin/python3.11-config --cflags
    -I/opt/include/python3.11 -I/opt/include/python3.11 -Wsign-compare  -DNDEBUG -g -fwrapv -O3 -Wall
    
  • pythonX.Y-config --ldflags --embed, bağlantı kurarken size önerilen bayrakları verecektir:

    $ /opt/bin/python3.11-config --ldflags --embed
    -L/opt/lib/python3.11/config-3.11-x86_64-linux-gnu -L/opt/lib -lpython3.11 -lpthread -ldl  -lutil -lm
    

Not

Birkaç Python kurulumu arasında (ve özellikle sistem Python ile kendi derlenmiş Python’unuz arasında) karışıklığı önlemek için, yukarıdaki örnekte olduğu gibi mutlak pythonX.Y-config yolunu kullanmanız önerilir.

Bu prosedür sizin için işe yaramazsa (tüm Unix benzeri platformlar için çalışması garanti edilmez; ancak, memnuniyetle karşılıyoruz: hata raporları) dinamik hakkında sisteminizin belgelerini okumanız gerekecektir ve/veya Python’un Makefile (konumunu bulmak için sysconfig.get_makefile_filename() kullanın) ve derleme seçeneklerini bağlamayı inceleyin. Bu durumda, sysconfig modülü, birleştirmek isteyeceğiniz konfigürasyon değerlerini programlı olarak çıkarmak için kullanışlı bir araçtır. Örneğin:

>>> import sysconfig
>>> sysconfig.get_config_var('LIBS')
'-lpthread -ldl  -lutil'
>>> sysconfig.get_config_var('LINKFORSHARED')
'-Xlinker -export-dynamic'

Linking for one or several Python versions on Linux

The flags obtained from pythonX.Y-config --ldflags --embed link the application against one minor version of Python. If that Python was built as a shared library, the executable records a dependency on the versioned library, such as libpython3.12.so.1.0, and expected that specific minor version at runtime. This is the recommended setup for most host (embedding) applications. Build the application for each Python version that it supports, or distribute it together with the Python that it was built with.

Using the Limited API does not change this. Defining Py_LIMITED_API restricts your source code to API that is compatible across minor versions, but the library dependency is recorded when linking, so the executable still requires libpython3.12.so.1.0.

A binary that needs to run with several minor Python versions must use only the Limited API and must also avoid the versioned dependency.

Uyarı

Host applications that use the Limited API must be aware of the following limitations:

  • It can only initialize Python with Py_Initialize() or Py_InitializeEx(), which set Python up much like the python command does. They honor PYTHONHOME and PYTHONPATH, add the user site-packages directory to sys.path, and terminate the process if initialization fails. They set sys.executable to the python3 program found on PATH, which can belong to a different Python installation than the library that was loaded. The functions that accept an isolated configuration, Py_InitializeFromConfig() and Py_InitializeFromInitConfig(), are not part of the Limited API.

  • The Python that gets loaded is only known at run-time. Checking that it is suitable is up to the application. An incompatible Python, such as a too-old or too-new version, is not rejected automatically.

  • The Stable ABI keeps functions available in later versions, but it does not guarantee identical behavior. The default configuration and the way sys.path is computed can change between Python versions, and between installations of the same version. Test the application with every Python version and installation that it is meant to support. See also Stable ABI Caveats.

A Python built with --enable-shared and without --with-pydebug installs libpython3.so next to the versioned library. When the dynamic loader loads this library, it also loads the versioned library that it was built with, and symbols looked up through libpython3.so are found in the versioned library. An application can therefore load libpython3.so with dlopen(3) and look up each function that it uses with dlsym(3).

This only works at run time. When linking an executable, the linker does not use the dependencies of a library to resolve symbols, and libpython3.so defines no Python symbols of its own, so linking with -lpython3 fails with undefined reference errors.

For example, with Python 3.12 as the oldest supported version:

#define Py_LIMITED_API 0x030c0000  /* Limited API 3.12 */
#include <Python.h>
#include <dlfcn.h>
#include <stdio.h>

/* The API this application uses, declared as in Python.h.
   Everything is reached through these pointers. */
static struct {
    const unsigned long *Version;
    const char *(*GetVersion)(void);
    void (*InitializeEx)(int);
    int (*FinalizeEx)(void);
} py;

#define LOAD(lib, pointer, name) \
    ((*(void **)&(pointer) = dlsym(lib, name)) != NULL)

int
main(int argc, char *argv[])
{
    /* For brevity, the path comes from the command line.  A real
       application reads it from its own configuration. */
    if (argc != 2 || argv[1][0] != '/') {
        fprintf(stderr, "usage: %s /absolute/path/to/libpython3.so\n",
                argv[0]);
        return 2;
    }
    /* RTLD_GLOBAL is required: it lets the extension modules that
       Python imports find Python's symbols. */
    void *lib = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
    if (lib == NULL) {
        fprintf(stderr, "Python is not available: %s\n", dlerror());
        return 1;
    }
    if (!LOAD(lib, py.Version, "Py_Version")
        || !LOAD(lib, py.GetVersion, "Py_GetVersion")
        || !LOAD(lib, py.InitializeEx, "Py_InitializeEx")
        || !LOAD(lib, py.FinalizeEx, "Py_FinalizeEx"))
    {
        fprintf(stderr, "Unsupported Python: %s\n", dlerror());
        return 1;
    }
    if (*py.Version < Py_LIMITED_API) {
        fprintf(stderr, "Python %s is older than 3.12\n", py.GetVersion());
        return 1;
    }

    py.InitializeEx(0);
    printf("Loaded Python %s\n", py.GetVersion());
    return py.FinalizeEx() < 0 ? 120 : 0;
}

Compile the program with Python’s headers, but without the output of --ldflags:

$ gcc $(/opt/bin/python3.12-config --includes) -o myapp myapp.c
$ ./myapp /usr/local/lib/libpython3.so
Loaded Python 3.14.10 (main, Nov  5 2026, 09:47:10) [GCC 14.2.0]

No Python library is passed to the linker, so the executable does not depend on one. You can check this with readelf:

$ readelf -d myapp | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

For the same reason, Python functions can only be called through such pointers. This includes the functions that macros in Python.h expand to: for example, call Py_DecRef() through a pointer instead of using Py_DECREF().

  • The path passed to dlopen() selects the code that runs in your process. It should come from a trusted source and name a file that only trusted users can replace.

  • Use an absolute path. The dynamic loader looks for a name without a slash in its usual search path, such as LD_LIBRARY_PATH and the system library directories. It looks for the versioned library that libpython3.so depends on in the same way, and not next to libpython3.so. To choose the exact file, pass the absolute path of the versioned library to dlopen() instead.

  • RTLD_GLOBAL is required. Extension modules are generally not linked against a Python library, so Python can only import them if its symbols are visible globally.

Unsupported workaround: renaming the dependency after linking

Some applications are instead linked against a versioned library as described above, and the dependency recorded in the executable is then renamed to libpython3.so, for example with patchelf.

Uyarı

Renaming the dependency this way is unsupported. Nothing checks that such an executable only uses the Stable ABI, and it can contain copies of Python data, such as PyLong_Type, whose size differs between Python versions.