Python libraries are written as modules and referenced from scripts using import or from statements. To put the code for connecting to MySQL into a function, we can write a module file Cookbook.py:
The filename basename determines the module name, so the module is called Cookbook.
Module methods are accessed through the module name, thus you would invoke the
connect( ) method of the Cookbook module like this:
conn = Cookbook.connect ( );
The Python interpreter searches for modules in directories named in the sys.path variable. Just as with Perl's @INC array, sys.path is initialized to a default set of directories. You can
find out what those directories are on your system by running Python interactively and entering a couple of commands:
If you put Cookbook.py in one of the default directories, you can reference it from a script using an import statement and Python will find it automatically:
import Cookbook
If you install Cookbook.py somewhere else, you can add the directory where it's installed to the value of sys.path. Do this by importing the sys module and invoking sys.path.insert( ). The following test harness script, harness.py, shows how to do this, assuming the Cookbook module is installed in the /usr/local/apache/lib/python directory:
Another way to tell Python where to find module files is to set the PYTHONPATH environment variable. If you set that variable to include your module directory, scripts that you run need not modify sys.path.
It's also possible to import individual symbols from a module using a from statement:
from Cookbook import connect This makes the connect( ) routine available to the script without the need for the module
name, so you'd use it like this: conn = connect ( )
# Cookbook.py - library file with utility routine for connecting to MySQL
import sys
import MySQLdb
# Establish a connection to the cookbook database, returning a connection
# object. Dies with a message if the connection cannot be established.
def connect ( ):
host_name = "localhost"
db_name = "cookbook"
user_name = "cbuser"
password = "cbpass"
try:
conn = MySQLdb.connect (db = db_name,
host = host_name,
user = user_name,
passwd = password)
return conn
except MySQLdb.Error, e:
print "Cannot connect to server"
print "Error code:", e.args[0]
print "Error message
import sys
import MySQLdb
# Establish a connection to the cookbook database, returning a connection
# object. Dies with a message if the connection cannot be established.
def connect ( ):
host_name = "localhost"
db_name = "cookbook"
user_name = "cbuser"
password = "cbpass"
try:
conn = MySQLdb.connect (db = db_name,
host = host_name,
user = user_name,
passwd = password)
return conn
except MySQLdb.Error, e:
print "Cannot connect to server"
print "Error code:", e.args[0]
print "Error message
Module methods are accessed through the module name, thus you would invoke the
connect( ) method of the Cookbook module like this:
conn = Cookbook.connect ( );
The Python interpreter searches for modules in directories named in the sys.path variable. Just as with Perl's @INC array, sys.path is initialized to a default set of directories. You can
find out what those directories are on your system by running Python interactively and entering a couple of commands:
% python
>>> import sys
>>> sys.path
>>> import sys
>>> sys.path
import Cookbook
If you install Cookbook.py somewhere else, you can add the directory where it's installed to the value of sys.path. Do this by importing the sys module and invoking sys.path.insert( ). The following test harness script, harness.py, shows how to do this, assuming the Cookbook module is installed in the /usr/local/apache/lib/python directory:
#! /usr/bin/python
# harness.py - test harness for Cookbook.py library
# Import sys module and add directory to search path
import sys
sys.path.insert (0, "/usr/local/apache/lib/python")
import MySQLdb
import Cookbook
conn = Cookbook.connect ( )
print "Connected"
conn.close ( )
print "Disconnected"
sys.exit (0)
# harness.py - test harness for Cookbook.py library
# Import sys module and add directory to search path
import sys
sys.path.insert (0, "/usr/local/apache/lib/python")
import MySQLdb
import Cookbook
conn = Cookbook.connect ( )
print "Connected"
conn.close ( )
print "Disconnected"
sys.exit (0)
It's also possible to import individual symbols from a module using a from statement:
from Cookbook import connect This makes the connect( ) routine available to the script without the need for the module
name, so you'd use it like this: conn = connect ( )