[IronPython] Problems with Imports

Michael Foord michael.foord at resolversystems.com
Wed Mar 21 15:01:41 CET 2007


Hello all,

We do see some differences between the way IronPython and CPython 
resolve imports. (Tested with IP 1.0.1 I'm afraid.) We encountered these 
when patching sys.modules for the purposes of testing, but they could 
affect any code that does magic with sys.modules.

It looks like you can't put arbitrary objects into sys.modules in 
IronPython, but you can with CPython. The following code works fine on 
CPython, but fails on IP :

import sys
class MockModule(object):
   def __init__(self, name):
   self.__name__ = name


TopModule = MockModule("TopModule")
sys.modules["TopModule"] = TopModule

SubModule = MockModule("SubModule")
SubModule.Object = object()
TopModule.SubModule = SubModule
sys.modules["TopModule.SubModule"] = SubModule

import TopModule.SubModule
from TopModule.SubModule import Object


There is also another difference. The following code works fine on 
IronPython, but *not* on CPython


import sys
ModuleType = type(sys)

TopModule = ModuleType("TopModule")
sys.modules["TopModule"] = TopModule

SubModule = ModuleType("SubModule")
SubModule.Object = object()
TopModule.SubModule = SubModule
# Required for CPython, not IronPython
# sys.modules["TopModule.SubModule"] = SubModule

import TopModule.SubModule
from TopModule.SubModule import Object


You have to uncomment out 'the line' to get it to work in CPython. (Not 
worth fixing by the way - unless it means that something else is broken 
as a result - just a 'difference'.)

Michael Foord
http://www.resolversystems.com

-- 
Michael Foord
Resolver Systems
michael.foord at resolversystems.com

Office address:     17a Clerkenwell Road, London EC1M 5RD, UK
Registered address: 843 Finchley Road, London NW11 8NA, UK

Resolver Systems Limited is registered in England and Wales as company number 5467329.
VAT No. GB 893 5643 79 




More information about the Ironpython-users mailing list