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.

Because this last import step actually runs the file’s code, if any top-level code in amodule file does real work, you’ll see its results at import time. For example, toplevelprint statements in a module show output when the file is imported. Functiondef statements simply define objects for later use.As you can see, import operations involve quite a bit of work—they search for files,possibly run a compiler, and run <strong>Python</strong> code. Because of this, any given module isimported only once per process by default. Future imports skip all three import stepsand reuse the already loaded module in memory. * If you need to import a file againafter it has already been loaded (for example, to support end-user customization),you have to force the issue with a reload call—a tool we’ll meet in the next chapter.Third-Party Software: distutilsThis chapter’s description of module search path settings is targeted mainly at userdefinedsource code that you write on your own. Third-party extensions for <strong>Python</strong>typically use the distutils tools in the standard library to automatically install themselves,so no path configuration is required to use their code.Systems that use distutils generally come with a setup.py script, which is run to installthem; this script imports and uses distutils modules to place such systems in adirectory that is automatically part of the module search path (usually in the Lib\sitepackagessubdirectory of the <strong>Python</strong> install tree, wherever that resides on the targetmachine).For more details on distributing and installing with distutils, see the <strong>Python</strong> standardmanual set; its use is beyond the scope of this book (for instance, it also provides waysto automatically compile C-coded extensions on the target machine). Also, see theemerging third-party open source eggs system, which adds dependency checking forinstalled <strong>Python</strong> software.Chapter SummaryIn this chapter, we covered the basics of modules, attributes, and imports, andexplored the operation of import statements. We learned that imports find the designatedfile on the module search path, compile it to byte code, and execute all of itsstatements to generate its contents. We also learned how to configure the searchpath to be able to import from other directories than the home directory and thestandard library directories, primarily with PYTHONPATH settings.* Technically, <strong>Python</strong> keeps already loaded modules in the built-in sys.modules dictionary, which it checks atthe start of an import operation to determine whether the referenced module is already loaded. If you wantto see which modules are loaded, import sys and print sys.modules.keys( ). More on this internal table inChapter 21.Chapter Summary | 395

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

Saved successfully!

Ooh no, something went wrong!