Python
  Home arrow Python arrow Page 4 - Python 101 (part 7): Dinner With A Hungry Giant
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Python 101 (part 7): Dinner With A Hungry Giant
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2001-08-07


    Table of Contents:
  • Python 101 (part 7): Dinner With A Hungry Giant
  • Mercury Rising
  • Between A Rock And...Another Rock
  • Love Bytes
  • Enter The Hungry Giant
  • From Python, With Love
  • Doing The Math
  • String Theory (And Other Interesting Stuff)
  • Bucking The System

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Python 101 (part 7): Dinner With A Hungry Giant - Love Bytes
    ( Page 4 of 9 )

    The "import" statement looks for module files in the directories specified in the $PYTHONPATH environment variable. If the named module isn't found in these directories, it returns an error - as the following example demonstrates:


    >>> import SQLfunctions Traceback (innermost last): File "", line 1, in ? ImportError: No module named SQLfunctions >>>


    Once you add the directory containing the module to the $PYTHONPATH variable and restart the interpreter, Python should find and import your module without any errors (you can also modify the search path via the built-in "sys" module - more on this later.)

    In case Python finds more than one module with the specified name, the first one is used.

    The first time Python imports a module, it automatically compiles the module as saves it as bytecode; this bytecode file has the same name as the module file, but ends in a .pyc extension. These .pyc files are automatically recompiled if the module changes in any way. If you take a look at your working directory after importing and using one or more modules in a Python program, you'll notice a bunch of these .pyc files scattered around - Python stores these to speed up the module the next time it's called.


    $ ls -l -rw-rw-r-- 77 Jul 23 23:04 dewey.py -rw-rw-r-- 181 Jul 23 23:05 dewey.pyc -rw-rw-r-- 98 Jul 23 23:04 huey.py -rw-rw-r-- 202 Jul 23 23:22 huey.pyc -rw-rw-r-- 244 Jul 23 22:49 temperature.py -rw-rw-r-- 435 Jul 23 22:50 temperature.pyc -rwxrwxr-x 222 Jun 8 10:50 module_test.py


    If you play with this a little, you'll see that I told a little white lie a few paragraphs back, when I said that Python could only import a module if it was located in a directory named in $PYTHONPATH. In fact, even if the module isn't located in the search path, but its corresponding bytecode is present, Python will still import and use the module.

    Consider the following example, where the working directory (which is in the search path) doesn't contain the module source, but *does* contain the compiled bytecode version - Python goes ahead and uses the bytecode version without blinking:


    $ ls -l -rw-rw-r-- 435 Jul 23 22:50 temperature.pyc -rwxrwxr-x 222 Jun 8 10:50 module_test.py

    $ python >>> import temperature >>> temperature.tempConv(95, "f") 35.0 >>>


     
     
    >>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek