0

I'm making a .net core app for Linux. The app make use of the pythonnet library to interact with python modules on the Linux box.

I'm trying to do some Debian package management using the python-apt module e.g. the apt.cache class.

This native python script works fine running directly on Linux printing all packages:

import apt
cache = apt.Cache()
for pkg_name in cache.keys():
     print(pkg_name)

My VB.net core code where I have a problem to declaring the apt.Cache() object correctly:

Runtime.PythonDLL = "/usr/lib/python3.9/config-3.9-x86_64-linux-gnu/libpython3.9.so"
Python.Runtime.PythonEngine.PythonHome = "/usr/lib/python3.9"
PythonEngine.Initialize()
Using Py.GIL
    Dim apt As PyObject = Py.Import("apt_pkg")
    Dim cache As PyDict = apt.Cache()
    ...
End Using

I get the exception:

_system not initialized at Python.Runtime.PythonException.ThrowLastAsClrException() at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw) at Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw) at Python.Runtime.PyObject.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result) at CallSite.Target(Closure , CallSite , Object ) at CallSite.Target(Closure , CallSite , Object ) at Microsoft.VisualBasic.CompilerServices.IDOUtils.CreateRefCallSiteAndInvoke(CallSiteBinder action, Object instance, Object[] arguments) at Microsoft.VisualBasic.CompilerServices.IDOBinder.IDOGet(IDynamicMetaObjectProvider instance, String memberName, Object[] arguments, String[] argumentNames, Boolean[] copyBack) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object instance, Type type, String memberName, Object[] arguments, String[] argumentNames, Type[] typeArguments, Boolean[] copyBack) at py_test_vb_core.Program.Main(String[] args) in Y:\Development projects~3rd part libraries\pythonnet\py_test_vb_core\py_test_vb_core\Program.vb:line 17

The documentation says apt.Cache() is a "dictionary-like object"

I use Visual Studio on Windows and I can connect to Linux box with SSH and can debug the .net core application just fine.

An additional questions: Is it possible to expose all class members etc. of the native python python-apt module in Visual Studio debugger? I don't dare to hope for IntelliSense support, but it would be a big help if I could see class members etc. of the python-apt module in the debug? It would also have helped me in declare the above object correctly.

(help in c# is also fine)

MrCalvin
  • 1,675
  • 1
  • 19
  • 27
  • 1
    You cut off Python exception details from your log, And from the part you posted it seems the exception happens in Python. – LOST Oct 16 '21 at 04:33
  • In Visual Studio you should be able to inspect `dynamic` objects. However, I only saw it in C#. – LOST Oct 16 '21 at 04:34
  • You might need to set `PYTHONHOME` via `PythonEngine.PythonHome`. – LOST Oct 16 '21 at 04:34

1 Answers1

0

I think I was just importing the wrong module, apt_pkg instead of apt

C# version (the embedded python code from above in .net):

static void Main(string[] args)
{
    Runtime.PythonDLL = "/usr/lib/python3.9/config-3.9-x86_64-linux-gnu/libpython3.9.so";
    //PythonEngine.PythonHome = "/usr/bin/python3"; //"/usr/lib/python3.9"; // 
    //PythonEngine.Initialize();
    using (Py.GIL())
    {
        dynamic APT = Py.Import("apt"); // Type: <module 'apt' from '/usr/lib/python3/dist-packages/apt/__init__.py'>
        dynamic Cache = APT.Cache();    // Type: <apt.cache.Cache object at 0x7fa701ec6760>
        dynamic PKGs = Cache.keys();    // Type: ['0ad', '0ad-data',...]
        foreach (string pkg_name in (String[])PKGs)
            {
                Console.WriteLine(pkg_name);
            }
    }
    Environment.Exit(0);
}
MrCalvin
  • 1,675
  • 1
  • 19
  • 27
  • I'm trying to figure out where you find the location of the pythonDLL on linux. I've search my system and can't find the libpython3.9.so file to use. How did you get that file? – Exzile Mar 23 '23 at 02:31