Problems using __import__ from library

34 views
Skip to first unread message

ack

unread,
Nov 21, 2011, 6:55:30 PM11/21/11
to Reinteract
First my apologies: I've reported a similar case as a bug (https://
bugzilla.gnome.org/show_bug.cgi?id=664432 ). Sadly that report has
become somewhat of a mess, so I would like to ask to please close it.

Because I'm no longer sure whether these are problems with pyttsx,
Python itself or simply limitations of Reinteract which we will have
to learn to live with, I'm listing them here, in the hope that with
some discussion we can sort this out.

Given a notebook with the following directory structure:

TestImport/
TestImport/package
TestImport/package/__init__.py
TestImport/package/module.py
TestImport/package/subpkg
TestImport/package/subpkg/submod.py
TestImport/package/subpkg/__init__.py
TestImport/index.rnb
TestImport/test_import.rws

the goal is to have 'module.py' import a module from 'subpkg' whose
name isn't known until runtime:

Case 1 (package/module.py: __import__ with non-empty 'fromlist'):
...
name = 'submod'
module = __import__('subpkg.%s' % name, globals(), locals(),
fromlist=[name])

Results:
Reinteract: tries to import 'subpkg.submod.submod' and fails
Python: Works, `print module` will output "<module 'subpkg.submod'
from 'subpkg/submod.py'>", exactly what is wanted.
(Perhaps Python is more forgiving here, when it fails to import
'submod' as a module from the package 'subpkg.submod', it tries to
import it from the package 'subpkg' instead?)

Case 2 (package/module.py: Simplify the import following the advise in
http://docs.python.org/library/functions.html#__import__ ):
...
import sys
name = 'submod'
__import__('subpkg.%s' % name)
module = sys.modules[name]

Results:
Reinteract: doesn't work, raises a KeyError.
Python: Works, same result as in Case 1

Case 3 (test_import.rws: import module from package, discovered by
accident):
import package.module

Results:
Reinteract: Raises AssertionError (why?)

Hopefully someone can help me sort these out.

Kind regards,

ack
Python: Works.

ack

unread,
Nov 21, 2011, 7:17:32 PM11/21/11
to Reinteract

ack

unread,
Nov 21, 2011, 8:08:02 PM11/21/11
to Reinteract
Sorry again, Case 2 was specified wrong, and therefore doesn't work in
either Reinteract or as a Python script.

It appears that the advise in the Python docs doesn't hold for
__import__s done within modules within packages. Besides messing about
with __getattr__ etc., a working __import__ with 'fromlist=' seems to
me to be the only alternative...

-ack

ack

unread,
Nov 21, 2011, 10:10:18 PM11/21/11
to Reinteract
More news for Case 3:
It appears that `import package.module` will throw an AssertionError
until I evaluate an `import package` first. What strikes me as
'really' odd is that this works even if the `import package` is in
another worksheet!... What gives? :-)

-ack

Robert Schroll

unread,
Nov 21, 2011, 10:58:26 PM11/21/11
to reint...@googlegroups.com
On 11/21/2011 10:10 PM, ack wrote:
> Case 1 (package/module.py: __import__ with non-empty 'fromlist'):
> ...
> name = 'submod'
> module = __import__('subpkg.%s' % name, globals(), locals(),
> fromlist=[name])
>
> Results:
> Reinteract: tries to import 'subpkg.submod.submod' and fails
> Python: Works, `print module` will output "<module 'subpkg.submod'
> from 'subpkg/submod.py'>", exactly what is wanted.
> (Perhaps Python is more forgiving here, when it fails to import
> 'submod' as a module from the package 'subpkg.submod', it tries to
> import it from the package 'subpkg' instead?)
>
The problems seems to be that Reinteract tries to make sure that it can
find `a.b` when you run `__import__('a', globals(), locals(), ['b'])`.
Python doesn't care. I've attached a patch to the bug
(https://bugzilla.gnome.org/show_bug.cgi?id=664432) that makes
Reinteract more forgiving. I think that should solve this problem.

There are several "asserts" sprinkled throughout notebook.py. I'm not
sure what they're trying to protect, but I assume one of them is being
tripped here. Maybe Owen can enlighten us.

Robert

ack

unread,
Nov 22, 2011, 4:08:13 AM11/22/11
to Reinteract
Thanks once again Robert for the quick reply and patch! :-) Works well
with pyttsx and some other quick tests.
I wish other software projects would have this incredible support and
these quick turnaround times!

Kind regards,

ack

Robert Schroll

unread,
Nov 22, 2011, 10:57:38 AM11/22/11
to reint...@googlegroups.com
On 11/22/2011 04:08 AM, ack wrote:
> Thanks once again Robert for the quick reply and patch! :-)

You're welcome. Having simple test cases was a big help. Also, I had
plenty of real work to avoid by playing with Reinteract.

Robert

Reply all
Reply to author
Forward
0 new messages