[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Embedding Python in C

Am 19.07.19 um 16:26 schrieb Jesse Ibarra:
> On Friday, July 19, 2019 at 8:17:43 AM UTC-6, Chris Angelico wrote:
>> On Sat, Jul 20, 2019 at 12:16 AM Jesse Ibarra
>> <jesse.ibarra.1996 at gmail.com> wrote:
>>> On Thursday, July 18, 2019 at 2:01:39 PM UTC-6, Chris Angelico wrote:
>>>> On Fri, Jul 19, 2019 at 5:51 AM Christian Gollwitzer <auriocus at gmx.de> wrote:
>>>>> Once you can do this, you can proceed to call a Python function, which
>>>>> in C means that you invoke the function PyObject_CallObject(). A basic
>>>>> example is shown here:
>>>>> https://docs.python.org/2/extending/embedding.html#pure-embedding
>>>> Or, better:
>>>> https://docs.python.org/3/extending/embedding.html#pure-embedding
>>>> ChrisA
>>> I need to call a .so file, but I don't know I way to do that with PyObject. I can only seem to bring in .py files
>> I have vastly insufficient context to be able to even attempt to answer this.
>> ChrisA
> Thank you for your help.
> If this helps. I need PyImport_Import to bring in a C shared lib. (.so file)

So your question is which CPython function is used to load a compiled 
Python extension (.so file). I haven't done this, but it will also be 
not sufficient to import numpy or other packages. These larger packages 
typically have a layer of Python code which acts as a loader and upon 
import loads the corresponding .so files at runtime.

After browsing the C-API, I would try either:


or even

PyRun_SimpleString("import numpy")

If this does not work, then maybe you need to setup the correct 
PYTHONPATH to point to the places where these packages are installed?