Discussion:
[Python.NET] how to load and execute py file which consumes a thrid part C-based python extension in C# code
lizy10b
2013-03-28 12:31:39 UTC
Permalink
Hi there,
I am a freshman and new to python.net.
I have a *.py script which calls a third part python library, and the library is a ctypes python wrapper of a C++ dll (or say C-based python extension?).
Frist I tried to make it using IronPython. IronPython works well when I load a pure python *.py (no third part libraries) file and execute it in C# code.
But it failed when I load a *.py which comsumes a thrid part ctypes python wrapper library mentioned above.

Then I turn to python.net. But I don't know how to load and execute *.py in C# code using python.net.

Thanks.

Zhiyu Li
Student
China

2013-03-28



lizy10b
Bradley Friedman
2013-03-28 14:15:12 UTC
Permalink
What you are looking to do is "embed" a python interpreter in your .net project, using pythonnet, and then tell the runtime to execute your python script.

I have done this in one of my projects. It does work.

http://pythonnet.sourceforge.net/readme.html#embedding

Sent from my iPad

On Mar 28, 2013, at 8:31 AM, "lizy10b" <lizy10b-***@public.gmane.org> wrote:

> Hi there,
> I am a freshman and new to python.net.
> I have a *.py script which calls a third part python library, and the library is a ctypes python wrapper of a C++ dll (or say C-based python extension?).
> Frist I tried to make it using IronPython. IronPython works well when I load a pure python *.py (no third part libraries) file and execute it in C# code.
> But it failed when I load a *.py which comsumes a thrid part ctypes python wrapper library mentioned above.
>
> Then I turn to python.net. But I don't know how to load and execute *.py in C# code using python.net.
>
> Thanks.
>
> Zhiyu Li
> Student
> China
>
> 2013-03-28
> lizy10b
> _________________________________________________
> Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
> http://mail.python.org/mailman/listinfo/pythondotnet
lizy10b
2013-03-28 18:09:31 UTC
Permalink
Thank you, Bradley.
I found a simple sample code on the maillist. Now I could load and execute a pure python function in a py file in C#.

And a new problem is that the PythonEngine.ImportModule("modulename") method always return NULL in my case. The modulename.py comsumes a third part ctypes wrapped library (the Rtree 0.7 python library). It seems the PythonEngine can not find it, but actually it has been installed to the default path (C:\Python262\Lib\site-packages\rtree) , and the modulename.py file runs smoothly under python command line.
I worte a new py file which only contains a function returning the sys.path, and load it in C#, the sys.path seems OK.
So I don't know what's wrong....

Thanks.

Zhiyu Li




2013-03-29



lizy10b



·¢ŒþÈË£º Bradley Friedman
·¢ËÍʱŒä£º 2013-03-28 22:15:18
ÊÕŒþÈË£º lizy10b
³­ËÍ£º pythondotnet
Ö÷Ì⣺ Re: [Python.NET] how to load and execute py file which consumes a thrid part C-based python extension in C# code

What you are looking to do is "embed" a python interpreter in your .net project, using pythonnet, and then tell the runtime to execute your python script.


I have done this in one of my projects. It does work.


http://pythonnet.sourceforge.net/readme.html#embedding

Sent from my iPad

On Mar 28, 2013, at 8:31 AM, "lizy10b" <***@126.com> wrote:


Hi there,
I am a freshman and new to python.net.
I have a *.py script which calls a third part python library, and the library is a ctypes python wrapper of a C++ dll (or say C-based python extension?).
Frist I tried to make it using IronPython. IronPython works well when I load a pure python *.py (no third part libraries) file and execute it in C# code.
But it failed when I load a *.py which comsumes a thrid part ctypes python wrapper library mentioned above.

Then I turn to python.net. But I don't know how to load and execute *.py in C# code using python.net.

Thanks.

Zhiyu Li
Student
China

2013-03-28



lizy10b
_________________________________________________
Python.NET mailing list - ***@python.org
http://mail.python.org/mailman/listinfo/pythondotnet
b***@public.gmane.org
2013-03-28 21:34:51 UTC
Permalink
In my code I'm running:

Python.Runtime.PythonEngine.Initialize();

