python import MySQLdb

Posted by jgenussr on 2009-04-27 13:27

jgenussr@gmail.com

Whats up with python 2.6 and import MySQLdb
import MySQLdb
Traceback (most recent call last):
File "", line 0, in
ImportError: No module named MySQLdb

jgenus@bull:/usr/lib/python-support/python-mysqldb/python2.4$ python
ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
Python 2.6.1 (r261:67515, Dec 5 2008, 12:22:31)
[GCC 3.3.1 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import _mysqldb
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named _mysqldb
>>> import _mysql.so
__main__:1: RuntimeWarning: Python C API version mismatch for module _mysql: This Python has API version 1013, module _mysql has version 1012.
Traceback (most recent call last):
File "", line 1, in
ImportError: _mysql: init failed
>>> quit()
jgenus@bull:/usr/lib/python-support/python-mysqldb/python2.4$ cd -
/usr/lib/python-support/python-mysqldb/python2.5
jgenus@bull:/usr/lib/python-support/python-mysqldb/python2.5$ python
ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
Python 2.6.1 (r261:67515, Dec 5 2008, 12:22:31)
[GCC 3.3.1 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import _mysql.so
Traceback (most recent call last):
File "", line 1, in
ImportError: _mysql: init failed
>>>

How do I get python setup to work with MySQL?

sridharr
ActiveState Staff
Tue, 2009-04-28 11:28

jgenus@bull:/usr/lib/python-support/python-mysqldb/python2.4$ python

Note that you are trying to import system python's mysqldb from your APy interpreter. This will not work for extension modules. The correct to way to install MySQLdb in APy is:

  # download from http://sourceforge.net/projects/mysql-python
  $ tar xfz MySQL-python-1.2.2.tar.gz
  $ cd MySQL-python-1.2.2
  $ /opt/ActivePython-2.6/bin/python setup.py build
  $ sudo /opt/ActivePython-2.6/bin/python setup.py install

Prior to building, you may want to install the mysql-dev (or mysql-devel) packages.

-srid

jgenussr | Wed, 2009-05-27 05:03

Thanks,
I attempted to install the extension but can't get the correct version of setuptools
Downloading http://cheeseshop.python.org/packages/2.6/s/setuptools/setuptools-0.6c5-...
Traceback (most recent call last):
File "setup.py", line 5, in
import ez_setup; ez_setup.use_setuptools()
File "/home/jgenus/download/install/MySQL-python-1.2.2/ez_setup.py", line 83, in use_setuptools
egg = download_setuptools(version, download_base, to_dir, download_delay)
File "/home/jgenus/download/install/MySQL-python-1.2.2/ez_setup.py", line 137, in download_setuptools
src = urllib2.urlopen(url)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 389, in open
response = meth(req, response)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 502, in http_response
'http', request, response, code, msg, hdrs)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 421, in error
result = self._call_chain(*args)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
result = func(*args)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 597, in http_error_302
return self.parent.open(new)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 389, in open
response = meth(req, response)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 502, in http_response
'http', request, response, code, msg, hdrs)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 427, in error
return self._call_chain(*args)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
result = func(*args)
File "/opt/ActivePython-2.6/lib/python2.6/urllib2.py", line 510, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

I found a copy but it is version setuptools-0.6c6-py2.6.egg. I'm new at python but not linux and having a bit of difficulty, can't imagine what it must be for those not familiar with either. Afraid I don't know where to go from here.
jgenus

sridharr
ActiveState Staff
Wed, 2009-05-27 09:20

Ok, try this instead:

$ wget http://peak.telecommunity.com/dist/ez_setup.py
$ sudo /opt/ActivePython-2.6/bin/python ez_setup.py
$ sudo /opt/ActivePython-2.6/bin/easy_install MySQL-python

That last command will automatically download and install MySQL-python.

-srid

PS: We are working on PyPM (ppm for Python) a binary package manager .. which will help you install packages easily.

sridharr
ActiveState Staff
Fri, 2010-09-17 17:00

You can now type pypm install mysql-python in order to install mysqldb. Also see http://www.activestate.com/activepython/python-database-modules