Getting an error on python v9.10+ "Cannot create uninitialized instances of types requiring managed activation"
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)
- 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.
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.
This is an issue with COM classes. Nobody likes to support COM.
There are two workarounds:
- Make a C# DLL that creates an instance of that class for you
- Use one of Python packages that work with COM directly