Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python Embedding
I don't know if this is the correct forum but,

I am doing some Python Embedding from:

I cannot figure out how to pass the keyword rosen from the C/Python API in the nelderMead function argument correctly.

I have tried hard-coding PyUnicode_DecodeFSDefault but it outputs a string into the Python when rosen itself is a type class
from scipy.

from scipy.optimize import minimize, rosen, rosen_der

def nelderMead(func):
    x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
    res = minimize(func, x0, method='Nelder-Mead', tol=1e-6)

please advise Huh .
jibarra Wrote:I cannot figure out how to pass the keyword rosen from the C/Python API in the nelderMead function argument correctly.
I don't understand what you want to do. Do you want to call function nelderMead? with which argument? from C, from python? Why can't you do it? Can you post your code?
I want to call the nelderMead Python function from C, and pass in the keyword rosen that I've imported from from scipy.optimize import minimize, rosen, rosen_der from C into the nelderMead function. The problem is whenever is pass in a string or PyObject, python reads it as a type string but rosen is a type class. So the rosen parameter does not get invoked as a class but as a string.

C Code below:


int main(){

PyObject *pName,*pFunc, *pValue, *pModule, *pOpt;


//Access file in local folder where C file is stored
PyRun_SimpleString("import sys");

//Convert String to PyObject
pName = PyUnicode_DecodeFSDefault("Rosenbrock_Function");
pModule = PyImport_Import(pName);


pFunc = PyObject_GetAttrString(pModule,"nelderMead");
pOpt = PyUnicode_DecodeFSDefault("rosen");

pValue = PyObject_CallFunctionObjArgs(pFunc,pOpt,NULL);

return 0;
You need to retrieve the 'rosen' attribute from the module as well. Here is a code that works for me (in python 3.5)

int main(){
    PyObject *pFunc, *pValue, *pModule, *pOpt;

    PyRun_SimpleString("import sys");

    if(pModule = PyImport_ImportModule("Rosenbrock_Function")){
        if(pFunc = PyObject_GetAttrString(pModule,"nelderMead")){
            if(pOpt = PyObject_GetAttrString(pModule, "rosen")){
                pValue = PyObject_CallFunctionObjArgs(pFunc,pOpt,NULL);
    return 0;
Don't use PyUnicode_DecodeFSDefault unless you're dealing with filesystem paths.
Thank you so much!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Embedding return in a print statement Tapster 3 118 Oct-07-2019, 03:10 PM
Last Post: Tapster
  Calling Extended Embedding Python as shared library jibarra 0 222 Jul-19-2019, 05:22 PM
Last Post: jibarra
  More Python Embedding Trouble jibarra 3 317 Jul-11-2019, 09:25 PM
Last Post: Gribouillis
  Embedding Python into a simulator siggi 0 371 Apr-24-2019, 07:42 PM
Last Post: siggi
  Embedding, windows, and virtual environments mariofutire 0 664 Sep-14-2018, 10:30 AM
Last Post: mariofutire
  [CPP] Embedding Python and use GDB Razazel 4 1,659 Nov-12-2017, 05:59 PM
Last Post: Razazel

Forum Jump:

Users browsing this thread: 1 Guest(s)