Is there a way to use .NET DLL from Python

Discussion in 'Python' started by Huayang Xia, Feb 6, 2008.

  1. Huayang Xia

    Huayang Xia Guest

    Hello All,

    I have several .NET DLL (I have no source code for them), is there
    anyway to use them from python instead of from C#.

    Thanks,
    Huayang
     
    Huayang Xia, Feb 6, 2008
    #1
    1. Advertisements

  2. Huayang Xia

    Shane Geiger Guest

    The following links *may* put you on the right path:


    Calling DLL functions from Python (
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146847 ), a
    fairly complete description with some helper class code. Another example
    ( http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/181063 ) of
    using the calldll module to do this.

    calldll module: http://www.nightmare.com/software.html


    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146847







    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy
     
    Shane Geiger, Feb 6, 2008
    #2
    1. Advertisements

  3. I used to put my .dll files into the .DLL folder, so I could simply
    import them as I would with any other python module.
    But since I started using Ironpython 2.0 Alpha* this doesn't work
    anymore...
    Any hint?

    Luis
     
    Luis M. González, Feb 6, 2008
    #3
  4. Huayang Xia

    Huayang Xia Guest

    Is there anyway to import class (to generate objects) from .NET DLL?
     
    Huayang Xia, Feb 6, 2008
    #4
  5. Huayang Xia

    Huayang Xia Guest

    Or maybe we can do it in IronPython?
     
    Huayang Xia, Feb 6, 2008
    #5
  6. Both recipes are rather obsolete now, since version 2.5 ctypes is included
    in the standard library. Anyway they don't help the OP to load a .NET dll;
    there is PythonDotNET http://pythonnet.sf.net/ but I've never used it
    myself.
     
    Gabriel Genellina, Feb 6, 2008
    #6
  7. You can use PythonDotNET if you want to access .NET assemblies in
    CPython (the standard Python implementation written in C).

    Christian
     
    Christian Heimes, Feb 6, 2008
    #7
  8. Huayang Xia

    Fuzzyman Guest

    To access .NET types you either need to use IronPython or
    Python.NET. .NET assemblies are dependent on the .NET runtime to do
    anything and so can't be accessed with ctypes as other DLLs can.

    Michael Foord
    http://www.manning.com/foord
     
    Fuzzyman, Feb 7, 2008
    #8
  9. Huayang Xia

    Fuzzyman Guest


    The rule is probably still that the DLLs must be in a directory on
    sys.path for the interpreter to find them. Try adding the directory
    containing the assemblies to sys.path and see if you can add
    references to them.

    Michael Foord
    http://www.manning.com/foord
     
    Fuzzyman, Feb 7, 2008
    #9
  10. I tried adding the directory to sys.path.
    Still not working...
    Traceback (most recent call last):
    File , line unknown, in ##235
    File , line unknown, in _stub_##2
    ImportError: No module named ClassLibrary1
     
    Luis M. González, Feb 7, 2008
    #10
  11. Huayang Xia

    Fuzzyman Guest

    You need to add references to assemblies before you can import from
    the namespaces they contain.

    import clr
    clr.AddReference('ClassLibrary1')

    HTH

    Fuzzyman
    http://www.manning.com/foord
     
    Fuzzyman, Feb 7, 2008
    #11
  12. Oh, I know what you mean.
    But that was exactly the reason for having a .DLLs folder, isn't it?
    When you place an assembly into this folder, you avoid having to write
    this boilerplate code, and simply import the assembly as you would
    with a normal python module. At least, that´s how it worked in
    previous versions...
     
    Luis M. González, Feb 7, 2008
    #12
  13. In IronPython and with PythonDotNET you can import namespaces. Assembly
    names and name spaces don't have to be related. E.g. ClassLibrary1.dll
    may provide the namespace ClassLibA and ClassLibB.
    clr.AddReference('ClassLibrary1') loads the assembly 'ClassLibrary1' and
    makes all namespaces available to Python.

    Christian
     
    Christian Heimes, Feb 7, 2008
    #13
  14. Huayang Xia

    Huayang Xia Guest

    What's the difference between .NET DLL and normal C DLL? Do you mean
    after clr.AddReference('ClassLibrary1'), there is no need to import
    ClassLibrary1?
     
    Huayang Xia, Feb 7, 2008
    #14
  15. A normal DLL and an assembly DLL share only the header. The rest is
    totally different. You can see the DLL as a container for the CIL code.

    clr.AddReference('ClassLibrary1') makes the namespaces of the
    ClassLibrary1 assembly available to IronPython and PythonDotNET. import
    ClassLibrary1 imports the name space. You must import the assembly
    before you can use its name spaces.

    Christian
     
    Christian Heimes, Feb 7, 2008
    #15
  16. Huayang Xia

    Fuzzyman Guest

    No. You have always had to add references to assemblies before being
    able to use the namespaces they contain. You even have to do this with
    C# in Visual Studio.

    Michael
    http://www.manning.com/foord
     
    Fuzzyman, Feb 7, 2008
    #16
  17. Huayang Xia

    Huayang Xia Guest

    Is there any special .NET DLL format for IronPython to import (e.g.
    rename the DLL to .pyd). Why when I try to import the DLL, it always
    complain the module doesn't exist?
     
    Huayang Xia, Feb 7, 2008
    #17
  18. Huayang Xia

    Fuzzyman Guest

    First you need to make sure the DLL is on the path - and *then* you
    need to add a reference to the assembly (dll) as I showed earlier.

    Michael
     
    Fuzzyman, Feb 7, 2008
    #18
  19. This *should* work in both IronPython 1.x and IronPyton 2.0 - the catch though is that it's implemented in the default site.py we ship with. So if you do the usual thing and use CPython's Lib directory you'll lose this feature w/o copying it over.
     
    Dino Viehland, Feb 13, 2008
    #19
  20. Sorry Dino, I don't understand...
    What does Cpython's Lib have to do with it?
     
    Luis M. González, Feb 13, 2008
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...