Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import error: No module named _mysql

Tags:

python

mysql

I am trying to import MySQLdb using python 2.7, while I am getting the below error. I am new on Ubuntu, so unable to figure out where things are going wrong.

error message :

import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 19, in <module>
ImportError: No module named _mysql

Could you please help me out ?

i have used below commands to install python mysqldb

$ gunzip MySQL-python-1.2.4b4.tar.gz
$ tar -xvf MMySQL-python-1.2.4b4.tar
$ cd MySQL-python-1.2.4b4
$ python setup.py build
$ python setup.py install
like image 428
Sourav Avatar asked Apr 02 '16 12:04

Sourav


People also ask

How to Fix error of No module named mysql?

The Python "ModuleNotFoundError: No module named 'mysql'" occurs when we forget to install the mysql-connector-python module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install mysql-connector-python command.


2 Answers

Install python-mysqldb:

You can intall this package in Ubuntu via this command:

apt-get install python-mysqldb
like image 149
Hasan Ramezani Avatar answered Oct 23 '22 09:10

Hasan Ramezani


Python DB interface api "MySQLdb"installation @RHEL version 6.4

(Probably should work with other Unix Flv too).

I installed the MySQLdb Version 1.2.2 and faced the same problem i.e ImportError: No module named _mysql

Note : Following explanation does tells anything about debugging the problem rather workaround by installing the "MySQLdb" version 1.2.5 which is working absolutely fine.

steps followed :

[user@hostname]$:wget http://downloads.sourceforge.net/mysql-python/MySQL-[user@hostname]$:python-1.2.2.tar.gz
[user@hostname]$:tar -xvf MySQL-python-1.2.2.tar
[user@hostname]$:cd MySQL-python-1.2.2
[user@hostname]$:python setup.py build
[user@hostname]$:python setup.py install 

Note:user should have privilege to execute the command - sudo (advised) or else root user and installation was suceesful

Later episode : ERROR :-

[user@hostname]$: python 

Python 2.7.9 (default, May 23 2015, 09:01:18) 

[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import MySQLdb
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: No module named _mysql

WORK AROUND : Renamed the Binary file "MySQL_python-1.2.2-py2.6-linux-i686.egg" @location : /usr/lib/python2.6/site-packages. (Generally @location where the "MySQL....i686.egg" is created after executing the command "python setup.py install" or "sudo python setup.py install")

Better option is to uninstall MySQLdb version 1.2.2 or in other cases namely version 1.2.4 and check "MySQL....i686.egg" file does not exist.

[user@hostname site-packages]$ sudo mv MySQL_python-1.2.2-py2.6-linux-i686.egg 123MySQL_python-1.2.2-py2.6-linux-i686.123egg

WHATS NEXT :

1.go to https://pypi.python.org/pypi/MySQL-python/1.2.5

2.Download "MySQL-python-1.2.5.zip" i have downloaded "MySQL-python-1.2.5.zip" in the folder named "download"

[user@hostname]$:cd download 
[user@hostname]$:unzip MySQL-python-1.2.5.zip
[user@hostname]$:cd MySQL-python-1.2.5
[user@hostname]$:ls
doc      HISTORY  MANIFEST.in   _mysql.c  _mysql_exceptions.py   PKG-INFO       README.md  setup_common.py  setup.py          site.cfg
GPL-2.0  INSTALL  metadata.cfg  MySQLdb   MySQL_python.egg-info  pymemcompat.h  setup.cfg  setup_posix.py   setup_windows.py  tests
[user@hostnaem]$:sudo python setup.py install 
    running install
    ...............
.......................
    Installed /usr/lib/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-i686.egg
    Processing dependencies for MySQL-python==1.2.5
    Finished processing dependencies for MySQL-python==1.2.5
[user@hostname]$:python  
    Python 2.7.9 (default, May 23 2015, 09:01:18) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import MySQLdb
    >>> import MySQLdb
    >>> dir(MySQLdb)
['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'DATETIME', 'DBAPISet', 'DataError', 'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'FIELD_TYPE', 'IntegrityError', 'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError', 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Time', 'TimeFromTicks', 'Timestamp', 'TimestampFromTicks', 'Warning', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__revision__', '__version__', '_mysql', 'apilevel', 'connect', 'connection', 'constants', 'debug', 'escape', 'escape_dict', 'escape_sequence', 'escape_string', 'get_client_info', 'paramstyle', 'release', 'result', 'server_end', 'server_init', 'string_literal', 'test_DBAPISet_set_equality', 'test_DBAPISet_set_equality_membership', 'test_DBAPISet_set_inequality', 'test_DBAPISet_set_inequality_membership', 'thread_safe', 'threadsafety', 'times', 'version_info']

Import MySQLdb is working fine....FINALLY WORKING ?

like image 40
Don_Manj Avatar answered Oct 23 '22 09:10

Don_Manj