I also have these defined:

public void AddPythonPath(string path)
{
CheckInitPython();
IntPtr l = Python.Runtime.PythonEngine.AcquireLock();
Python.Runtime.PythonEngine.RunSimpleString("import sys");
Python.Runtime.PythonEngine.RunSimpleString("sys.path.append(\"" + path.Replace("\\", "\\\\") + "\")");
Python.Runtime.PythonEngine.ReleaseLock(l);
}

public Python.Runtime.PyObject ImportModule(string name)
{
CheckInitPython();
IntPtr l = Python.Runtime.PythonEngine.AcquireLock();
Python.Runtime.PyObject module = Python.Runtime.PythonEngine.ImportModule(name);
Python.Runtime.PythonEngine.ReleaseLock(l);
if (module == null)
{
throw new libscclient.PythonModuleNotCreatedException(name);
}
return module;
}

And when my code loads a "plugin" python module, it runs this:

string pythonVersionBasePath = GetPythonCodeBaseVersionPath(); //my dynamic plugin code.
string scriptName = GetPythonScriptName(); //also my dynamic plugin code.
System.Console.WriteLine("importing module");
Pedestal.AddPythonPath(pythonVersionBasePath); //calling code above
Python.Runtime.PyObject module = Pedestal.ImportModule(scriptName.Split(new char[] { '.' }, 2)[0]); //calling code above
System.Console.WriteLine("module imported");
IntPtr l = Pedestal.GetPythonLock();
module.InvokeMethod(GetPythonInitMethodName(), new Python.Runtime.PyObject[] { Python.Runtime.PyObject.FromManagedObject(this) }); //calling a method by name. the name comes from my dynamic plugin code.
module.Dispose(); //I'm done with the module so I toss it. my code here registers callbacks.
Pedestal.ReleasePythonLock(l);

On Mar 28, 2013, at 2:09 PM, "lizy10b" <lizy10b-***@public.gmane.org> wrote:

> Thank you, Bradley.
> I found a simple sample code on the maillist. Now I could load and execute a pure python function in a py file in C#.
>
> And a new problem is that the PythonEngine.ImportModule("modulename") method always return NULL in my case. The modulename.py comsumes a third part ctypes wrapped library (the Rtree 0.7 python library). It seems the PythonEngine can not find it, but actually it has been installed to the default path (C:\Python262\Lib\site-packages\rtree) , and the modulename.py file runs smoothly under python command line.
> I worte a new py file which only contains a function returning the sys.path, and load it in C#, the sys.path seems OK.
> So I don't know what's wrong....
>
> Thanks.
>
> Zhiyu Li
>
>
>
>
> 2013-03-29
> lizy10b
> ·¢ŒþÈË£º Bradley Friedman
> ·¢ËÍʱŒä£º 2013-03-28 22:15:18
> ÊÕŒþÈË£º lizy10b
> ³­ËÍ£º pythondotnet
> Ö÷Ì⣺ Re: [Python.NET] how to load and execute py file which consumes a thrid part C-based python extension in C# code
> What you are looking to do is "embed" a python interpreter in your .net project, using pythonnet, and then tell the runtime to execute your python script.
>
> I have done this in one of my projects. It does work.
>
> http://pythonnet.sourceforge.net/readme.html#embedding
>
> Sent from my iPad
>
> On Mar 28, 2013, at 8:31 AM, "lizy10b" <lizy10b-***@public.gmane.org> wrote:
>
>> Hi there,
>> I am a freshman and new to python.net.
>> I have a *.py script which calls a third part python library, and the library is a ctypes python wrapper of a C++ dll (or say C-based python extension?).
>> Frist I tried to make it using IronPython. IronPython works well when I load a pure python *.py (no third part libraries) file and execute it in C# code.
>> But it failed when I load a *.py which comsumes a thrid part ctypes python wrapper library mentioned above.
>>
>> Then I turn to python.net. But I don't know how to load and execute *.py in C# code using python.net.
>>
>> Thanks.
>>
>> Zhiyu Li
>> Student
>> China
>>
>> 2013-03-28
>> lizy10b
>> _________________________________________________
>> Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
>> http://mail.python.org/mailman/listinfo/pythondotnet
b***@public.gmane.org
2013-03-28 21:49:01 UTC
Permalink
Sorry. One more.

