[Python.NET] call python in Visual C# WinForm application

Brian Lloyd brian.d.lloyd at gmail.com
Thu Oct 13 20:26:46 EDT 2016


Hi all - to clarify on Denis’ totally legit observation on the mailing list:

While I haven’t been able to be particularly active in python.net development for a number of years, I have been the moderator of the mailman list this whole time. We get a pretty huge amount of spam, which is why I’ve kept the list moderated to try to weed that out for folks.

While I try to keep up with it every day, sometimes I’m traveling or otherwise offline so sometimes legitimate messages languish for a day or two.

If some folks want to volunteer to help with moderation of the list I’m more than happy to open that up to a wider group - let me know if you are interested in lending a hand.



On 10/13/16, 6:41 AM, "PythonDotNet on behalf of Denis Akhiyarov" <pythondotnet-bounces+brian.d.lloyd=gmail.com at python.org on behalf of denis.akhiyarov at gmail.com> wrote:

>You may have much faster response on GitHub issues instead of using this mailing list:
>https://github.com/pythonnet/pythonnet/issues/new
>Sometimes it takes few days for messages to post on this mailing list!!!
>Did you try suggestions for resolving LoaderLock from Stackoverflow?
>http://stackoverflow.com/questions/12900192/loaderlock-was-detected-with-visual-studio-2012
>The problem with first code snippet is that you have ".py" at the end of module name.
>Inside WinForms there maybe threading issues, but try above first.
>Did you confirm that loading works without WinForms code?
>Cheers,
>Denis
>
>On Oct 12, 2016 8:59 PM, "solospirit" <solospirit at sohu.com> wrote:
>
>sorry, i just send it again, since i do not see it at https://mail.python.org/pipermail/pythondotnet/, i guess it might be lost, thanks.
>
>----- 原始邮件 -----
>
>发件人:solospirit<solospirit at sohu.com>
>发送时间:2016-10-11 22:18:27
>收件人:A list for users and developers of Python for .NET<pythondotnet at python.org>
>主 题:回复:Re: [Python.NET] call python in Visual C# WinForm application
>Thanks Denis.
>
>I tried the following code, it still does not work, the pModule is NULL.
>
>       //string scriptdir = @"C:\gitcode\neural-networks-and-deep-learning\src";
>        private void CallPython2(string scriptdir)
>        {
>            PythonEngine.Initialize();
>            IntPtr pythonLock = PythonEngine.AcquireLock();
>            PythonEngine.RunSimpleString("import sys\n");
>            PythonEngine.RunSimpleString("sys.path.append('" + scriptdir + "')\n");
>            PyObject pModule = PythonEngine.ImportModule("network2.py");
>        }
>
>I also tried the following code, there is exception, see below.
>       //string scriptdir = @"C:\gitcode\neural-networks-and-deep-learning\src";
>        private void CallPython3(string scriptdir)
>        {
>            using (Py.GIL())
>            {
>                dynamic sys = Py.Import("sys");
>                sys.path.append(scriptdir);
>                dynamic testmodule = Py.Import("network2");
>
>                //the following code works
>                //dynamic np = Py.Import("numpy");
>                //dynamic sin = np.sin;
>                //double c = np.cos(5) + sin(5);
>                //MessageBox.Show(c.ToString());
>            }
>        }
>
>Exception information:
>LoaderLock was detected
>Message: Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
>
>Actually, my script is called when a button on winform is clicked, it is not called as the above exception message says.
>
>
>----- 原始邮件 -----
>
>发件人:Denis Akhiyarov<denis.akhiyarov at gmail.com>
>发送时间:2016-09-27 01:54:22
>收件人:A list for users and developers of Python for .NET<pythondotnet at python.org>
>主 题:Re: [Python.NET] call python in Visual C# WinForm application
>@solospirit: 
> 
> 
> You are doing illegal things with Python and believe me - it bites back :)
>
> 
> Remember that in Python you cannot just import modules (.py) just from arbitrary paths, unless you are using importlib or sys.path.
>
> 
> Hence here is the correct workflow using sys.path:
>
> 
> Microsoft (R) Roslyn C# Compiler version 1.2.0.60425  
>Loading context from 'CSharpInteractive.rsp'.  
>Type "#help" for more information.  
>> #r "C:\Python\Python27_32b\Lib\site-packages\Python.Runtime.dll"  
>> using Python.Runtime;  
>> dynamic sys; using (Py.GIL()) { sys= Py.Import("sys"); }  
>> using (Py.GIL()) { sys.path.append(@"C:\Users\denis.akhiyarov\Downloads") ;  }  
>> dynamic testmodule; using (Py.GIL()) { testmodule = Py.Import("network2"); }  
>> testmodule.Network  
>[<class 'network2.Network'>]  
>
>
> 
> 
> 
> Perhaps this should be documented here:
>
> 
> https://github.com/pythonnet/pythonnet/wiki/pythonnet-troubleshooting-wiki-on-Windows   
>  
> 
> 
>
> 
> Thanks,
>Denis
>
> 
>
> 
> On Mon, Sep 26, 2016 at 8:40 AM, solospirit  < 
>  solospirit at sohu.com> wrote:   
>  
>  
>Hi,
>
>I'm using windows 7 64bit + Visual C#2012 to develop WinForm 32bit application with .Net Framework 4.0. I want to call some python script within my winform application.
>
>After read the document, i'm now able to call the following code within my WinForm application, and the result is correct.
>
>            using (Py.GIL())
>            {
>                dynamic np = Py.Import("numpy");
>                dynamic sin = np.sin;
>                double c = np.cos(5) + sin(5);
>                MessageBox.Show(c.ToString());
>            }
>
>I'm using a python script from https://github.com/mnielsen/neural-networks-and-deep-learning/blob/master/src/network2.py <https://github.com/mnielsen/neural-networks-and-deep-learning/blob/master/src/network2.py,> by git clone to my local machine c:\mypath\neural-networks-and-deep-learning, it works inside python in my system.
>
>I tried the following code, but the np returns NULL.
>
>            using (Py.GIL())
>            {
>                dynamic np = Py.Import(@"c:\mypath\neural-networks-and-deep-learning\src\network2");
>            }
>
>I also tried the following code, but pModule also returns NULL
>            string workingdir = @"c:\mypath\neural-networks-and-deep-learning\src";
>            PythonEngine.Initialize();
>            IntPtr pythonLock = PythonEngine.AcquireLock();
>            PythonEngine.RunSimpleString("import sys\n");
>            PythonEngine.RunSimpleString("sys.path.append('" + workingdir + "')\n");
>            PyObject pModule = PythonEngine.ImportModule(workingdir + "\\network2.py");
>
>How could I invoke such python script, thanks a lot!
>
>
>   
>   
>   
>   
>  ________________________________________
>
>    <http://score.mail.sohu.com/?ref=mail_tailad>  
>
>   
>   
>   
>   
>   
>   
>_________________________________________________   
>   
>Python.NET mailing list -   
>   PythonDotNet at python.org   
>   
>   
>   https://mail.python.org/mailman/listinfo/pythondotnet   
>   
>  
>
>
>
>
>
>
>
>
>
>
>
>
>_________________________________________________
>Python.NET mailing list - PythonDotNet at python.org
>https://mail.python.org/mailman/listinfo/pythondotnet
>
>
>
>
>_________________________________________________
>Python.NET mailing list - PythonDotNet at python.org
>https://mail.python.org/mailman/listinfo/pythondotnet



More information about the PythonDotNet mailing list