Menu

#48 "from nzmath import *" does not work properly

1.1.0
open
naoki
None
5
2011-09-13
2011-08-31
No

When you start using nzmath via "from nzmath import *", some subpackage modules are visible but some are not;
nzmath.factor modules are all visible but newly added nzmath.poly.groebner, for example, is not visible.
My guess of the cause is that those visible modules are imported from some other visible modules, but invisibles are not.

I'm not very sure how to make "from nzmath import *" works. Perhaps explicitly import them from somewhere, e.g. nzmath.poly.multiutil.

This bug is found by Hamada-san of Knoppix/math.

code example:

>>> from nzmath import *
>>> factor.misc
<module 'nzmath.factor.misc' from 'nzmath/factor/misc.pyc'>
>>> poly.uniutil
<module 'nzmath.poly.uniutil' from 'nzmath/poly/uniutil.pyc'>
>>> poly.groebner
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'groebner'
>>> poly.hensel
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'hensel'

Discussion

  • naoki

    naoki - 2011-09-13
    • assigned_to: nobody --> naoki_labo
     
  • naoki

    naoki - 2011-09-13

    In our discussion today, we consider adding a new module,
    which includes statements for importing modules in subpackages such as 'poly' or 'factor'.

    We check this matter more detail.
    -----------------------------------------------------------
    From what little we have been able to gather, "from nzmath import *" probably works as follows:
    1. Find and load the module "nzmath/__init__.py".
    2. Find and load modules specified by the variable "nzmath.__all__".
    3. Delete the name 'nzmath'.

    For example, the word 'poly' is in "nzmath.__all__", so "nzmath/poly/__init__.py" is loaded.

    However, we cannot load modules in subpackages such as "nzmath/poly/hensel.py", because "poly.hensel" is not in "nzmath.__all__".
    (If you write it, it causes AttributeError.)

    On the other hand, if you import a module which includes internal import statements,
    the internally imported modules seem to affect the global namespace.
    For instance, we consider the following code example:

    >>> from nzmath import arith1
    >>> arith1.gcd
    <module 'nzmath.gcd' from 'nzmath/gcd.pyc'>
    >>> import nzmath
    >>> nzmath.gcd
    <module 'nzmath.gcd' from 'nzmath/gcd.pyc'>

    The module 'arith1' includes the statement "import nzmath.gcd as gcd".
    So Python seems to save the module name "nzmath.gcd".
    Then, once the package name 'nzmath' is available, you can access the module "nzmath.gcd".
    The same can be said for "nzmath.poly.multiutil".
    -----------------------------------------------------------

    Thus, we can solve this "import problem"
    by adding a new module including importing statements for needed modules in subpackages.
    (We consider calling this module "all.py".)
    Note that we do not think you should add these statements in "nzmath/__init__.py"
    because these statements are executed even if you do not want (ex. import nzmath.arith1).

    We request for any comments or more good proposal.