pythonnet icon indicating copy to clipboard operation
pythonnet copied to clipboard

Getting an error on python v9.10+ "Cannot create uninitialized instances of types requiring managed activation"

Open alxkos opened this issue 2 years ago • 1 comments

Environment

  • Pythonnet version: 3.0.1
  • Python version: 3.11.1
  • Operating System: windows
  • .NET Runtime: v4.0.30319

Details

  • using CLR with 3D app - Solidworks gives me an error:

Unhandled Exception: System.NotSupportedException: Cannot create uninitialized instances of types requiring managed activation. at System.Runtime.Serialization.FormatterServices.nativeGetUninitializedObject(RuntimeType type) at Python.Runtime.ClassObject.tp_new_impl(BorrowedReference tp, BorrowedReference args, BorrowedReference kw) at Python.Runtime.NativeCall.Call_3(IntPtr fp, BorrowedReference a1, BorrowedReference a2, BorrowedReference a3) at Python.Runtime.MetaType.tp_call(BorrowedReference tp, BorrowedReference args, BorrowedReference kw)

import clr clr.AddReference("C:\Program Files\SOLIDWORKS 2019\SOLIDWORKS\SolidWorks.Interop.sldworks.dll") <System.Reflection.RuntimeAssembly object at 0x00000285677E4900> from SolidWorks.Interop.sldworks import ISldWorks, SldWorksClass swApp = ISldWorks(SldWorksClass())

This happening only on python 3.10+ on 3.9 it works well.

update: The problem is not with the python version, but using VENV. Outside VENV its working well, but once I setup VENV (any ver) i'm getting the error.

alxkos avatar Jan 02 '23 16:01 alxkos

This is an issue with COM classes. Nobody likes to support COM.

There are two workarounds:

  1. Make a C# DLL that creates an instance of that class for you
  2. Use one of Python packages that work with COM directly

lostmsu avatar May 05 '23 05:05 lostmsu