question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.AccessViolationException for latest verion (3.0.0-preview2022-04-03)

See original GitHub issue

Environment

  • Pythonnet version: 3.0.0-preview2022-04-03
  • Python version: 3.9.7
  • Operating System: Windows 10 (version 21H2)
  • .NET Runtime: 5.0

Details

  • Describe what you were trying to get done.

    Run the example C# program.

  • What commands did you run to trigger this issue? If you can provide a Minimal, Complete, and Verifiable example this will help us understand the issue.

using Python.Runtime;
using System;
using System.Collections.Generic;

namespace PythonCondaEnvironment
{
    internal class Program
    {
        static void Main(string[] args)
        { 
            using (Py.GIL())
            {
                dynamic np = Py.Import("numpy");
                Console.WriteLine(np.cos(np.pi * 2));

                dynamic sin = np.sin;
                Console.WriteLine(sin(5));

                double c = (double)(np.cos(5) + sin(5));
                Console.WriteLine(c);

                dynamic a = np.array(new List<float> { 1, 2, 3 });
                Console.WriteLine(a.dtype);

                dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
                Console.WriteLine(b.dtype);

                Console.WriteLine(a * b);
                Console.ReadKey();
            }
        }
    }
}
  • If there was a crash, please include the traceback here.
System.AccessViolationException
  HResult=0x80004003
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

Note: the above code works for last version: 3.0.0-preview2022-03-03, but for this version: 3.0.0-preview2022-04-03, it is not working any more!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
731315163commented, Apr 6, 2022

it is need PythonEngine.Shutdown(); `// See https://aka.ms/new-console-template for more information global using System; using Python.Runtime; using Serilog;

static void Main() {

//init();
PythonEngine.Initialize();
using (Py.GIL())
{

}
PythonEngine.Shutdown();

// Excel.CreateFontPage();

}`

0reactions
zydjohnHotmailcommented, Apr 4, 2022

Hello: Thanks for your reply.
I figured it out: Add PYTHONHOME in user variable, and Add PYTHONPATH to system variable. Then add: PythonEngine.Initialize(); Before this statment: using (Py.GIL()) Then run my code, it works with latest version: 3.0.0-preview2022-04-03. I think the document is not well written for this issue. By the way, I didn’t try to use Python 3.10 yet. As far as I know, there are many package not working in Python 3.10 yet, so using the latest version of Python, you may have risk not able to run many programs. But thanks for your help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AccessViolationException Class (System)
Initializes a new instance of the AccessViolationException class with a system-supplied message that describes the error.
Read more >
c# - Programs randomly getting System. ...
I can confirm this problem. It occurs in a small (very trivial) WinForms program distributed with my own commercial app. It's never happened...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found