Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python for .NET

I have a problem loading an external dll using Python through Python for .NET. I have tried different methodologis following stackoverflow and similar. I will try to summarize the situation and to describe all the steps that I've done.

I have a dll named for e.g. Test.NET.dll. I checked with dotPeek and I can see, clicking on it, x64 and .NET Framework v4.5. On my computer I have installed the .Net Framework 4.

I have also installed Python for .NET in different ways. I think that the best one is download the .whl from this website LINK. I have downloaded and installed: pythonnet‑2.0.0.dev1‑cp27‑none‑win_amd64.whl. I can imagine that it will works for .NET 4.0 since Requires the Microsoft .NET Framework 4.0.

Once I have installed everything, I can do these commands:

>>> import clr
>>> import System
>>> print System.Environmnet.Version
>>> print System.Environment.Version
4.0.30319.34209

It seems work. Then, I have tried to load my dll typing these commands:

>>> import clr
>>> dllpath= r'C:\Program Files\API\Test.NET'
>>> clr.AddReference(dllpath)

Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    clr.AddReference(dllpath)
FileNotFoundException: Unable to find assembly 'C:\Program Files\API\Test.NET'.
   at Python.Runtime.CLRModule.AddReference(String name)

I have also tried to add '.dll' at the end of the path but nothing changed. Then, I have also tried different solutions as described in LINK, LINK, LINK, and much more.... Unfortunately, it doesn't work and I get different errors. I know that exists IronPython but I was trying to avoid to use it.

Thanks for your help!

like image 989
user3075816 Avatar asked Jun 05 '15 14:06

user3075816


People also ask

Can I use .NET with Python?

Python.NET is a package that gives Python programmers nearly seamless integration with the . NET 4.0+ Common Language Runtime (CLR) on Windows and Mono runtime on Linux and OSX. Python for . NET provides a powerful application scripting tool for .

Can you use Python with C#?

You can directly call python code from C# using the library http://pythonnet.github.io/.

Does IronPython require Python?

IronPython is an open-source implementation of the Python programming language which is tightly integrated with . NET. IronPython can use . NET and Python libraries, and other .

What is IronPython used for?

This means that IronPython can be used for client-side scripting in the browser. IronPython is a Python compiler. It compiles Python code to in memory bytecode before execution (which can be saved to disk, making binary only distributions possible).


1 Answers

Did Test.NET.dll come from a different computer? According to this thread some security features of .NET can prevent .dlls from loading nicely.

For a more informative error message, try

> from clr import System
> from System import Reflection
> full_filename = r'C:\Program Files\API\Test.NET'
> Reflection.Assembly.LoadFile(dllpath)

If you get an error message along the lines of

NotSupportedException: An attempt was made to load an assembly from a 
network location which would have caused the assembly to be sandboxed in 
previous versions of the .NET Framework. This release of the .NET Framework 
does not enable CAS policy by default, so this load may be dangerous. If 
this load is not intended to sandbox the assembly, please enable the 
loadFromRemoteSources switch.

then the following solved the problem for me:

  • right click on Test.NET.dll
  • select 'Properties'
  • under the 'General' tab, click on 'Unblock'
  • click on 'Apply'
like image 106
DrJSomeday Avatar answered Sep 29 '22 17:09

DrJSomeday