Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Embedding
#3
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:
//https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html

#include</usr/include/python3.6m/Python.h>


int main(){

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

Py_Initialize();

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

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

Py_DECREF(pName);

if(pModule){
pFunc = PyObject_GetAttrString(pModule,"nelderMead");
Py_DECREF(pModule);
}
pOpt = PyUnicode_DecodeFSDefault("rosen");

pValue = PyObject_CallFunctionObjArgs(pFunc,pOpt,NULL);

Py_FinalizeEx();
return 0;
}
Reply


Messages In This Thread
Python Embedding - by jibarra - Jul-10-2019, 07:23 PM
RE: Python Embedding - by Gribouillis - Jul-11-2019, 05:11 AM
RE: Python Embedding - by jibarra - Jul-11-2019, 01:54 PM
RE: Python Embedding - by Gribouillis - Jul-11-2019, 03:08 PM
RE: Python Embedding - by jibarra - Jul-11-2019, 03:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Embedding python script into html via pyscript pyscript_dude 7 1,572 Apr-16-2023, 11:17 PM
Last Post: pyscript_dude
  C++ python embedding comarius 0 834 Aug-26-2022, 02:01 AM
Last Post: comarius
Question Embedding a python file online Dreary35 0 1,535 Jun-10-2021, 05:05 PM
Last Post: Dreary35
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 1,729 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  Need help merging/embedding duckredbeard 10 3,454 Aug-13-2020, 04:48 AM
Last Post: duckredbeard
  Embedding return in a print statement Tapster 3 2,295 Oct-07-2019, 03:10 PM
Last Post: Tapster
  Calling Extended Embedding Python as shared library jibarra 0 2,235 Jul-19-2019, 05:22 PM
Last Post: jibarra
  More Python Embedding Trouble jibarra 3 2,948 Jul-11-2019, 09:25 PM
Last Post: Gribouillis
  Embedding or adding IDE like "repl" inside Flask app sray 1 2,238 Jul-03-2019, 03:13 PM
Last Post: nilamo
  Embedding Python into a simulator siggi 0 2,203 Apr-24-2019, 07:42 PM
Last Post: siggi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020