0

I am trying to import a dll package into python through pythonnet clr. I am aware that the package CLR and pythonnet both end up having a namespace called clr so the command "import clr" can be equivocal. Long story short, I seem to need pythonnet not the other one. I would like to be able to specify the address of the dll assembly; this one works:

import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
Apath=os.path.normpath("C://Folder//Folder//Folder//AssemblyA.dll")
clr.AddReference(Apath)

but this one fails (got the idea of this one from here):

import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
BfolderPath=os.path.normpath("C://Folder//Folder//Folder")
sys.path.append(BfolderPath)
clr.AddReference('AssemblyB.dll')

I get the following error when I try running the second one: "System.IO.FileNotFoundException: Unable to find assembly 'AssemblyB.dll'. at Python.Runtime.CLRModule.AddReference(String name)"

this one also fails

import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
BfolderPath=os.path.normpath("C://Folder//Folder//Folder")
clr.AddReferenceToFileAndPath(Bpath)

"AttributeError: module 'clr' has no attribute 'AddReferenceToFileAndPath'"

ps1. I need the second or the third way to work because I have to be sure the second assembly is not confused with another one with a similar name. ps2. I cannot find the documentation of pythonnet or see what kind of commands are available in my clr. Any idea?

Any tip is appreciated.

Father Geppeto
  • 139
  • 1
  • 8
  • 1
    In your second example: `clr.AddReference('AssemblyB.dll')` I believe when you're referencing an assembly without providing a directory to it, you use the assembly name, not the file name. Thus; `clr.AddReference('AssemblyB')` should work. – Mark Diedericks Apr 18 '20 at 04:35
  • Thanks for your tip, however, that is exactly the problem: the assembly in question has a name that is confused with another thing (a python module). This is why I need to directly add reference to the assembly by means of its full address. – Father Geppeto Apr 27 '20 at 12:30
  • Ah I see, only thing that comes to mind is to use the DLLs fullname if possible. E.g. `clr.AddReference("Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")` – Mark Diedericks Apr 29 '20 at 00:45
  • thanks, finally I see something I had not tried :). Looks promising. Will report back if it worked. – Father Geppeto Apr 30 '20 at 08:43
  • Father Geppeto any outcome? – jodá Feb 26 '21 at 10:19
  • @Johannes: truth be told, I got tired of this technicality and abandoned the idea of importing that blessed dll and searched for pure python alternatives instead for the job that required those dlls. – Father Geppeto Mar 06 '21 at 14:30

1 Answers1

1

In second idea,

remove .dll from clr.AddReference('AssemblyB.dll') and use clr.AddReference('AssemblyB') because clr.AddReference() Requires only assembly name whether is it .exe of .dll not the folder path

That's why first idea is not working!

And for third idea clr.AddReferenceToFileAndPath() is not working because it part of Ironpython not pythonnet.

Ironpython is also used like pythonnet, but Ironpython is using Managed python code, while pythonnet is not using Managed code.