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.

Furthermore, these imports imply that dir1 resides within some container directorydir0, which is accessible on the <strong>Python</strong> module search path. In other words, the twoimport statements imply a directory structure that looks something like this (shownwith DOS backslash separators):dir0\dir1\dir2\mod.py# Or mod.pyc, mod.so, etc.The container directory dir0 needs to be added to your module search path (unlessit’s the home directory of the top-level file), exactly as if dir1 were a module file.From there down, the import statements in your script give the directory paths leadingto the modules explicitly.Packages and Search Path SettingsIf you use this feature, keep in mind that the directory paths in your importstatements can only be variables separated by periods. You cannot use any platformspecificpath syntax in your import statements, such as C:\dir1, My Documents.dir2,or ../dir1—these do not work syntactically. Instead, use platform-specific syntax inyour module search path settings to name the container directories.For instance, in the prior example, dir0—the directory name you add to your modulesearch path—can be an arbitrarily long and platform-specific directory path leadingup to dir1. Instead of using an invalid statement like this:import C:\mycode\dir1\dir2\mod# Error: illegal syntaxadd C:\mycode to your PYTHONPATH variable or a .pth file (assuming it is not the program’shome directory, in which case this step is not necessary), and say this:import dir1.dir2.modIn effect, entries on the module search path provide platform-specific directory pathprefixes, which lead to the leftmost names in import statements. Import statementsprovide directory path tails in a platform-neutral fashion. *Package _ _init_ _.py FilesIf you choose to use package imports, there is one more constraint you must follow:each directory named within the path of a package import statement must contain afile named _ _init_ _.py, or your package imports will fail. That is, in the examplewe’ve been using, both dir1 and dir2 must contain a file called _ _init_ _.py; the containerdirectory dir0 does not require such a file because it’s not listed in the importstatement itself. More formally, for a directory structure such as this:* The dot path syntax was chosen partly for platform neutrality, but also because paths in import statementsbecome real nested object paths. This syntax also means that you get odd error messages if you forget to omitthe .py in your import statements. For example, import mod.py is assumed to be a directory path import—itloads mod.py, then tries to load a mod\py.py, and ultimately issues a potentially confusing error message.416 | Chapter 20: Module Packages

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

Saved successfully!

Ooh no, something went wrong!