在Centos上安装mysql-python

问题描述 投票:0回答:6

我正在尝试在 centos 5.5 上安装 MySQL-python 库。 我跑了

sudo yum install MySQL-python

但是当我尝试时:

import MySQLdb

我收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "MySQLdb/__init__.py", line 22, in ?
    raise ImportError("this is MySQLdb version %s, but _mysql is version %r" %
ImportError: this is MySQLdb version (1, 2, 3, 'final', 0), \ # added linebreak
but _mysql is version (1, 2, 1, 'final', 1)

有关如何解决此问题的任何线索?

python mysql
6个回答
41
投票

第 1 步 - 安装软件包

# yum install MySQL-python
Loaded plugins: auto-update-debuginfo, langpacks, presto, refresh-packagekit
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package MySQL-python.i686 0:1.2.3-3.fc15 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package              Arch         Version                 Repository      Size
================================================================================
Installing:
 MySQL-python         i686         1.2.3-3.fc15            fedora          78 k

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 78 k
Installed size: 220 k
Is this ok [y/N]: y
Downloading Packages:
Setting up and reading Presto delta metadata
Processing delta metadata
Package(s) data still to download: 78 k
MySQL-python-1.2.3-3.fc15.i686.rpm                       |  78 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : MySQL-python-1.2.3-3.fc15.i686                               1/1 

Installed:
  MySQL-python.i686 0:1.2.3-3.fc15                                              

Complete!

第 2 步 - 测试工作

import MySQLdb
db = MySQLdb.connect("localhost","myusername","mypassword","mydb" )
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()    
print "Database version : %s " % data    
db.close()

输出:

Database version : 5.5.20 

11
投票

我有 Python 2.7.5、MySQL 5.6 和 CentOS 7.1.1503

对我来说,它可以使用以下命令:

# pip install mysql-python

请注意此处的先决条件:

安装Python pip:

# rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

# yum -y update
Reboot the machine (if kernel is also updated)

# yum -y install python-pip

安装Python开发包:

# yum install python-devel

安装MySQL开发包:

# yum install mysql-devel

7
投票

对于centos7我需要:


sudo yum install mysql-devel gcc python-pip python-devel
sudo pip install mysql-python

所以,

gcc
mysql-devel
(而不是
mysql
)很重要


3
投票

您可能没有通过 yum 安装 MySQL?存储库中的 MySQLDB 版本与存储库中的 MySQL 版本相关联。版本需要匹配。

您的选择是:

  1. 安装 RPM 版本的 MySQL。
  2. 将 MySQLDB 编译为您的版本 MySQL。

1
投票

mysql-python
支持Python3,您可能需要:

sudo pip3 install mysqlclient

另外,请查看这篇文章以获取更多替代方案。


0
投票

它可以在我的 CentOS 7 上运行:

yum install MySQL-python
© www.soinside.com 2019 - 2024. All rights reserved.