When my code then executes those callbacks, back into Python, it does so this way:

System.Console.WriteLine("Getting Projects");
List<libcontextutilsclient.ProjectContextSettings> ret = new List<libcontextutilsclient.ProjectContextSettings>(); //the things I expect python to give me back
System.Console.WriteLine("Getting lock");
IntPtr p = this.Pedestal.GetPythonLock();
System.Console.WriteLine("Got Lock");
if (this.ProjectsRequested != null)
{
foreach (libcontextutils.GetProjectSettingsDelegate del in this.ProjectsRequested.GetInvocationList()) //looping through callbacks on delegate
{
System.Console.WriteLine("Calling Project Delegate");
ret.AddRange(del(projectsType, this)); //doing the callback, which is a python callback in truth.
}
}
else
{
System.Console.Error.WriteLine("No delegates registered to ProjectsRequested Event.");
}
System.Console.WriteLine("Releasing lock");
this.Pedestal.ReleasePythonLock(p);
System.Console.WriteLine("Released Lock");
return ret;

On Mar 28, 2013, at 5:34 PM, "brad-DLbl0k+***@public.gmane.org" <brad-DLbl0k+***@public.gmane.org> wrote:

> In my code I'm running:
>
> Python.Runtime.PythonEngine.Initialize();
>
> I also have these defined:
>
> public void AddPythonPath(string path)
> {
> CheckInitPython();
> IntPtr l = Python.Runtime.PythonEngine.AcquireLock();
> Python.Runtime.PythonEngine.RunSimpleString("import sys");
> Python.Runtime.PythonEngine.RunSimpleString("sys.path.append(\"" + path.Replace("\\", "\\\\") + "\")");
> Python.Runtime.PythonEngine.ReleaseLock(l);
> }
>
> public Python.Runtime.PyObject ImportModule(string name)
> {
> CheckInitPython();
> IntPtr l = Python.Runtime.PythonEngine.AcquireLock();
> Python.Runtime.PyObject module = Python.Runtime.PythonEngine.ImportModule(name);
> Python.Runtime.PythonEngine.ReleaseLock(l);
> if (module == null)
> {
> throw new libscclient.PythonModuleNotCreatedException(name);
> }
> return module;
> }
>
> And when my code loads a "plugin" python module, it runs this:
>
> string pythonVersionBasePath = GetPythonCodeBaseVersionPath(); //my dynamic plugin code.
> string scriptName = GetPythonScriptName(); //also my dynamic plugin code.
> System.Console.WriteLine("importing module");
> Pedestal.AddPythonPath(pythonVersionBasePath); //calling code above
> Python.Runtime.PyObject module = Pedestal.ImportModule(scriptName.Split(new char[] { '.' }, 2)[0]); //calling code above
> System.Console.WriteLine("module imported");
> IntPtr l = Pedestal.GetPythonLock();
> module.InvokeMethod(GetPythonInitMethodName(), new Python.Runtime.PyObject[] { Python.Runtime.PyObject.FromManagedObject(this) }); //calling a method by name. the name comes from my dynamic plugin code.
> module.Dispose(); //I'm done with the module so I toss it. my code here registers callbacks.
> Pedestal.ReleasePythonLock(l);
>
> On Mar 28, 2013, at 2:09 PM, "lizy10b" <lizy10b-***@public.gmane.org> wrote:
>
>> Thank you, Bradley.
>> I found a simple sample code on the maillist. Now I could load and execute a pure python function in a py file in C#.
>>
>> And a new problem is that the PythonEngine.ImportModule("modulename") method always return NULL in my case. The modulename.py comsumes a third part ctypes wrapped library (the Rtree 0.7 python library). It seems the PythonEngine can not find it, but actually it has been installed to the default path (C:\Python262\Lib\site-packages\rtree) , and the modulename.py file runs smoothly under python command line.
>> I worte a new py file which only contains a function returning the sys.path, and load it in C#, the sys.path seems OK.
>> So I don't know what's wrong....
>>
>> Thanks.
>>
>> Zhiyu Li
>>
>>
>>
>>
>> 2013-03-29
>> lizy10b
>> ·¢ŒþÈË£º Bradley Friedman
>> ·¢ËÍʱŒä£º 2013-03-28 22:15:18
>> ÊÕŒþÈË£º lizy10b
>> ³­ËÍ£º pythondotnet
>> Ö÷Ì⣺ Re: [Python.NET] how to load and execute py file which consumes a thrid part C-based python extension in C# code
>> What you are looking to do is "embed" a python interpreter in your .net project, using pythonnet, and then tell the runtime to execute your python script.
>>
>> I have done this in one of my projects. It does work.
>>
>> http://pythonnet.sourceforge.net/readme.html#embedding
>>
>> Sent from my iPad
>>
>> On Mar 28, 2013, at 8:31 AM, "lizy10b" <lizy10b-***@public.gmane.org> wrote:
>>
>>> Hi there,
>>> I am a freshman and new to python.net.
>>> I have a *.py script which calls a third part python library, and the library is a ctypes python wrapper of a C++ dll (or say C-based python extension?).
>>> Frist I tried to make it using IronPython. IronPython works well when I load a pure python *.py (no third part libraries) file and execute it in C# code.
>>> But it failed when I load a *.py which comsumes a thrid part ctypes python wrapper library mentioned above.
>>>
>>> Then I turn to python.net. But I don't know how to load and execute *.py in C# code using python.net.
>>>
>>> Thanks.
>>>
>>> Zhiyu Li
>>> Student
>>> China
>>>
>>> 2013-03-28
>>> lizy10b
>>> _________________________________________________
>>> Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
>>> http://mail.python.org/mailman/listinfo/pythondotnet
>
> _________________________________________________
> Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
> http://mail.python.org/mailman/listinfo/pythondotnet
lizy10b
2013-03-29 09:42:14 UTC
Permalink
Thanks for your code, Bradley

