0

I followed the procedure and installed MySQL for python. When i try to import MySQLdb in python it gives an error.

import MySQLdb Traceback (most recent call last): File "", line 1, in File "MySQLdb/init.py", line 19, in import _mysql ImportError: No module named _mysql
spajce
  • 7,044
  • 5
  • 29
  • 44
kush
  • 31
  • 1
  • 1
  • 2

2 Answers2

0

set the LD_LIBRARY_PATH environment variable so that it includes the path to the MySQL libraries.

So in your .bash_profile (.bashrc, .profile depends which one you are using) add a line like this one :

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

(/usr/local/mysql/lib is the path to my MySQL libraries and may differ in your system)

thikonom
  • 4,219
  • 3
  • 26
  • 30
  • The issue is not (yet) MySQL client libraries -- it is the Python module itself. – Ben Jan 22 '13 at 19:58
  • i did as u said. now getting the following msg- > import MySQLdb Traceback (most recent call last): File "", line 1, in ImportError: No module named MySQLdb – kush Jan 22 '13 at 20:20
  • You probably need to reinstall python-mysql now. There are a number of ways, this answer names a few http://stackoverflow.com/a/5873259/183667. – thikonom Jan 22 '13 at 20:30
0

Use pip for package management and import and then run:

pip install MySQL-python

in your code you should then import the library at the top using:

import _mysql
db=_mysql.connect(host="localhost",user="joebob",
              passwd="moonpie",db="thangs")

See https://github.com/farcepest/MySQLdb1/blob/master/doc/user_guide.rst for more details on the package use.

See http://www.pip-installer.org/en/latest/ for more details on pip.

This is also a great read on best practices for package/environment management https://python-guide.readthedocs.org/en/latest/

Matt Alcock
  • 12,399
  • 14
  • 45
  • 61