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

Denis Akhiyarov denis.akhiyarov at gmail.com
Mon Sep 26 13:54:22 EDT 2016


@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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20160926/a77ebd65/attachment-0001.html>


More information about the PythonDotNet mailing list