Re: [Tutor] RE Embedding Python in C

2019-07-09 Thread Alan Gauld via Tutor
On 09/07/2019 15:13, Ibarra, Jesse wrote:

Caveat: I'm no expert on embedding and indeed have only done
it once using the examples in the docs. However, based on
my general Python experience...

> 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");

These import the same module twice. But Python usually checks if a
module is already imported so I'm guessing it does the same here.

>   Py_XDECREF(pModule);
>   Py_XDECREF(pName);

Maybe if you decrement the count after each import you will
get the result you want?

Although I'm guessing that relying on code to run via an import
is probably a bad practice. You'd normally expect to import
the module then call a function.

But that's mostly guesswork, so I could well be wrong!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] RE Embedding Python in C

2019-07-09 Thread Ibarra, Jesse
Sorry for the duplicate threads but the forwarded message did not send the 
original email. I apologize for any inconvenience.
The file are below.

I am running CentOS7:

[jibarra@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@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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor