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.

Also, notice in the install hierarchy shown earlier that _ _init_ _.py files were addedto the system1 and system2 directories to make this work, but not to the root directory.Only directories listed within import statements in your code require these files;as you’ll recall, they are run automatically the first time the <strong>Python</strong> process importsthrough a package directory.Technically, in this case, the system3 directory doesn’t have to be under root—justthe packages of code from which you will import. However, because you never knowwhen your own modules might be useful in other programs, you might as well placethem under the common root directory to avoid similar name-collision problems inthe future.Finally, notice that both of the two original systems’ imports will keep workingunchanged. Because their home directories are searched first, the addition of thecommon root on the search path is irrelevant to code in system1 and system2; theycan keep saying just import utilities, and expect to find their own files. Moreover,if you’re careful to unpack all your <strong>Python</strong> systems under a common root like this,path configuration becomes simple: you’ll only need to add the common root directory,once.Why You Will Care: Module PackagesNow that packages are a standard part of <strong>Python</strong>, it’s common to see larger third-partyextensions shipped as a set of package directories, rather than a flat list of modules.The win32all Windows extensions package for <strong>Python</strong>, for instance, was one of thefirst to jump on the package bandwagon. Many of its utility modules reside in packagesimported with paths. For instance, to load client-side COM tools, you use a statementlike this:from win32com.client import constants, DispatchThis line fetches names from the client module of the win32com package (an installsubdirectory).Package imports are also pervasive in code run under the Jython Java-based implementationof <strong>Python</strong> because Java libraries are organized into hierarchies as well. In recent<strong>Python</strong> releases, the email and XML tools are likewise organized into packaged subdirectoriesin the standard library. Whether you create package directories, you willprobably import from them eventually.Why Use Package Imports? | 423

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

Saved successfully!

Ooh no, something went wrong!