Title: keyword arguments not supported on __import__
Type: Severity: normal
Components: Any Versions: 2.5alpha1
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: fwierzbicki, nriley, wesleys
Priority: Keywords:

Created on 2008-08-26.10:01:13 by wesleys, last changed 2008-10-13.17:21:38 by fwierzbicki.

msg3449 (view) Author: Wesley Schwengle (wesleys) Date: 2008-08-26.10:01:12

def load_modules(dir, module_prefix = None):
    files = os.listdir(dir)
    for filename in files:
        if not filename.endswith('.py') or not filename[0:1].isupper():

        file = filename[:-3]
        if module_prefix == None:
            mod = __import__(file, fromlist=filename)
            mod = __import__('%s.%s' % (module_prefix, file),

        c = getattr(mod, file)
        o = c()
        info(o) // prints information about the module/class

path = "/home/wesleys/sbox/blpython/src/lib/OSS/bladelogic"
load_modules(path, "OSS.bladelogic")

Running this code in python (2.5.1) works as expected, with jython it

Traceback (innermost last):
  File "", line 48, in ?
  File "", line 38, in load_modules
TypeError: __import__() takes no keyword arguments

Traceback (most recent call last):
  File "", line 48, in <module>
    load_modules(path, "OSS.bladelogic")
  File "", line 38, in load_modules
    mod = __import__('%s.%s' % (module_prefix, file), fromlist=filename)
TypeError: __import__() takes no keyword arguments
msg3450 (view) Author: Nicholas Riley (nriley) Date: 2008-08-27.03:13:25
You can work around this bug (in ImportFunction.__call__) by using positional 
rather than keyword arguments.  Pass {} for globals and locals in order to get 
to fromlist.

The function doesn't verify the number of positional arguments it's passed, 

Jython trunk:
>>> __import__('sys', {}, {}, [], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
<module 'sys' (built-in)>

Python 2.5:
>>> __import__('sys', {}, {}, [], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __import__() takes at most 5 arguments (14 given)
msg3451 (view) Author: Wesley Schwengle (wesleys) Date: 2008-08-27.03:47:30

had to change my code for loading the modules to:

        # Work around for
        # mod = __import__(modules, {}, {}, [ filename ], -1)
        mod = __import__(module_name)
        components = module_name.split('.')
        for comp in components[1:]:
            mod = getattr(mod, comp)

Works for 2.2.1 and 2.5a1.

BTW, from the documentation regarding __import__ I saw these notes:
Changed in version 2.5: The level parameter was added. Changed in
version 2.5: Keyword support for parameters was added.

Don't know if level is supported by jython, but just in case :)
msg3512 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-09-13.17:35:21
Relative imports are near the top of my todo list (though the things
above it may take a week or two more.  I've assigned this bug to myself
to get at the same time.
msg3675 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-10-13.17:21:38
Fixed in trunk r5384.  The level keyword is accepted but not yet acted upon.
Date User Action Args
2008-10-13 17:21:38fwierzbickisetstatus: open -> closed
resolution: fixed
messages: + msg3675
2008-09-13 17:35:21fwierzbickisetassignee: fwierzbicki
messages: + msg3512
nosy: + fwierzbicki
2008-08-27 03:47:31wesleyssetmessages: + msg3451
2008-08-27 03:13:27nrileysetnosy: + nriley
title: __import__() not working in 2.2.1 -> keyword arguments not supported on __import__
messages: + msg3450
versions: + 2.5alpha1, - 2.2.2
2008-08-26 10:01:13wesleyscreate