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. You also have the option of downloading 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 3.0 or later. For Linux® operating systems, you also require the python3.0-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 is going to connect to a remote IBM database, one of the following products on the computer where your application is going to 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 needed.

Procedure

To set up the Python environment:

  1. Using the following method, install the ibm_db Python driver:
    • Install from the remote repository:
      1. If you want to avoid automatic installation of the clidriver and would like to use an existing copy of the driver, you can set IBM_DB_HOME.

        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/<dsdriver installation>/clidriver
      2. Issue the following command:
        $ pip install ibm_db

        Or

        $ easy_install ibm_db
  2. Optional: Using the following method, install the ibm_db_sa SQLAlchemy adapter or ibm_db_django Django adapter:
    • Install from the remote repository:
      • To install the SQLAlchemy adapter, issue the following command:
        $ pip install ibm_db_sa

        Or

        $ easy_install ibm_db_sa
      • To install the django adapter, issue the following command:
        $ pip install ibm_db_django

        Or

        $ easy_install ibm_db_django
  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 the IBM_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.