Pythonnet in C# - debugger stopped when PythonEngine.Initialize() is called - c#

I tried to run Python code from c# using pythonnet. I followed this intstuction: https://somegenericdev.medium.com/calling-python-from-c-an-introduction-to-pythonnet-c3d45f7d5232 but debugger stopped when I run the line PythonEngine.Initialize(). What is the reason and how to fix it?

Related

IyronPyton Error: Microsoft.Scripting.SyntaxErrorException: 'invalid syntax'

I am using IronPython version 3.4 to run Python script in C#.
I get the following error when I call a script that uses the "Instagrapi" package.
Error: Microsoft.Scripting.SyntaxErrorException: 'invalid syntax'
The problem/error occurs when the following line is executed:
var actual = source.Execute(scope);
(When I run the script in Python IDE, it runs without problems)
I have long researched and found out that Instagrapi requires Python version >= 3.8 and Ironpython supports syntax from Python version 3.4. Can this be the problem? Is there a solution for this to make the script work?
I've been trying to solve the problem for several days but unfortunately without success. I hope someone can help.

debug program.cs using visual studio remote debugger on IIS

I am trying to remotely debug a project with ASP.NET Core 2.1 that uses IIS as a server using the remote debugger tool, since I was able to establish the connection to the remote server and debug some things using breakpoints, the problem that is currently happening to me is that I can debug the file code as controllers,services, but I cannot debug the Program.cs file code using breakpoints.
Any ideas?
you could check the below things if you are not getting any error but breakpoint didn’t hit:
1)if you are running your code on more than one machine so the first check you are debugging the correct place code.
2)make sure your codding is running and you set the System.Diagnostics.Debugger.Break or __debugbreak( for c++) in your code where you want to set the breakpoint. (do not forget to build your project after adding this code)
3)If you are debugging optimized code, make sure the function where your breakpoint is set isn’t being inlined into another function. The Debugger.Break test described in the previous check can work to test this issue as well.
Add a call to Launch at the beginning of the OnStart()method.
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
}
How to: Debug the OnStart Method

Pythonnet stackoverflow exception in Python.Runtime.dll when calling Py.Import

I'm using python 3.5 installed in a virtual conda environment. The stackoverflow happens during a call to Py.Import:
dynamic np = Py.Import("numpy");
This was once working on my machine, but I decided to reinstall my Python environment. I set PythonEngine.PythonPath to the location of my virtual environment. My root Python install is also version 3.5. If I uninstall numpy, Py.Import fails expectedly due to the module not being found. If I reinstall numpy, I get a stackoverflow:
An unhandled exception of type 'System.StackOverflowException'
occurred in Python.Runtime.dll
Worker Thread Python.Runtime.dll!Python.Runtime.ImportHook.import Normal
[External Code]
The maximum number of stack frames supported by Visual Studio has been exceeded.
Importing built-in python modules works. I tried importing one of my own modules with a print statement at the first line, and the print statement gets called twice, so something is fishy. Any ideas why the stackoverflow exception is occurring?
Python 3.5.3
64-bit
numpy 1.13.1
VS2015
Pythonnet 2.30-py35-dotnet

Cannot run executable in TeamCity

I'm attempting to run a .Net console application (in TeamCity) I wrote which will change a variables value in a batch script located in the check out directory.
Unfortunately I'm receiving an error while running the step.
Process exited with code -2146232576
Currently I'm using a Command Line build step with the following settings:
Runner Type: Command Line
Step Name: Update setVars.bat
Execute Step: If all previous steps finished successfully
Run: Executable with parameters
Command Executable: E:\Test\Release\ReplaceText.exe
Command Parameters:
"%system.teamcity.build.checkoutDir%\DeploymentScripts\setVars.bat"
"(?m)(SET RunDate\s?=\s?[A-Za-z]{4}-[A-Za-z]{5}-(\d+[.]?)+)"
"SET RunDate=Build-%build.number%" "%system.teamcity.build.checkoutDir%\DeploymentScripts\setVars.bat"
There are 4 parameters listed.
1) - Where to locate the batch script
2) - The Regex pattern
3) - The replacement text
4) - The output directory
I'm unable to use the built in file replacer as it reverts back its changes once the build is successful.
I've run the console application on its own and it works just fine. So I'm unsure as to why team city is unable to run the application. The error code tells me nothing.
Any help would be great appreciated.
OK, I found the issue.
When I created the Console application I used VS 2017 with a .Net version of 4.6.2. That version of .Net is not installed on the CI server.
I changed the Console application to target .Net 4.5. This fixed the issue.

Debugging - Step into PowerShell Script Executed via API

I'm using System.Management.Automation.PowerShell to programmatically execute a PowerShell script from C# application. The PowerShell script loads a .Net dll which it uses to perform its activities.
var script = "Add-Type -Path 'MyLibrary.dll'; ....";
using (var powershell = PowerShell.Create()) {
powershell.AddScript(script);
powershell.Invoke();
}
Is it possible to somehow connect Visual Studio's debugger to the PowerShell object instance so that the debugger can seamlessly step from the C# application into the PowerShell script and from that script into MyLibrary.dll (assuming I have symbols for the dll)?
Edit: Based on the below, it appears that there may not be a way to seamlessly flow debugging in Visual Studio from C# to PowerShell. However, it is possible to use VS to debug the C# code that launches and that is launched by PowerShell.
You could debug the dll file by calling the following in your helper class:
System.Diagnostics.Debugger.Launch();

Categories

Resources