Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Variations on An import Theme

There are several variations on the import statement. We looked at these briefly in the section called “The math Module”. In this section, we'll cover the variations available on the import statement.

  • Basic Import. This is covered in the section called “Module Use: The import Statement”.

  • Import As. This allows us to import a module, and assign it a new name.

  • From Module Import Names. This allows us to import a module, making some names part of the global namespace.

  • Combined From and As import.

Import As

A useful variation in the import statement is to rename a module using the as clause.

import module as name

This module renaming is used in two situations.

  • We have two or more interchangable versions of a module.

  • The module name is rather long and painful to type.

There are number of situations where we have interchangable versions of a module. One example is the built-in os module. This module gives us a number of functions that behave identically on most major operating systems. The way this is done is to create a number of variant implementations of these functions, and then use as appropriate as clause to give them a platform-neutral name.

Here's a summary of how the os module uses import as .

if 'posix' in _names:
    import posixpath as path
elif 'nt' in _names:
    import ntpath as path
elif 'mac' in _names:
    import macpath as path

After this if -statement, one of the various platform-specific modules will have been imported, and it will have the platform-independent name of os.path.

In the case of some modules, the name is rather long. For example, sqlalchemy is long and easy to misspell. It's somewhat simpler to use the following technique.

import sqlalchemy as sa
db= sa.create_engine('sqlite:///file.db')

This allows us to use sa as the module name.

From Module Import Names

Two other variations on the import statement introduce selected names from the module into the local namespace. One form picks specific names to make global.

from module import name ,...

This version of import adds a step after the module is imported. It adds the given list of names into the local namespace, making them available without using the module name as a qualifier.

For example:

from math import sin, cos, tan
print dir(math)
['__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 
'cosh', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 
'log', 'log10', 'modf', 'pi', 'pow', 'sin', 'sinh', 'sqrt', 'tan', 
'tanh']

print locals()
{'math': <module 'math' (built-in)>, '__doc__': None, 
'__version__': '1.0', 
'__file__': 'Macintosh HD:SWdev:Jack:Python:Mac:Tools:IDE:PythonIDE.py', 
'__name__': '__main__', 
'__builtins__': <module '__builtin__' (built-in)>, 
'inspect': <function inspect at 0x0d084310>, 
'sin': <built-in function sin>, 'cos': <built-in function cos>, 
'tan': <built-in function tan>}

In this example, the locals value shows that the sin, cos and tan functions are now directly part of the namespace. We can use these functions without referring to the math module. We can evaluate sin(0.7854), rather than having to say math.sin(0.7854).

This is discouraged because it tends to conceal the origin of objects.

Another variation on import makes all names in the module part of the local namespace. This import has the form:

from module import *

This makes all names from the module available in the local namespace.

Import and Rename

Finally, we can combine the from and as options to both import selected items and provide more understandable names for them.

We can say things like:

from module import name as name

In this case, we're both concealing the source of the item and it's original name. We'd best have a very good reason for this. Think of the confusion that can be caused by

from math import sqrt as sin

This must be used cautiously to prevent creating more problems than it appears to solve.


 
 
  Published under the terms of the Open Publication License Design by Interspire