简体   繁体   English

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

[英]Import error: No module named _mysql

I am trying to import MySQLdb using python 2.7, while I am getting the below error. 我试图使用python 2.7导入MySQLdb,而我收到以下错误。 I am new on Ubuntu, so unable to figure out where things are going wrong. 我是Ubuntu的新手,所以无法弄清楚出了什么问题。

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

Install python-mysqldb : 安装python-mysqldb

You can intall this package in Ubuntu via this command: 您可以通过以下命令在Ubuntu此包:

apt-get install python-mysqldb

Python DB interface api "MySQLdb"installation @RHEL version 6.4 Python DB接口api“MySQLdb”安装@RHEL版本6.4

(Probably should work with other Unix Flv too). (也许应该与其他Unix Flv一起工作)。

I installed the MySQLdb Version 1.2.2 and faced the same problem ie ImportError: No module named _mysql 我安装了MySQLdb版本1.2.2并遇到了同样的问题,即ImportError:没有名为_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. 注意:以下说明确实通过安装“MySQLdb”版本1.2.5来解决调试问题,而不是解决方法。

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 注意:用户应该有权执行命令 - sudo(建议)或root用户,安装是可取的

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. 周围的工作:重命名二进制文件“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") (通常是@location,执行命令“python setup.py install”或“sudo python setup.py install”后创建“MySQL .... i686.egg”)

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. 更好的选择是卸载MySQLdb版本1.2.2或在其他情况下,即版本1.2.4并检查“MySQL .... i686.egg”文件不存在。

[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 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" 2. 下载 “MySQL-python-1.2.5.zip” 我已经在名为“download”的文件夹中下载了“MySQL-python-1.2.5.zip”

[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 ? 导入MySQLdb工作正常....最终工作?

As the error message says, you will need to install MySQLdb . 如错误消息所示,您将需要安装MySQLdb

The common way to install MySQLdb is to use pip as described here. 安装MySQLdb的常用方法是使用此处所述的pip But for Ubuntu, the recommended way is what was mentioned in the previous answer: 但对于Ubuntu,推荐的方法是前面的答案中提到的:

sudo apt-get install python-mysqldb

However, you will need first to install the necessary dependencies: 但是,您首先需要安装必要的依赖项:

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

Now you should be able to import it: >>> import MySQLdb 现在您应该能够导入它: >>> import MySQLdb

apt-get install python-mysql.connector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM