MySQL Connector/Python Developer Guide  /  The Connector/Python C Extension  /  Application Development with the Connector/Python C Extension

8.1 Application Development with the Connector/Python C Extension

Installations of Connector/Python from version 2.1.1 on support a use_pure argument to connect() that indicates whether to use the pure Python interface to MySQL or the C Extension that uses the MySQL C client library:

  • By default, use_pure is True and the C Extension is not used even for Connector/Python installations that include it.

  • For Connector/Python installations that include the C Extension, enable it by passing a use_pure=False argument to connect().

  • For Connector/Python installations that do not include the C Extension, passing use_pure=False to connect() raises an exception.

  • For older Connector/Python installations that know nothing of the C Extension (before version 2.1.1), passing use_pure to connect() raises an exception regardless of its value.

Note

On OS X, if your Connector/Python installation includes the C Extension, but Python scripts are unable to use it, try setting your DYLD_LIBRARY_PATH environment variable the directory containing the C client library. For example:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib   (for sh)
setenv DYLD_LIBRARY_PATH /usr/local/mysql/lib   (for tcsh)

If you built the C Extension from source, this directory should be the one containing the C client library against which the extension was built.

If you need to check whether your Connector/Python installation is aware of the C Extension, test the HAVE_CEXT value. There are different approaches for this. Suppose that your usual arguments for connect() are specified in a dictionary:

config = {
  'user': 'scott',
  'password': 'tiger',
  'host': '127.0.0.1',
  'database': 'employees',
}

The following example illustrates one way to add use_pure to the connection arguments:

import mysql.connector

if mysql.connector.__version_info__ > (2, 1) and mysql.connector.HAVE_CEXT:
  config['use_pure'] = False

Alternatively, add use_pure to the configuration arguments as follows:

try:
  have_cext = mysql.connector.HAVE_CEXT
except AttributeError:
  have_cext = False

if have_cext:
  config['use_pure'] = False

User Comments
Sign Up Login You must be logged in to post a comment.