I followed your sample, add the path first , then PythonEngine.ImportModule,
spending all the day in embeding the third part C-based library into my C# code, but PythonEngine.ImportModule still return Null.
I wrote a simple test C dll (only one function returns a+1), and a wrapper py file using the ctypes.
Then load the py file in C#, the PythonEngine.ImportModule does return a non-null value, but when invoke the function is saids can not find the dll. When I put the dll in the Python root dir (on my end is C:\Python262\) , the py file runs smoothly. I try to add the path of the dll into sys.path, but does not work, only putting it into the Python root dir works.

So I think the situation is: third part C-base python library (in my case, rtree and psycopg2) uses more complex ctypes warpper, so maybe the python.net is incomptible to it? or I still have some chance to make it work, maybe still the sys.path problem?

Thanks

Zhiyu Li



2013-03-29



lizy10b



·¢ŒþÈË£º ***@fie.us
·¢ËÍʱŒä£º 2013-03-29 06:03:32
ÊÕŒþÈË£º lizy10b
³­ËÍ£º pythondotnet
Ö÷Ì⣺ Re: [Python.NET] how to load and execute py file which consumes a thrid part C-based python extension in C# code

Sorry. One more.


When my code then executes those callbacks, back into Python, it does so this way:


System.Console.WriteLine("Getting Projects");
List<libcontextutilsclient.ProjectContextSettings> ret = new List<libcontextutilsclient.ProjectContextSettings>(); //the things I expect python to give me back
System.Console.WriteLine("Getting lock");
IntPtr p = this.Pedestal.GetPythonLock();
System.Console.WriteLine("Got Lock");
if (this.ProjectsRequested != null)
{
foreach (libcontextutils.GetProjectSettingsDelegate del in this.ProjectsRequested.GetInvocationList()) //looping through callbacks on delegate
{
System.Console.WriteLine("Calling Project Delegate");
ret.AddRange(del(projectsType, this)); //doing the callback, which is a python callback in truth.
}
}
else
{
System.Console.Error.WriteLine("No delegates registered to ProjectsRequested Event.");
}
System.Console.WriteLine("Releasing lock");
this.Pedestal.ReleasePythonLock(p);
System.Console.WriteLine("Released Lock");
return ret;



On Mar 28, 2013, at 5:34 PM, "***@fie.us" <***@fie.us> wrote:


