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.
http://stackoverflow.com/questions/211100/pythons-import-doesnt-work-as-expected
http://stackoverflow.com/questions/2724260/why-does-pythons-import-require-fromlist
http://bugs.python.org/issue2090
Kind regards,
ack
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
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
Kind regards,
ack
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