[docs] Python Embedding PyImport_ImportModule

Ibarra, Jesse jibarra4 at live.nmhu.edu
Tue Jun 25 11:23:35 EDT 2019


I am running CentOS7:

[jibarra at redsky ~]$ uname -a
Linux redsky.lanl.gov 3.10.0-957.21.2.el7.x86_64 #1 SMP Wed Jun 5 14:26:44 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

I am using Python3.6:
[jibarra at redsky ~]$ python3.6
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>


I was sucessfully ran example:
https://docs.python.org/3.6/extending/embedding.html#very-high-level-embedding

I am sucessfully ran https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html example and named it Rosenbrock_Function.py:

from scipy.optimize import minimize, rosen, rosen_der
x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
res = minimize(rosen, x0, method='Nelder-Mead', tol=1e-6)
print(res.x)

I then embedded the example using C/Python API:
https://docs.python.org/3.6/extending/embedding.html#pure-embedding

int main(){
  PyObject *pModule, *pName;

  Py_Initialize();
  PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path.append('.')");

  pModule = PyImport_ImportModule("Rosenbrock_Function");
  pName = PyImport_ImportModule("Rosenbrock_Function");

  Py_XDECREF(pModule);
  Py_XDECREF(pName);

  Py_Finalize();

  return 0;
}


Why does this code only print the result(array([ 1.,  1.,  1.,  1.,  1.])) once, when I am calling the python code twice?

Please advise,
Jesse Ibarra



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20190625/25cd8c34/attachment.html>


More information about the docs mailing list