From fernandez_dan2 at hotmail.com Thu Jul 14 08:29:31 2016 From: fernandez_dan2 at hotmail.com (Daniel Fernandez) Date: Thu, 14 Jul 2016 06:29:31 -0600 Subject: [Python.NET] clr ImportError Message-ID: Hi All, I was wondering if anyone has ran into this before. I have latest pythonnet installed and everything is working great. I have been using for the last couple of weeks with python 3.5.2 (32 bit) on Win 7 box. I decided to try out ptpython so I pip install it. Now I am getting a "ImportError: dynamic module does not define module export function (PyInit_clr)" error. I go back to the normal python repl window and the same error occurs. Has anyone seen this before and what's the best way to debug this? Thanks. Danny -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Fri Jul 15 03:37:43 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Fri, 15 Jul 2016 02:37:43 -0500 Subject: [Python.NET] clr ImportError In-Reply-To: References: Message-ID: I would start with clean installation of pythonnet wheel from PyPi or source on master branch on github, preferable in conda env or virtualenv. Also make sure you have no left-over clr.pyd or Python.Runtime.dll files in system or python paths. I used pt(i)python with no problems before. But what happens if you remove it completely? It is possible to debug this, but let's first find out if this is repeatable? The way I normally debug this initialization code is by checking Debugger.IsAttached and sleeping the thread if the flag is false. On Thursday, July 14, 2016, Daniel Fernandez wrote: > Hi All, > > I was wondering if anyone has ran into this before. I have latest > pythonnet installed and everything is working great. I have been using for > the last couple of weeks with python 3.5.2 (32 bit) on Win 7 box. I decided > to try out ptpython so I pip install it. Now I am getting a > "ImportError: dynamic module does not define module export function > (PyInit_clr)" error. > > I go back to the normal python repl window and the same error occurs. Has > anyone seen this before and what's the best way to debug this? > > Thanks. > > Danny > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fernandez_dan2 at hotmail.com Sat Jul 16 15:54:45 2016 From: fernandez_dan2 at hotmail.com (Daniel Fernandez) Date: Sat, 16 Jul 2016 13:54:45 -0600 Subject: [Python.NET] clr ImportError In-Reply-To: References: , Message-ID: Hi Denis, I was able to get it working on my side but I needed to execute import clr as the first command then my scripts. I will try the recommend path below and see what I can find out below. Thanks. Danny Date: Fri, 15 Jul 2016 02:37:43 -0500 From: denis.akhiyarov at gmail.com To: pythondotnet at python.org Subject: Re: [Python.NET] clr ImportError I would start with clean installation of pythonnet wheel from PyPi or source on master branch on github, preferable in conda env or virtualenv. Also make sure you have no left-over clr.pyd or Python.Runtime.dll files in system or python paths. I used pt(i)python with no problems before. But what happens if you remove it completely? It is possible to debug this, but let's first find out if this is repeatable? The way I normally debug this initialization code is by checking Debugger.IsAttached and sleeping the thread if the flag is false. On Thursday, July 14, 2016, Daniel Fernandez wrote: Hi All, I was wondering if anyone has ran into this before. I have latest pythonnet installed and everything is working great. I have been using for the last couple of weeks with python 3.5.2 (32 bit) on Win 7 box. I decided to try out ptpython so I pip install it. Now I am getting a "ImportError: dynamic module does not define module export function (PyInit_clr)" error. I go back to the normal python repl window and the same error occurs. Has anyone seen this before and what's the best way to debug this? Thanks. Danny _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Sat Jul 16 20:44:22 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Sat, 16 Jul 2016 19:44:22 -0500 Subject: [Python.NET] clr ImportError In-Reply-To: References: Message-ID: I had similar issue before. import clr has to be at the top, not sure why. On Saturday, July 16, 2016, Daniel Fernandez wrote: > Hi Denis, > > I was able to get it working on my side but I needed to execute import clr > as the first command then my scripts. I will try the recommend path below > and see what I can find out below. > > Thanks. > > Danny > > ------------------------------ > Date: Fri, 15 Jul 2016 02:37:43 -0500 > From: denis.akhiyarov at gmail.com > > To: pythondotnet at python.org > > Subject: Re: [Python.NET] clr ImportError > > I would start with clean installation of pythonnet wheel from PyPi or > source on master branch on github, preferable in conda env or virtualenv. > Also make sure you have no left-over clr.pyd or Python.Runtime.dll files in > system or python paths. > > I used pt(i)python with no problems before. But what happens if you remove > it completely? > > It is possible to debug this, but let's first find out if this is > repeatable? > > The way I normally debug this initialization code is by checking > Debugger.IsAttached and sleeping the thread if the flag is false. > > > > On Thursday, July 14, 2016, Daniel Fernandez > wrote: > > Hi All, > > I was wondering if anyone has ran into this before. I have latest > pythonnet installed and everything is working great. I have been using for > the last couple of weeks with python 3.5.2 (32 bit) on Win 7 box. I decided > to try out ptpython so I pip install it. Now I am getting a > "ImportError: dynamic module does not define module export function > (PyInit_clr)" error. > > I go back to the normal python repl window and the same error occurs. Has > anyone seen this before and what's the best way to debug this? > > Thanks. > > Danny > > > _________________________________________________ Python.NET mailing list > - PythonDotNet at python.org > > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Tue Jul 19 12:10:25 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Tue, 19 Jul 2016 11:10:25 -0500 Subject: [Python.NET] Debugging .NET assembly from Python 3 In-Reply-To: References: Message-ID: @ *Pochon-Gu?rin Ludovic* just to clarify: to debug C# code called using pythonnet from CPython in PTVS you need to select ONLY "Managed" debugger engine. By default it selects "Managed + Python" which does not work. It is possible to get Managed + Native + CPython mixed- mode debugging working, but this requires additional steps. If you follow my steps and still experience issues, than please provide me with a simple reproducible example: C# + Python code. Note that there should not be any problem with Miniconda/Anaconda/conda python installation, because it is still CPython interpreter with debugging symbols provided in the installation. Unless you hijacked it with your custom CPython build :) On Thu, Jun 16, 2016 at 8:13 AM, Pochon-Gu?rin Ludovic < Ludovic.Pochon-Guerin at statkraft.com> wrote: > Thank you for your help. > > > > It is not possible to attach to the Python process the way explained in > PTVS discussion, because it is greyed. > > I think it is because I am already running the python process when > starting debugger from python file in Visual Studio > > > > I think my problem is due to the Python environment. > > I use Miniconda3 and not CPython interpreter in Visual Studio. > > It might be something to do with the CLR module too. > > > > Unfortunately I don?t have time to investigate further and will have to > work without debugging for the time being. > > > > Thanks anyway > > Ludovic > > > > > > *From:* PythonDotNet [mailto:pythondotnet-bounces+ludovic.pochon-guerin= > statkraft.com at python.org] *On Behalf Of *Denis Akhiyarov > *Sent:* 14. juni 2016 16:49 > *To:* A list for users and developers of Python for .NET > *Subject:* Re: [Python.NET] Debugging .NET assembly from Python 3 > > > > I regularly debugging C# code called from python side using pythonnet. So > something is likely wrong in your settings. > > > > Here is how you set debugger engines (see picture in the comments): > > https://github.com/Microsoft/PTVS/issues/674 > > > > This mailing list is super-slowly approved for new messages. You may get > much faster and better response on PTVS issue tracker: > > https://github.com/Microsoft/PTVS/issues/new > > Just provide Microsoft Python team with reproducible example. Also mention > what symbols you see in Debugger "Modules" window. > > > > If issue is very specific to pythonnet and works on ironpython, then you > can use pythonnet issue tracker: > > https://github.com/pythonnet/pythonnet/issues/new > > > > Note that PTVS has very specialized code for debugging IronPython, while > pythonnet debugging happens through CPython debugger. > > > > > > On Tue, Jun 14, 2016 at 4:46 AM, Pochon-Gu?rin Ludovic < > Ludovic.Pochon-Guerin at statkraft.com> wrote: > > Thank you for your answer, > > > > I tried all of this without success. > > > > What do you mean by ?select manually managed debugger?? > > > > Please can you clarify if it is possible to stop at breakpoints in C# code > when called from Python code (using python dot net)? > > > > Thanks > > Ludovic > > > > > > *From:* PythonDotNet [mailto:pythondotnet-bounces+ludovic.pochon-guerin= > statkraft.com at python.org] *On Behalf Of *Denis Akhiyarov > *Sent:* 8. juni 2016 08:32 > *To:* A list for users and developers of Python for .NET > *Subject:* Re: [Python.NET] Debugging .NET assembly from Python 3 > > > > There are many conversations (both codeplex and github) recorded in issue > tracker for PTVS about debugging pythonnet code. > > > > The starting point is to ensure that you are on .NET 4+, select manually > managed, native, and python debugger engines. Also make sure that managed > compatibility mode is not selected. Finally make sure that your python > debug symbols are available to PTVS/VS for cross-language debugging. > > > > Note that currently the PTVS remote debugger is not working for pythonnet > when debugging python code. > > > > > > On Tue, Jun 7, 2016 at 3:20 AM, Pochon-Gu?rin Ludovic < > Ludovic.Pochon-Guerin at statkraft.com> wrote: > > Hi, > > > > Using Visual Studio 2015, I have a Python project and a C# DLL project. > > From the Python code, I import and call functions from the C# class. > > When running in Debug, I am able to stop at breakpoints in the Python > code, but not in the C# code. > > The debugger jumps over the calls as if no symbols were loaded. > > > > I would like to know if it is possible to stop at breakpoints in the C# > code and how to setup it in Visual Studio. > > > > This feature works when using IronPython, but we want to use PythonDotNet > from Miniconda3. > > > > Thanks > > Ludovic > > > > > > > _________________________________________________ > 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wjchicago at gmail.com Thu Jul 28 09:51:50 2016 From: wjchicago at gmail.com (Jian Wang) Date: Thu, 28 Jul 2016 21:51:50 +0800 Subject: [Python.NET] PythonEngine partially initialized? Message-ID: Dear PythonNet group, I am working on a c# project which would like to access functions written in python from time to time. Below is a wrapper function. I have a unittest setup for this function. Everytime I run this unittest, it will get information from python world as I desired. public ScriptContext GetContextFromScript() { ScriptContext scriptContext; try { if (!PythonEngine.IsInitialized) PythonEngine.Initialize(); var threadState = PythonEngine.BeginAllowThreads(); var gilState = PythonEngine.AcquireLock(); PyObject mod = PythonEngine.ImportModule(this.GetModuleName()); PyObject instance = mod.InvokeMethod("Strategy", new PyTuple()); scriptContext =(ScriptContext)instance.InvokeMethod("SetContext", new PyTuple()).AsManagedObject(typeof (ScriptContext)); PythonEngine.ReleaseLock(gilState); PythonEngine.EndAllowThreads(threadState); PythonEngine.Shutdown(); } catch (Exception ex) { throw; } return scriptContext; } However, when I launch the UI and call this function by clicking some button, the program hangs at PythonEngine.Initialize(). After debugging into the PythonNet source code, I found when calling through UI, the RunTime.Initialize() will return true for Runtime.Py_IsInitialized(). internal static void Initialize() { is32bit = IntPtr.Size == 4; if (0 == Runtime.Py_IsInitialized()) { Runtime.Py_Initialize(); } if (0 == Runtime.PyEval_ThreadsInitialized()) { Runtime.PyEval_InitThreads(); } } And the whole initialize process will hang when result.Dispose() is called. It actually trying to acquire lock but cannot get it. using (StreamReader reader = new StreamReader(stream)) { // add the contents of clr.py to the module string clr_py = reader.ReadToEnd(); PyObject result = RunString(clr_py, module_globals, locals.Handle); if (null == result) throw new PythonException(); result.Dispose(); } It looks like something happend before code reaching this pythong wrapper function. PythonEninge is partially initialized with Runtime.Py_IsInitialized() = true and Runtime.PyEval_ThreadsInitialized() = false. Questions 1. what could be the possible cause for this situation? 2. If I cannot find the root cause and put BegineAllowThreads() there, what could be a solution for it? Many thanks! Jian -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Thu Jul 28 15:42:25 2016 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Thu, 28 Jul 2016 14:42:25 -0500 Subject: [Python.NET] PythonEngine partially initialized? In-Reply-To: References: Message-ID: 1. Jian, can you convert your code to simplified interface using Py.GIL() calls? If not possible, please let us know why. https://github.com/pythonnet/pythonnet/blob/master/README.md 2. Your first code sample (unit test) is self-contained, but in second code sample (UI) it is not clear where you acquire and release the GIL? 3. Finally can you post this as an issue on github? https://github.com/pythonnet/pythonnet/issues/new Thanks, Denis On Thu, Jul 28, 2016 at 8:51 AM, Jian Wang wrote: > Dear PythonNet group, > > I am working on a c# project which would like to access functions written > in python from time to time. Below is a wrapper function. > > I have a unittest setup for this function. Everytime I run this unittest, > it will get information from python world as I desired. > > public ScriptContext GetContextFromScript() > { > ScriptContext scriptContext; > try > { > if (!PythonEngine.IsInitialized) > PythonEngine.Initialize(); > var threadState = PythonEngine.BeginAllowThreads(); > var gilState = PythonEngine.AcquireLock(); > > PyObject mod = > PythonEngine.ImportModule(this.GetModuleName()); > PyObject instance = mod.InvokeMethod("Strategy", new > PyTuple()); > scriptContext > =(ScriptContext)instance.InvokeMethod("SetContext", new > PyTuple()).AsManagedObject(typeof (ScriptContext)); > > PythonEngine.ReleaseLock(gilState); > PythonEngine.EndAllowThreads(threadState); > PythonEngine.Shutdown(); > } > catch (Exception ex) > { > throw; > } > return scriptContext; > } > > However, when I launch the UI and call this function by clicking some > button, the program hangs at PythonEngine.Initialize(). > > After debugging into the PythonNet source code, I found when calling > through UI, the RunTime.Initialize() will return true for > Runtime.Py_IsInitialized(). > > internal static void Initialize() > { > is32bit = IntPtr.Size == 4; > > if (0 == Runtime.Py_IsInitialized()) > { > Runtime.Py_Initialize(); > } > > if (0 == Runtime.PyEval_ThreadsInitialized()) > { > Runtime.PyEval_InitThreads(); > } > } > > And the whole initialize process will hang when result.Dispose() is > called. It actually trying to acquire lock but cannot get it. > > using (StreamReader reader = new StreamReader(stream)) > { > // add the contents of clr.py to the module > string clr_py = reader.ReadToEnd(); > PyObject result = RunString(clr_py, > module_globals, locals.Handle); > if (null == result) > throw new PythonException(); > result.Dispose(); > } > > It looks like something happend before code reaching this pythong wrapper > function. PythonEninge is partially initialized with > Runtime.Py_IsInitialized() = true and Runtime.PyEval_ThreadsInitialized() = > false. > > Questions > 1. what could be the possible cause for this situation? > 2. If I cannot find the root cause and put BegineAllowThreads() there, > what could be a solution for it? > > Many thanks! > > Jian > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wjchicago at gmail.com Sat Jul 30 06:40:27 2016 From: wjchicago at gmail.com (Jian Wang) Date: Sat, 30 Jul 2016 18:40:27 +0800 Subject: [Python.NET] PythonEngine partially initialized? Message-ID: Denis, Thank you for your reply! 1. I did use simplified version at the very beginning. Just for debug purpose, I converted it to the explicit one. 2. UI and unit test actually access the same block of code, as pasted in my original email. 3. After trying to force the python interpreter shutdown, by calling Runtime.Py_Finalize()in PythonEngine.Initialize(), I got an exception thrown by a service used by the UI. So it is that service, which is not written by me, that starts up python interpreter. Now the problem has nothing to do with PythonNet interface. Regards, Jian > ---------------------------------------------------------------------- > > Message: 1 > Date: Thu, 28 Jul 2016 14:42:25 -0500 > From: Denis Akhiyarov > To: "A list for users and developers of Python for .NET" > > Subject: Re: [Python.NET] PythonEngine partially initialized? > Message-ID: > vKVg at mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > 1. Jian, can you convert your code to simplified interface using Py.GIL() > calls? If not possible, please let us know why. > > https://github.com/pythonnet/pythonnet/blob/master/README.md > > 2. Your first code sample (unit test) is self-contained, but in second code > sample (UI) it is not clear where you acquire and release the GIL? > > 3. Finally can you post this as an issue on github? > > https://github.com/pythonnet/pythonnet/issues/new > > Thanks, > Denis > > On Thu, Jul 28, 2016 at 8:51 AM, Jian Wang wrote: > > > Dear PythonNet group, > > > > I am working on a c# project which would like to access functions written > > in python from time to time. Below is a wrapper function. > > > > I have a unittest setup for this function. Everytime I run this unittest, > > it will get information from python world as I desired. > > > > public ScriptContext GetContextFromScript() > > { > > ScriptContext scriptContext; > > try > > { > > if (!PythonEngine.IsInitialized) > > PythonEngine.Initialize(); > > var threadState = PythonEngine.BeginAllowThreads(); > > var gilState = PythonEngine.AcquireLock(); > > > > PyObject mod = > > PythonEngine.ImportModule(this.GetModuleName()); > > PyObject instance = mod.InvokeMethod("Strategy", new > > PyTuple()); > > scriptContext > > =(ScriptContext)instance.InvokeMethod("SetContext", new > > PyTuple()).AsManagedObject(typeof (ScriptContext)); > > > > PythonEngine.ReleaseLock(gilState); > > PythonEngine.EndAllowThreads(threadState); > > PythonEngine.Shutdown(); > > } > > catch (Exception ex) > > { > > throw; > > } > > return scriptContext; > > } > > > > However, when I launch the UI and call this function by clicking some > > button, the program hangs at PythonEngine.Initialize(). > > > > After debugging into the PythonNet source code, I found when calling > > through UI, the RunTime.Initialize() will return true for > > Runtime.Py_IsInitialized(). > > > > internal static void Initialize() > > { > > is32bit = IntPtr.Size == 4; > > > > if (0 == Runtime.Py_IsInitialized()) > > { > > Runtime.Py_Initialize(); > > } > > > > if (0 == Runtime.PyEval_ThreadsInitialized()) > > { > > Runtime.PyEval_InitThreads(); > > } > > } > > > > And the whole initialize process will hang when result.Dispose() is > > called. It actually trying to acquire lock but cannot get it. > > > > using (StreamReader reader = new > StreamReader(stream)) > > { > > // add the contents of clr.py to the module > > string clr_py = reader.ReadToEnd(); > > PyObject result = RunString(clr_py, > > module_globals, locals.Handle); > > if (null == result) > > throw new PythonException(); > > result.Dispose(); > > } > > > > It looks like something happend before code reaching this pythong wrapper > > function. PythonEninge is partially initialized with > > Runtime.Py_IsInitialized() = true and > Runtime.PyEval_ThreadsInitialized() = > > false. > > > > Questions > > 1. what could be the possible cause for this situation? > > 2. If I cannot find the root cause and put BegineAllowThreads() there, > > what could be a solution for it? > > > > Many thanks! > > > > Jian > > > > _________________________________________________ > > 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/20160728/f4afa872/attachment-0001.html > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: