Sandip
Sandip

Reputation: 3761

How to call .pyc function from C or C++?

I have .pyc files I know functions inside it.

I need to call those function from C/ C++ I tried using PyImport_Import but it returns NULL.

pName = PyString_FromString("test");
pModule = PyImport_Import(pName );

if I call pModule = PyImport_ImportModule("py_ffile"); then it's working fine for .py files. But I don't want to call function from .py. I want to call it from .pyc file.

Upvotes: 2

Views: 2988

Answers (3)

Sacha
Sacha

Reputation: 164

The path where your py file is located could be unknown. Trying to add your path by using sys like this, if you are using Boost Python:

PyRun_SimpleString("import sys"); 
PyRun_SimpleString("sys.path.append(\"your path here\")");

or use this one:

PySys_SetPath("your path here");

And like this, if you are using directly the python dll:

SetSysPath("your path here");

and then try again with PyImport_Import

Upvotes: 1

Baltasarq
Baltasarq

Reputation: 12212

This is not exactly what you are asking for, but you could consider using Cython. It will compile your code to C, and you'll have no problem to use it from a C++ program.

Upvotes: 1

Avinash
Avinash

Reputation: 13257

Boost::Python a C++ library which enables seamless interoperability between C++ and the Python programming language may help you.

Upvotes: 1

Related Questions