Pythonnet: cannot access to dll class - python.net

I'm having issues trying to load a Dll's class made in c# with pythonnet
Here's the dll project config (VS)
Here's the class code
using System;
namespace ClassLibrary1
{
public class Class1{
public void test()
{
Console.WriteLine("Hello from a library");
}
}
}
Here's the dll info
And the script
import clr
clr.AddReference('ClassLibraryAss')
from ClassLibraryAss import Class1
x = Class1()
x.test()
Is able to find the assembly. I follow some examples and it can't be more difficult than this.
This is the output
Traceback (most recent call last):
File ".\testDLL.py", line 23, in <module>
from ClassLibraryAss import Class1
ModuleNotFoundError: No module named 'ClassLibraryAss'
I tried different ways to load the module but with no luck
Environment
Pythonnet version: 2.5.2 (Also tried 2.5.1)
Python version: 3.8.0
Operating System: Windows 10
Visual studio 2019 (I didn't tried other)

In your from XXX import Class1 XXX should be the namespace where Class1 resides, not the DLL name

Turns out that the dll was made in .net core and pythonnet by default don't work with that.
Acording to the issue
So making the dll with .net framework works. In the issue says a way to make this run with net core dlls, I didn't try it though
import clr
#I made a dll with .net Framework 4.8 called ClassLibraryNetF
from ClassLibraryNetF import Class1
x = Class1()
x.test()

For anyone stumbling on this question while debuggin similar problem, pythonnet wouldn't load the assembly correctly if your script is being ran as a module, e.g. python -m Package.Module
and inside you've added assembly folder to sys.path
sys.path.append(os.path.join(os.getcwd(), "Assemblies"))
clr.AddReference("MyAssembly")
import MyAssembly
from MyAssembly import SimpleClass
Don't know why, but the latest version reproduces this on Win 10, python 3.9.
Probably something to do with the way pythonnet works with modules.
if you run it as a script from it's folder (python Module.py), it works fine.

Related

IntelliJ Idea - Not compiling class - but resolves library

I have a peculiar problem with IntelliJ
The project libraries are showing as resolved meaning any import from the library works fine. For example, import javax.servlet.http.HttpServletRequest;
But for the java sources within the module - it fails to import them. Even a simple interface with one method , For example - public interface X { void go()} and a class implementing public Y implements X - fails to import X.
I have also an ant build which i run inside docker which compiles the project fine !
The older import of the same project works fine and I am unable to find the difference between the two.
The ant build runs fine!!. It does not compile any file , I put a next line and save the File - still it will not resolve it !!
Community Edition 2019.3

Robot not able to import .java library

I am trying to import the 'SwingLibrary' into my Robot project and have had a lot of issues with it. I was able to work around my first error that seems very common by removing the version number from the file name:
Importing test library 'SwingLibrary' failed: ImportError: No module named SwingLibrary
Now everything is named SwingLibrary (jar & xml included) so I got a little further but I am now seeing the error:
Importing test library 'SwingLibrary' failed: Expected class or module, got javapackage.
I know Robot is supposed to know when it's trying to import a python or jython package but for some reason it doesn't seem to be working here. I did not have an issue importing the java version of Selenium or any of the standard libraries.

servicehost constructor doesn't initialize its members in linux (mono - vb.net)

I wrote a simple program since my application doesn't work in mono. But even that simple program doesn't work and I understand is the ServiceHost constructor that doens't initialize its member (despite the fact that in mono source code you can see correct instructions).
I use mono/.net 4.0 ('mono --version' says 4.0.1, monodevelop 5.10, ubuntu 14.04.2 lts (32bit). I've tried to add the dll needed (ver 4.0) in the exe path, but nothing change.
That's the easy code I uses (in .Net no errors, in linux "invalid IL code in System.ServiceModel.ServiceHostBase:get_Credentials() : IL_0000 ret").
Imports System.Security.Cryptography.X509Certificates
Imports System.servicemodel
Public Interface IMyService
Sub DoSomething()
End Interface
Public Class CMyService
Implements IMyService
Public Sub DoSomething() Implements IMyService.DoSomething
Console.WriteLine("-dosomething-")
End Sub
End Class
Module Module1
Sub Main()
Dim h As New ServiceHost(GetType(CMyService), New System.Uri("net.tcp://127.0.0.1/10000"))
'if you check 'h', you'll see all its member set to Nothing :(
h.Credentials.ClientCertificate.Authentication.CertificateValidationMode = Security.X509CertificateValidationMode.None
End Sub
End Module
It is compiled succesfully but it throws an exception if executed.
As reported on source code, if you check members of variable 'h' I have all its items set to 'Nothing'.
I've tried using 32bit and 64bit exe compiled on Windows 7 using Xamarin Studio and Visual Studio (they work great on Windows, but same error in mono). I used in ubuntu the dll copied from Windows .net 4.0. Obviously the 64bit version doesn't even start. I even compiled inside linux using monodevelop 5.10 but I get the same runtime error.
I changed "System.Security.Criptography.X509Certificates" in "Mono.Security.Criptography.X509Certificates" changing also the dll, but in linux I have the same error invariably.
What can I do/check?
I'm stuck for 2 days :/
Thank you in advance.
Update 1
I converted the source code in c#, compiled with mcs, which suggests using also System.IdentityModel.dll. The application in c# works this way. I then update the project in monodevelop, it compile and I can do step-by-step in that IDE.
Then I updated the vb.net project, adding the "missing" dll. It compiles again correctly under monodevelop and the step-by-step debug stops again for the same reason.
Then I compiled using vbnc and it works. But I cannot debug inside the IDE.
I cannot understand why... I hope I must not convert all my source code to C# to see other "missing dll", and guess strange behavour of monodevelop...
Maybe I try to add the 'missing' dll in Windows 7, compile and see what linux says.
Update 2
Linux doesn't like the program compiled in Windows 7, it must be compiled in linux using vbnc by command line.
The problem was solved compiling the test program in linux with vbnc called by command line (for some unknown reason, monodevelop compile correctly only the c# version and not the equivalent version in vb.net).
So it's impossible to do a step-by-step debuggin using monodevelop and vb.net, but it works using c#.
During compilation in c# (using msc) a warning about a missing dll (System.Identity.dll) appears. Microsoft exe doesn't need it, mono does, but only msc tell you about it. I find info about it nowhere, so maybe it can be usefull to someone else... I was 3 days stuck on this :/

Can't get System.Numerics to work with command-line Mono (mcs) on OS X

We're trying to run a simple Mono script on the command line on OS X. Most scripts work fine for us, but as soon as we try to use System.Numerics, we get "error CS0234: The type or namespace name `Numerics' does not exist in the namespace System."
This isn't too surprising, and should be fixable with an appropriate command-line option to mcs, plus properly set up PKG_CONFIG_PATH... but this is where we get stumped. First, here's the script so you can follow along at home:
using System;
using System.Numerics;
public static class MainProgram {
public static void Main(string[] args) {
Console.WriteLine("Hello world!");
}
}
So next we tried "mcs -r:System.Numerics Test.cs". This produces "error CS0006: Metadata file `System.Numerics' could not be found".
"man mcs" suggests that we can get the other system packages by adding "-pkg:dotnet" to the command line. But that produces:
Package dotnet was not found in the pkg-config search path. Perhaps
you should add the directory containing `dotnet.pc' to the
PKG_CONFIG_PATH environment variable No package 'dotnet' found error
CS8027: Error running pkg-config. Check the above output.
OK then, we had no PKG_CONFIG_PATH, so we tried defining one:
export PKG_CONFIG_PATH=/Library/Frameworks/Mono.framework/Versions/Current/lib/pkgconfig/
This fixes the CS8027; but we still get the CS0234 we started with. And if I combine the -pkg and the -r, e.g. "mcs -pkg:dotnet -r:System.Numerics Test.cs", I get "error CS0006: Metadata file `System.Numerics' could not be found".
I'm stumped at this point... any idea what incantation I'm missing to make System.Numerics work with mcs?
If you're using Mono 2.10.x, you will have to compile with dmcs rather than mcs to enable the 4.0 profile (System.Numerics is a C# 4.0+ feature only).
If you're using Mono 2.11.x or 3.0.x, then mcs by default should select the 4.5 profile. mcs -help should show 2, 4, and 4.5 as possible values for the -sdk option. If it doesn't, then the framework isn't properly installed; I had that once, where I think that /Library/Frameworks/Mono.framework/Versions/Current pointed to the wrong directory; installing a second time fixed that.
Manipulating pkg-config should be unnecessary.

Error with dll when importing Qt Desktop Components for PySide

I am trying to get Qt Quick Desktop Components to work with PySide 1.1.2 and Python 2.7. So far, I have compiled the latest version of the Desktop Components with MSVC in release mode and copied the components folder, containing both the .qml files and plugin/styleplugin.dll, to Python\Lib\site-packages\PySide\imports\QtDesktop.
Here is my current code:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import QDeclarativeView
app = QApplication(sys.argv)
view = QDeclarativeView()
view.setSource(QUrl('server.qml'))
view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
view.show()
sys.exit(app.exec_())
My Qml file:
import QtQuick 1.1
import QtDesktop 0.1
Window {
}
The error I am getting:
file:///C:/Programming/CibAbi/server.qml:15:1: plugin cannot be loaded for module "QtDesktop":
Cannot load library C:/Programming/Python27_32bit/Lib/site-packages/PySide/imports/QtDesktop/plugin/styleplugin.dll:
The specified procedure could not be found.
import QtDesktop 0.1
^
Does anybody know of a way to fix this, or does anyone have more specific steps on using the Desktop Components with PySide? Thank you. :)
Edit your system PATH variable so that the PySide directory appears in the path before any PyQt or other Qt directory.
http://qt-project.org/wiki/PySide_FAQ#6624847b60a84a6d81a5950805dfb6df
works perfectly
pyside py 3.3 32 Bit
Qt libraries 4.8.5 for Windows (VS 2010)
config release and build
rename folder "component" to "QtDesktop"
copy this folder en pyside/imports/
and Guala