On Wednesday November 7, 2007, Dinesh B Vadhia wrote: > Hello! The standard Python practice for importing modules is, for example: > > import sys > import os > etc. > > In NumPy (and SciPy) the 'book' suggests using: > > from numpy import * > from scipy import * > > However, when I instead use 'import numpy' it causes all sorts of errors > in my existing code.
The issue is the following. The numpy module includes many definitions, for example a class named array. When you use the syntax, from numpy import * That takes all definitions from the module and places them into your current namespace. At this point, it would be fine to use a command such as values = array([1.0, 2.0, 3.0]) which instantiates a (numpy) array. If you instead use the syntax import numpy things brings that module as a whole into your namespace, but to access definitions from that module you have to give a qualified name, for example as values = numpy.array([1.0, 2.0, 3.0]) You cannot simply use the word array as in the first scenario. This would explain why your existing code would no longer work with the change. > What do you suggest? The advantage of the "from numpy import *" syntax is mostly convenience. However, the better style is "import numpy" precisely becuase it does not automatically introduce many other definitions into your current namespace. If you were using some other package that also defined an "array" and then you were to use the "from numpy import *", the new definition would override the other definition. The use of qualified names helps to avoid these collisions and makes clear where those definitions are coming from. With regard, Michael _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor