[IronPython] Problems with Imports

Dino Viehland dinov at exchange.microsoft.com
Wed Mar 21 19:56:21 CET 2007

Thanks for reporting this.  I've opened bug #9066 to track this.  If you don't need to add custom runtime behavior to the objects as a work around you could create a new module using imp.new_module and push the values into there.  I'm marking this as a v2.0 bug but if it's blocking you let me know.

On the 2nd example you might want to try using a type other than sys as the module type.  Sys is a little special in IronPython.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
Sent: Wednesday, March 21, 2007 7:02 AM
To: Discussion of IronPython
Subject: [IronPython] Problems with Imports

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

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

users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list