<div dir="ltr">@solospirit:<div><br></div><div>You are doing illegal things with Python and believe me - it bites back :)</div><div><br></div><div>Remember that in Python you cannot just import modules (.py) just from arbitrary paths, unless you are using importlib or sys.path.</div><div><br></div><div>Hence here is the correct workflow using sys.path:</div><div><br></div><div><div><b>Microsoft (R) Roslyn C# Compiler version 1.2.0.60425</b></div><div><b>Loading context from 'CSharpInteractive.rsp'.</b></div><div><b>Type "#help" for more information.</b></div><div><b>> #r "C:\Python\Python27_32b\Lib\site-packages\Python.Runtime.dll"</b></div><div><b>> using Python.Runtime;</b></div><div><b>> dynamic sys; using (Py.GIL()) { sys= Py.Import("sys"); }</b></div><div><b>> using (Py.GIL()) { sys.path.append(@"C:\Users\denis.akhiyarov\Downloads") ;  }</b></div><div><b>> dynamic testmodule; using (Py.GIL()) { testmodule = Py.Import("network2"); }</b></div><div><b>> testmodule.Network</b></div><div><b>[<class 'network2.Network'>]</b></div></div><div><br></div><div><br></div><div>Perhaps this should be documented here:</div><div><br></div><div><a href="https://github.com/pythonnet/pythonnet/wiki/pythonnet-troubleshooting-wiki-on-Windows">https://github.com/pythonnet/pythonnet/wiki/pythonnet-troubleshooting-wiki-on-Windows</a><br></div><div><br></div><div>Thanks,</div><div>Denis</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Sep 26, 2016 at 8:40 AM, solospirit <span dir="ltr"><<a href="mailto:solospirit@sohu.com" target="_blank">solospirit@sohu.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p>Hi,</p><p><br></p><p>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.</p><p><br></p><p>After read the document, i'm now able to call the following code within my WinForm application, and the result is correct.<br></p><p>            using (Py.GIL())</p><p>            {</p><p>                dynamic np = Py.Import("numpy");</p><p>                dynamic sin = np.sin;</p><p>                double c = np.cos(5) + sin(5);</p><p>                MessageBox.Show(c.ToString());</p><p>            }</p><p><br></p><p>I'm using a python script from <a href="https://github.com/mnielsen/neural-networks-and-deep-learning/blob/master/src/network2.py," target="_blank">https://github.com/<wbr>mnielsen/neural-networks-and-<wbr>deep-learning/blob/master/src/<wbr>network2.py</a> by git clone to my local machine c:\mypath\neural-networks-and-<wbr>deep-learning, it works inside python in my system.</p><p><br></p><p>I tried the following code, but the np returns NULL.<br></p><p style="white-space:normal">            using (Py.GIL())</p><p style="white-space:normal">            {</p><p style="white-space:normal">                dynamic np = Py.Import(@"<span style="line-height:21px">c:\mypath\neural-<wbr>networks-and-deep-learning\<wbr>src\network2</span>");</p><p style="white-space:normal">            }</p><p style="white-space:normal"><br></p><p style="white-space:normal">I also tried the following code, but <span style="line-height:21px">pModule also returns NULL</span></p><p style="white-space:normal">            string workingdir = @"<span style="line-height:21px">c:\mypath\neural-networks-<wbr>and-deep-learning\src</span>";</p><p>            PythonEngine.Initialize();</p><p>            IntPtr pythonLock = PythonEngine.AcquireLock();</p><p>            PythonEngine.RunSimpleString("<wbr>import sys\n");</p><p>            PythonEngine.RunSimpleString("<wbr>sys.path.append('" + workingdir + "')\n");</p><p>            PyObject pModule = PythonEngine.ImportModule(<wbr>workingdir + "\\network2.py");</p><p><br></p><p>How could I invoke such python script, thanks a lot!</p><p><br></p><br><br><br><hr size="1"><br><div><a href="http://score.mail.sohu.com/?ref=mail_tailad" target="_blank"><img border="0" src="http://ad.mail.sohu.com/mail/images/score_ad_foot1_750x79.png"> </a></div>
<br><br><br>
<br>______________________________<wbr>___________________<br>
Python.NET mailing list - <a href="mailto:PythonDotNet@python.org">PythonDotNet@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pythondotnet" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/pythondotnet</a><br></blockquote></div><br></div>