DB2 10.5 for Linux, UNIX, and Windows

Setting up the Python environment for IBM database servers

Before you can connect to an IBM® database server and run SQL statements, you must set up the Python environment by installing the ibm_db (Python) driver and, optionally, the ibm_db_sa (SQLAlchemy) or ibm_db_django (Django) adapter.

Before you begin

Ensure that the following software is installed on your system:
  • Python 2.5 or later. For Linux operating systems, you also require the python2.5-dev package.
  • The setuptools program or the distribute program. The setuptools program is available at http://pypi.python.org/pypi/setuptools, and the distribute program is available at http://pypi.python.org/pypi/distribute. You can use the setuptools program or the distribute program to download, build, install, upgrade, and uninstall Python packages.
  • If your Python application will connect to a remote IBM database, one of the following products on the computer where your application will run:
    • The IBM Data Server Client product
    • The IBM Data Server Runtime Client product
    • The IBM Data Server Driver Package product
    • The IBM Data Server Driver for ODBC and CLI product
    If your Python application connects to an IBM database server on the local computer, no additional IBM data server products are required.

Procedure

To set up the Python environment:

  1. Using one of the following two methods, install the ibm_db Python driver:
    • Install from the remote repository:
      1. Set the IBM_DB_HOME environment variable by using the export command:
        $export IBM_DB_HOME=DB2HOME
        where DB2HOME is the directory where the IBM data server product is installed.
        For example, issue the following command to set the IBM_DB_HOME environment variable:
        $ export IBM_DB_HOME=/home/db2inst1/sqllib
      2. Issue the following command:
        $ easy_install ibm_db 
    • Use the files that are included with the IBM data server products. The IBM data server client and IBM Data Server Driver Package software include the required Python files.
      1. Change the current path to the IBM data server product installation path where the Python egg files are located (IBM_DB_HOME/pythonXX).
      2. Issue the following command:
        $ easy_install ibm_db-X.X.X-pyX.X-XXXXXXXX.egg
  2. Optional: Using one of the following two methods, install the ibm_db_sa SQLAlchemy adapter or the ibm_db_django Django adapter:
    • Install from the remote repository:
      • To install the SQLAlchemy adapter, issue the following command:
        $ easy_install ibm_db_sa
      • To install the django adapter, issue the following command:
        $ easy_install ibm_db_django
    • Use the files that are included with the IBM data server products:
      1. Change the current path to the IBM data server product installation path where the Python egg files are located (IBM_DB_HOME/pythonXX).
      2. To install the SQLAlchemy adapter, issue the following command:
        $ easy_install ibm_db_sa-X.X.X-pyX.X.egg
      3. To install the django adapter, issue the following command:
        $ easy_install ibm_db_django-X.X.X-pyX.X.egg
  3. Ensure that the Python driver can access the libdb2.so CLI driver file:
    • For 32-bit Linux and UNIX operating systems other than the AIX operating system, set the LD_LIBRARY_PATH variable to the IBM_DB_HOME/lib32 directory by issuing the export command:
      export LD_LIBRARY_PATH=IBM_DB_HOME/lib32
    • For 64-bit Linux and UNIX operating systems other than the AIX operating system, set the LD_LIBRARY_PATH variable to the IBM_DB_HOME/lib64 directory by issuing the export command:
      export LD_LIBRARY_PATH=IBM_DB_HOME/lib64
    • For a 32-bit AIX operating system, set the LIBPATH variable to theIBM_DB_HOME/lib32 directory by issuing the export command:
      export LIBPATH=IBM_DB_HOME/lib32
    • For a 64-bit AIX operating system, set the LIBRARY_PATH variable to the IBM_DB_HOME/lib64 directory by issuing the export command:
      export LIBPATH=IBM_DB_HOME/lib64

What to do next

Test the ibm_db Python driver, the ibm_db_sa SQLAlchemy adapter, and the ibm_db_django Django adapter connection by using the test applications.