'as' keyword - when/how to use it

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Jul 24 06:44:40 EDT 2010


On Fri, 23 Jul 2010 23:33:55 -0700, Dummey wrote:

> I am having the hardest time trying to find documentation on proper use
> of the 'as' keyword (aside from . I initially thought that I would be
> allowed to do something such as:
> 
> import shared.util as util
> 
> The as statement seems to be causing a lot of ''module' object has no
> attribute'. Is it safe to assume that this is not the way to use it?

It works for me.

>>> import math as spam
>>> spam
<module 'math' from '/usr/lib/python2.5/lib-dynload/mathmodule.so'>
>>> import email.mime.text as ham
>>> ham
<module 'email.mime.text' from '/usr/lib/python2.5/email/mime/text.pyc'>


My guess is that you are trying to import individual objects from a 
module using the package dot notation:

>>> import math.sin as eggs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named sin



-- 
Steven



More information about the Python-list mailing list