Question about import

Alex Martelli aleax at aleax.it
Tue Aug 5 14:27:31 EDT 2003


Mark Daley wrote:

> The difference is how you access the methods in the module.  If you use:
> 
> import Module
> 
> you will only be able to use the Module.Method usage.  If you use:
> 
> from Module import *
> 
> you can access the Methods directly.
> 
> 
> The drawback to using from Module import * comes from conflicts if you
> import multiple Modules, which may have identically named Methods, where
> you will only be able to get the Method from the most recently imported
> Module. Better to import the Module, IMO.
> 
> Any thoughts, all?

There are also other drawbacks to using the 'from' statement rather
than the 'import' statement.  For example, if you ever reload() the
module, this will have no effect in other modules that used 'from',
while it will cause usage of the updated (reloaded) module in other
modules that used 'import'.  Personally, I think a beginner would
be best served by totally forgetting the existence of 'from' and
using only 'import' instead -- perhaps 'import ... as' if the need
to abbreviate the name used to refer to the module is strongly felt.


Alex





More information about the Python-list mailing list