3

I have installed mysql server using

$ sudo apt-get install mysql-server

then I installed python-mysqldb using

$ sudo apt-get install python-mysqldb

but when I import MySQLdb in python it shows the following error

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb

I reinstalled both mysql server and python-mysqldb but it shows the same error. What am I getting wrong?

1

7 Answers 7

8

You needed to install the module with pip pip install mysql-python

Sign up to request clarification or add additional context in comments.

1 Comment

If these packages python-dev and libmysqlclient-dev are not installed, you cannot install mysql-python using pip.
3

Python module names are case sensitive, even on case-insensitive file systems. Therefore "MySQLdb" and "mysqldb" are two different modules/packages. Use import MySQLdb instead.

Comments

2

The package name is "MySQLdb". Case counts.

3 Comments

That's nice. Which is your system Python?
i am using Ubuntu 10.10 and using default python which comes with the os.
i have updated my python to Python 3.1.2. but the result is same.
2

Your code snippet is fine. So the problem lies with getting libraries to match up. Here are some tools to help track it down... but do post back to let us know how this worked out for you:

$ python --version
Python 2.6.6
$ easy_install yolk
$ yolk --list
DecoratorTools  - 1.7          - active
MySQL-python    - 1.2.2        - active
...

$ pydoc -p 8080
pydoc server ready at http://localhost:8080/

$ python
>>> import MySQLdb
>>> help('modules')
Please wait a moment while I gather a list of all available modules...    
ArgImagePlugin      _bytesio            exceptions          pyexpat
BaseHTTPServer      MspImagePlugin      cProfile            multifile
MySQLdb  ....


$ dpkg -L python-mysqldb
/usr/lib/pyshared/python2.7/_mysql.so
/usr/lib/pyshared/python2.6/_mysql.so

Note the Ubuntu package installs mysql to several python versions.

Comments

2

Try these commands

sudo apt-get install python-dev libmysqlclient-dev

sudo pip install MySQL-python

Comments

2

"mysql-python" currently does not support python3. There are currently a few options for using Python 3 with mysql. I installed mysqlclient and can import MySQLdb successfully.

$ pip install mysqlclient

$ python
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> 

Details refer to: https://stackoverflow.com/a/25724855/966660

Comments

1

Why not try installing from the synaptic package manager instead. You could install python-mysqldb-dbg also.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.