在macOS Mavericks系统上安装MySQL-python时,用户使用pip命令遇到以下错误:
Downloading/unpacking mysql-python
Downloading MySQL-python-1.2.4.zip (113kB): 113kB downloaded
Running setup.py egg_info for package mysql-python
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz
Extracting in /var/folders/wl/wh6bdcxx2psbvyncp8y8br340000gn/T/tmpXUZzMR
Now working in /var/folders/wl/wh6bdcxx2psbvyncp8y8br340000gn/T/tmpXUZzMR/distribute-0.6.28
Building a Distribute egg in /private/var/folders/wl/wh6bdcxx2psbvyncp8y8br340000gn/T/pip_build_fredericmaurer/mysql-python
/private/var/folders/wl/wh6bdcxx2psbvyncp8y8br340000gn/T/pip_build_fredericmaurer/mysql-python/distribute-0.6.28-py2.7.egg
Installing collected packages: mysql-python
Running setup.py install for mysql-python
building '_mysql' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -O2 -DNDEBUG -g -O3 -Dversion_info=(1,2,4,'final',1) -D__version__=1.2.4 -I/usr/local/Cellar/mysql/5.6.14/include/mysql -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.6-intel-2.7/_mysql.o -Os -g -fno-strict-aliasing
In file included from _mysql.c:44:
/usr/local/Cellar/mysql/5.6.14/include/mysql/my_config.h:348:11: warning: 'SIZEOF_SIZE_T' macro redefined
#define SIZEOF_SIZE_T SIZEOF_LONG
^
/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pymacconfig.h:56:17: note: previous definition is here
# define SIZEOF_SIZE_T 4
^
In file included from _mysql.c:44:
/usr/local/Cellar/mysql/5.6.14/include/mysql/my_config.h:442:9: warning: 'HAVE_WCSCOLL' macro redefined
#define HAVE_WCSCOLL
^
/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pyconfig.h:902:9: note: previous definition is here
#define HAVE_WCSCOLL 1
^
In file included from _mysql.c:44:
/usr/local/Cellar/mysql/5.6.14/include/mysql/my_config.h:659:9: warning: 'SIZEOF_TIME_T' macro redefined
#define SIZEOF_TIME_T 8
^
/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pymacconfig.h:57:17: note: previous definition is here
# define SIZEOF_TIME_T 4
^
_mysql.c:1567:10: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (how < 0 || how >= sizeof(row_converters)) {
~~~ ^ ~
4 warnings generated.
In file included from _mysql.c:44:
/usr/local/Cellar/mysql/5.6.14/include/mysql/my_config.h:348:11: warning: 'SIZEOF_SIZE_T' macro redefined
#define SIZEOF_SIZE_T SIZEOF_LONG
^
/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pymacconfig.h:43:17: note: previous definition is here
# define SIZEOF_SIZE_T 8
^
In file included from _mysql.c:44:
/usr/local/Cellar/mysql/5.6.14/include/mysql/my_config.h:442:9: warning: 'HAVE_WCSCOLL' macro redefined
#define HAVE_WCSCOLL
^
/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pyconfig.h:902:9: note: previous definition is here
#define HAVE_WCSCOLL 1
^
_mysql.c:1567:10: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (how < 0 || how >= sizeof(row_converters)) {
~~~ ^ ~
3 warnings generated.
I get the same error message when i try to install it manually.
Has anyone any idea?
I searched with google but couldn't find any solution.
I appreciate your help.
解决方案:
- 检查MySQL版本
该错误是由于MySQL-python 1.2.4版本只支持MySQL 5.5版本以下。因此,需要确保已安装兼容版本的MySQL。
- 安装兼容版本的MySQL
可以从MySQL官方网站下载并安装兼容版本的MySQL,比如MySQL 5.5。
- 重新安装MySQL-python
安装好兼容版本的MySQL后,重新使用pip命令安装MySQL-python:
pip install mysql-python
- 安装MySQL-python并指定MySQL版本
如果仍然遇到错误,可以通过指定MySQL版本来安装MySQL-python:
pip install mysql-python==1.2.4 --global-option=build_ext \
--global-option="-I/usr/local/mysql/5.5/include" \
--global-option="-L/usr/local/mysql/5.5/lib" \
--global-option="-lmysqlclient"
- 使用MySQLdb
如果仍然无法安装MySQL-python,可以尝试使用MySQLdb,它是MySQL-python的替代品,它与MySQL 5.6版本兼容。
代码例子:
import mysql.connector
config = {
'user': 'user1',
'password': 'user1',
'host': 'localhost',
'database': 'mydb'
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
cursor.execute("SELECT * FROM employees")
for row in cursor:
print(row)
cursor.close()
cnx.close()