12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

This defines a package named mypkg containing modules named mypkg.main andmypkg.string. Now, suppose that the main module tries to import a module namedstring. In <strong>Python</strong> 2.4 and earlier, <strong>Python</strong> will first look in the mypkg directory toperform a relative import. It will find and import the string.py file located there,assigning it to the name string in the mypkg.main module’s namespace.It could be, though, that the intent of this import was to load the <strong>Python</strong> standardlibrary’s string module instead. Unfortunately, in these versions of <strong>Python</strong>, there’sno straightforward way to ignore mypkg.string and look for the standard library’sstring module located further to the right on the module search path. We cannotdepend on any extra package directory structure above the standard library beingpresent on every machine.In other words, imports in packages can be ambiguous—within a package, it’s notclear whether an import spam statement refers to a module within or outside thepackage. More accurately, a local module or package can hide another hangingdirectly off of sys.path, whether intentionally or not.In practice, <strong>Python</strong> users can avoid reusing the names of standard library modulesthey need for modules of their own (if you need the standard string, don’t name anew module string). But this doesn’t help if a package accidentally hides a standardmodule; moreover, <strong>Python</strong> might add a new standard library module in the futurethat has the same name as a module of your own. Code that relies on relativeimports is also less easy to understand because the reader may be confused aboutwhich module is intended to be used. It’s better if the resolution can be made explicitin code.In <strong>Python</strong> 2.5, we can control the behavior of imports, forcing them to be absoluteby using the from _ _future_ _ import directive listed earlier. Again, bear in mind thatthis absolute-import behavior will become the default in a future version (plannedcurrently for <strong>Python</strong> 2.7). When absolute imports are enabled, a statement of the followingform in our example file mypkg/main.py will always find the standardlibrary’s version of string, via an absolute import search:import string# Imports standard lib stringYou should get used to using absolute imports now, so you’re prepared when thechange takes effect. That is, if you really want to import a module from your package,to make this explicit and absolute, you should begin writing statements like thisin your code (mypkg will be found in an absolute directory on sys.path):from mypkg import string# Imports mypkg.string (absolute)Relative imports are still possible by using the dot pattern in the from statement:from . import string # Imports mypkg.string (relative)Relative Import Syntax | 433

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!