<html><head><meta http-equiv="Content-Type" content="text/html charset=GB2312"><base href="x-msg://6533/"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">In my code I'm running:<div><br></div><div>                Python.Runtime.PythonEngine.Initialize();<br><div><div><br></div><div>I also have these defined:</div><div><br></div><div>        public void AddPythonPath(string path)<br>        {<br>            CheckInitPython();<br>            IntPtr l = Python.Runtime.PythonEngine.AcquireLock();<br>            Python.Runtime.PythonEngine.RunSimpleString("import sys");<br>            Python.Runtime.PythonEngine.RunSimpleString("sys.path.append(\"" + path.Replace("\\", "\\\\") + "\")");<br>            Python.Runtime.PythonEngine.ReleaseLock(l);<br>        }<br><br>        public Python.Runtime.PyObject ImportModule(string name)<br>        {<br>            CheckInitPython();<br>            IntPtr l = Python.Runtime.PythonEngine.AcquireLock();<br>            Python.Runtime.PyObject module = Python.Runtime.PythonEngine.ImportModule(name);<br>            Python.Runtime.PythonEngine.ReleaseLock(l);<br>            if (module == null)<br>            {<br>                throw new libscclient.PythonModuleNotCreatedException(name);<br>            }<br>            return module;<br>        }<br></div><div><br></div><div>And when my code loads a "plugin" python module, it runs this:</div><div><br></div><div>            string pythonVersionBasePath = GetPythonCodeBaseVersionPath(); //my dynamic plugin code.<br>            string scriptName = GetPythonScriptName(); //also my dynamic plugin code.<br>            System.Console.WriteLine("importing module"); <br>            Pedestal.AddPythonPath(pythonVersionBasePath); //calling code above<br>            Python.Runtime.PyObject module = Pedestal.ImportModule(scriptName.Split(new char[] { '.' }, 2)[0]);  //calling code above<br>            System.Console.WriteLine("module imported");<br>            IntPtr l = Pedestal.GetPythonLock();<br>            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.<br>            module.Dispose(); //I'm done with the module so I toss it.  my code here registers callbacks.<br>            Pedestal.ReleasePythonLock(l);<br></div><div><br></div><div>On Mar 28, 2013, at 2:09 PM, "lizy10b" <<a href="mailto:lizy10b@126.com">lizy10b@126.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; margin: 10px; font-family: verdana; font-size: 10pt; "><div><font color="#000080" size="2" face="Verdana">Thank you,<span class="Apple-converted-space"> </span><font>Bradley.</font></font></div><div><font color="#000080" size="2" face="Verdana">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#. </font></div><div> </div><div>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.</div><div>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.</div><div>So I don't know what's wrong....</div><div> </div><div>Thanks.</div><div> </div><div>Zhiyu Li</div><div> </div><div> </div><div> </div><div><font color="#000080" size="2" face="Verdana"></font> </div><div><font color="#c0c0c0" size="2" face="Verdana">2013-03-29</font></div><font color="#000080" size="2" face="Verdana"><hr align="left" size="1" style="width: 100px; "></font><div><font color="#c0c0c0" size="2" face="Verdana">lizy10b</font></div><hr size="1"><div><font size="2" face="Verdana"><strong>发件人:</strong><span class="Apple-converted-space"> </span>Bradley Friedman</font></div><div><font size="2" face="Verdana"><strong>发送时间:</strong><span class="Apple-converted-space"> </span>2013-03-28  22:15:18</font></div><div><font size="2" face="Verdana"><strong>收件人:</strong><span class="Apple-converted-space"> </span>lizy10b</font></div><div><font size="2" face="Verdana"><strong>抄送:</strong><span class="Apple-converted-space"> </span>pythondotnet</font></div><div><font size="2" face="Verdana"><strong>主题:</strong><span class="Apple-converted-space"> </span>Re: [<a href="http://Python.NET" style="color: purple; text-decoration: underline; ">Python.NET</a>] how to load and execute py file which consumes a thrid part C-based python extension in C# code</font></div><div><font size="2" face="Verdana"></font></div><div><font size="2" face="Verdana"><div style="-webkit-text-size-adjust: auto; ">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. </div><div style="-webkit-text-size-adjust: auto; "><br></div><div style="-webkit-text-size-adjust: auto; ">I have done this in one of my projects. It does work. </div><div style="-webkit-text-size-adjust: auto; "><br></div><div style="-webkit-text-size-adjust: auto; "><span style="line-height: 19px; white-space: nowrap; font-size: 15px; -webkit-text-size-adjust: none; "><a href="http://pythonnet.sourceforge.net/readme.html#embedding" style="color: purple; text-decoration: underline; ">http://pythonnet.sourceforge.net/readme.html#embedding</a></span></div><div><br><span style="-webkit-text-size-adjust: auto; ">Sent from my iPad</span></div><div style="-webkit-text-size-adjust: auto; "><br>On Mar 28, 2013, at 8:31 AM, "lizy10b" <<a href="mailto:lizy10b@126.com" style="color: purple; text-decoration: underline; ">lizy10b@126.com</a>> wrote:<br><br></div><blockquote type="cite" style="-webkit-text-size-adjust: auto; "><div><font size="2" face="Verdana">Hi there,</font></div><div>   I am a freshman and new to<span class="Apple-converted-space"> </span><a href="http://python.net" style="color: purple; text-decoration: underline; ">python.net</a>.</div><div>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?).</div><div>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.</div><div>But it failed when I load a *.py which comsumes a thrid part ctypes python wrapper library mentioned above.</div><div> </div><div>Then I turn to<span class="Apple-converted-space"> </span><a href="http://python.net" style="color: purple; text-decoration: underline; ">python.net</a>. But I don't know how to load and execute *.py in C# code using<span class="Apple-converted-space"> </span><a href="http://python.net" style="color: purple; text-decoration: underline; ">python.net</a>.</div><div> </div><div>Thanks.</div><div> </div><div>Zhiyu Li</div><div>Student</div><div>China</div><div><font size="2" face="Verdana"></font> </div><div align="left"><font color="#c0c0c0" size="2" face="Verdana">2013-03-28</font></div><font size="2" face="Verdana"><hr align="left" size="2" style="width: 122px; height: 2px; "><div><font color="#c0c0c0" size="2" face="Verdana"><span>lizy10b</span></font></div></font></blockquote><blockquote type="cite" style="-webkit-text-size-adjust: auto; "><span>_________________________________________________</span><br><span><a href="http://Python.NET" style="color: purple; text-decoration: underline; ">Python.NET</a><span class="Apple-converted-space"> </span>mailing list -<span class="Apple-converted-space"> </span><a href="mailto:PythonDotNet@python.org" style="color: purple; text-decoration: underline; ">PythonDotNet@python.org</a></span><br><span><a href="http://mail.python.org/mailman/listinfo/pythondotnet" style="color: purple; text-decoration: underline; ">http://mail.python.org/mailman/listinfo/pythondotnet</a></span></blockquote></font></div></div></blockquote></div><br></div></body></html>