[Tutor] "from X import Y" vs "import X"
Kent Johnson
kent37 at tds.net
Wed Mar 21 13:09:19 CET 2007
emilia12 at mail.bg wrote:
> Hi list,
>
> In Python books one can read that "from X import Y" is
> better than "import X", but some times (IMO) it is not.
What book is that? I'm not aware of any general reason to prefer one of
these forms over another. "from X import *" is discouraged.
>
> for eg. in SciPy,
>
> from numpy import matrix
> from scipy.linalg import inv, det, eig
> A=matrix([[1,1,1],[4,4,3],[7,8,5]])
> print det(A)
>
> crashes with :
> RuntimeError: module compiled against version 1000002 of
> C-API but this version of numpy is 1000009
> RuntimeError: module compiled against version 1000002 of
> C-API but this version of numpy is 1000009
>
> BUT next code works fine:
>
> from numpy import matrix
> import numpy
> A=matrix([[1,1,1],[4,4,3],[7,8,5]])
> print numpy.linalg.det(A)
>
> no crashes at all!
>
> i am using: Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC
> v.1310 32 bit (Intel)] on win32
> and the last version of SciPy ...
>
> my actual question is "is this a rare case related to the
> scipy distribution, or something general in python"
In the first code you use scipy.linalg, in the second code you use
numpy.linalg. I don't think it is the form of import that matters, but
the different package. It looks like you have an incompatible version of
scipy - one compiled for a different version of Python or numpy than you
are using.
Kent
More information about the Tutor
mailing list