导入错误:没有名为_mysql的模块

时间:2016-04-02 12:05:30

标签: python mysql

我正在尝试使用python 2.7导入MySQLdb,而我收到以下错误。我是Ubuntu的新手,因此无法弄清楚出了什么问题。

错误消息:

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

你能帮帮我吗?

我使用以下命令安装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

4 个答案:

答案 0 :(得分:6)

安装python-mysqldb

您可以通过此命令在Ubuntu中输入此包:

apt-get install python-mysqldb

答案 1 :(得分:2)

Python数据库接口api&#34; MySQLdb&#34;安装@RHEL版本6.4

(可能也应该与其他Unix Flv合作)。

我安装了MySQLdb版本1.2.2并遇到了同样的问题 即ImportError:没有名为_mysql的模块

注意:以下说明确实说明了调试问题,而是通过安装&#34; MySQLdb&#34;来解决问题。版本1.2.5,工作正常。

遵循的步骤:

[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 

注意:用户应该有权执行命令 - sudo(建议)或者root用户,安装是可取的

后期剧集: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

周围的工作: 重命名二进制文件&#34; MySQL_python-1.2.2-py2.6-linux-i686.egg&#34; @location:/usr/lib/python2.6/site-packages。 (通常是@location,其中&#34; MySQL .... i686.egg&#34;是在执行命令&#34; python setup.py install&#34;或&#34; sudo python setup.py install& #34)

更好的选择是卸载MySQLdb版本1.2.2或其他情况即版本1.2.4并检查&#34; MySQL .... i686.egg&#34;文件不存在。

[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

下一步:

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

2.Download&#34; MySQL-python-1.2.5.zip&#34; 我已下载&#34; MySQL-python-1.2.5.zip&#34;在名为&#34的文件夹中;下载&#34;

[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']

导入MySQLdb工作正常....最终工作?

答案 2 :(得分:0)

如错误消息所示,您需要安装MySQLdb

安装MySQLdb的常用方法是使用此处所述的pip。但对于Ubuntu,推荐的方法是前面的答案中提到的:

sudo apt-get install python-mysqldb

但是,您需要先安装必要的依赖项:

sudo apt-get install build-essential python-dev libmysqlclient-dev

现在您应该可以导入它:>>> import MySQLdb

答案 3 :(得分:0)

apt-get install python-mysql.connector