In my code I'm running:


Python.Runtime.PythonEngine.Initialize();



I also have these defined:


public void AddPythonPath(string path)
{
CheckInitPython();
IntPtr l = Python.Runtime.PythonEngine.AcquireLock();
Python.Runtime.PythonEngine.RunSimpleString("import sys");
Python.Runtime.PythonEngine.RunSimpleString("sys.path.append(\"" + path.Replace("\\", "\\\\") + "\")");
Python.Runtime.PythonEngine.ReleaseLock(l);
}

public Python.Runtime.PyObject ImportModule(string name)
{
CheckInitPython();
IntPtr l = Python.Runtime.PythonEngine.AcquireLock();
Python.Runtime.PyObject module = Python.Runtime.PythonEngine.ImportModule(name);
Python.Runtime.PythonEngine.ReleaseLock(l);
if (module == null)
{
throw new libscclient.PythonModuleNotCreatedException(name);
}
return module;
}



And when my code loads a "plugin" python module, it runs this:


string pythonVersionBasePath = GetPythonCodeBaseVersionPath(); //my dynamic plugin code.
string scriptName = GetPythonScriptName(); //also my dynamic plugin code.
System.Console.WriteLine("importing module");
Pedestal.AddPythonPath(pythonVersionBasePath); //calling code above
Python.Runtime.PyObject module = Pedestal.ImportModule(scriptName.Split(new char[] { '.' }, 2)[0]); //calling code above
System.Console.WriteLine("module imported");
IntPtr l = Pedestal.GetPythonLock();
module.InvokeMethod(GetPythonInitMethodName(), new Python.Runtime.PyObject[] { Python.Runtime.PyObject.FromManagedObject(this) }); //calling a method by name. the name comes from my dynamic plugin code.
module.Dispose(); //I'm done with the module so I toss it. my code here registers callbacks.
Pedestal.ReleasePythonLock(l);



On Mar 28, 2013, at 2:09 PM, "lizy10b" <***@126.com> wrote:


Thank you, Bradley.
I found a simple sample code on the maillist. Now I could load and execute a pure python function in a py file in C#.

And a new problem is that the PythonEngine.ImportModule("modulename") method always return NULL in my case. The modulename.py comsumes a third part ctypes wrapped library (the Rtree 0.7 python library). It seems the PythonEngine can not find it, but actually it has been installed to the default path (C:\Python262\Lib\site-packages\rtree) , and the modulename.py file runs smoothly under python command line.
I worte a new py file which only contains a function returning the sys.path, and load it in C#, the sys.path seems OK.
So I don't know what's wrong....

Thanks.

Zhiyu Li




2013-03-29



lizy10b



·¢ŒþÈË£º Bradley Friedman
·¢ËÍʱŒä£º 2013-03-28 22:15:18
ÊÕŒþÈË£º lizy10b
³­ËÍ£º pythondotnet
Ö÷Ì⣺ Re: [Python.NET] how to load and execute py file which consumes a thrid part C-based python extension in C# code
What you are looking to do is "embed" a python interpreter in your .net project, using pythonnet, and then tell the runtime to execute your python script.


I have done this in one of my projects. It does work.


http://pythonnet.sourceforge.net/readme.html#embedding

Sent from my iPad

On Mar 28, 2013, at 8:31 AM, "lizy10b" <***@126.com> wrote:


Hi there,
I am a freshman and new to python.net.
I have a *.py script which calls a third part python library, and the library is a ctypes python wrapper of a C++ dll (or say C-based python extension?).
Frist I tried to make it using IronPython. IronPython works well when I load a pure python *.py (no third part libraries) file and execute it in C# code.
But it failed when I load a *.py which comsumes a thrid part ctypes python wrapper library mentioned above.

Then I turn to python.net. But I don't know how to load and execute *.py in C# code using python.net.

Thanks.

Zhiyu Li
Student
China

2013-03-28



lizy10b
_________________________________________________
Python.NET mailing list - ***@python.org
http://mail.python.org/mailman/listinfo/pythondotnet


_________________________________________________
Python.NET mailing list - ***@python.org
http://mail.python.org/mailman/listinfo/pythondotnet
Loading...