[IronPython] Cannot import module from .dll directly

Jeff Hardy jdhardy at gmail.com
Mon May 17 22:54:57 CEST 2010


When CPython is searching for modules to import, it will check all the
folders in sys.path for .py, .pyc/.pyo, and .pyd/.dll files that match
the requested import. For IronPython, .py is obviously supported,
.pyc/.pyo are obviously(?) not, and neither are .pyd/.dll. Is there
any particular reason that IronPython does not try to find .dll files
when importing? There are many Python libraries that work this way
(PIL was the one that prompted this, but PyCrypto works in a similar
way) and it's easier than having to clr.AddReference assemblies.

To be clear, I'm taking about a layout like this:

foo
+-- __init__.py
+-- _foo.dll

where __init__.py contains:
    from _foo import *

and _foo.dll is an IronPython module similar to:

    [assembly: IronPython.Runtime.PythonModule("_foo",
typeof(IronPython.Foo.Foo))]

    namespace IronPython.Foo
    {
        public class Foo
        {
            public int Frob(int x) { return x + 42; }
        }
    }

Currently, running this on IRonPython results in:

    >>> import foo
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "...\foo\__init__.py", line 1, in <module>
    ImportError: No module named _foo

Supporting this would make it much easier to port libraries that
require a native component; in many cases, the Python code could be
used as-is and only the "native" code swapped out.

- Jeff



More information about the Ironpython-users mailing list