[IronPython] Cannot import module from .dll directly

Dino Viehland dinov at microsoft.com
Wed May 19 01:31:15 CEST 2010


Jeff wrote:
> 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.

What if we required the extension to be named "_foo.ipyd" or "_foo.ipe"
or something along those lines which would be similar to CPython's
.pyd extension?  That way we wouldn't be querying random DLLs that
might or might not load and might have unexpected side effects.





More information about the Ironpython-users mailing list