mysqldb构建错误
我记得在我的32位Mac上安装了Python、Django、MySQL和MySQLdb,系统是Leopard 10.5.7。 后来我在Mac Snow Leopard上尝试了同样的安装步骤,但不幸的是遇到了很多错误…… 我不知道怎么回事,感觉有些奇怪。请看一下错误日志:
Amit-Vermas-MacBook:mysql-python-1.2.2 amitverma$ python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.3-i386-2.5
gcc-4.0 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -Dversion_info=(1,2,2,'final',0) -D__version__=1.2.2 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c _mysql.c -o build/temp.macosx-10.3-i386-2.5/_mysql.o -g -Os -arch x86_64 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL
In file included from /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/Python.h:57,
from pymemcompat.h:10,
from _mysql.c:29:
/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/pyport.h:761:2: error: #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
In file included from _mysql.c:35:
/usr/local/mysql/include/my_config.h:1050:1: warning: "HAVE_WCSCOLL" redefined
In file included from /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/Python.h:8,
from pymemcompat.h:10,
from _mysql.c:29:
/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/pyconfig.h:721:1: warning: this is the location of the previous definition
In file included from _mysql.c:35:
/usr/local/mysql/include/my_config.h:1168:1: warning: "SIZEOF_LONG" redefined
In file included from /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/Python.h:8,
from pymemcompat.h:10,
from _mysql.c:29:
/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/pyconfig.h:811:1: warning: this is the location of the previous definition
In file included from _mysql.c:35:
/usr/local/mysql/include/my_config.h:1177:1: warning: "SIZEOF_PTHREAD_T" redefined
In file included from /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/Python.h:8,
from pymemcompat.h:10,
from _mysql.c:29:
/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/pyconfig.h:820:1: warning: this is the location of the previous definition
error: command 'gcc-4.0' failed with exit status 1
Amit-Vermas-MacBook:mysql-python-1.2.2 amitverma$
4 个回答
下面这篇博客文章帮我在Mac上成功安装了MySQLdb 1.2.2:http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/
不过,后来我尝试了MySQLDB 1.2.3c1,发现直接就能顺利安装,没有遇到任何问题。1.2.2版本已经有好几年了,在Python 2.6上会出现一些过时的警告。我建议你直接换成1.2.3c1版本,看看能不能解决你的问题。
1.2.3c1是目前在PyPi上最新的版本。
这是我个人用来处理这个问题的makefile规则
MYSQLDB_VERSION=1.2.3c1
MYSQLDB_TARGET=$(BUILD_FLAGS_DIR)/mysqldb
MYSQLDB_PACKAGE=MySQL-python-$(MYSQLDB_VERSION).tar.gz
MYSQLDB_PACKAGE_URL=http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/$(MYSQLDB_VERSION)/$(MYSQLDB_PACKAGE)
.PHONY: mysqldb mysqldb-download
mysqldb: $(MYSQLDB_TARGET)
mysqldb-download: $(DOWNLOAD_DIR)/$(MYSQLDB_PACKAGE)
$(MYSQLDB_TARGET): $(INIT_TARGET) $(MYSQLDB_DEPS) $(DOWNLOAD_DIR)/$(MYSQLDB_PACKAGE)
-rm -rf $(UNPACK_DIR)/MySQL-python-$(MYSQLDB_VERSION)
tar -m -C $(UNPACK_DIR) -xzvf $(DOWNLOAD_DIR)/$(MYSQLDB_PACKAGE)
-cd $(UNPACK_DIR)/MySQL-python-$(MYSQLDB_VERSION); \
for patch in $(PATCH_DIR)/mysqldb-$(MYSQLDB_VERSION)_$(ARCH)_*; \
do patch -p1 < $$patch; \
done
cd $(UNPACK_DIR)/MySQL-python-$(MYSQLDB_VERSION); export CC="gcc -m64" FC="g95 -m64" CPPFLAGS="-I$(RUNTIME_DIR)/include" CFLAGS="-m64 -I$(RUNTIME_DIR)/include" LD_LIBRARY_PATH=$(RUNTIME_DIR)/lib64:$(RUNTIME_DIR)/lib:$$LD_LIBRARY_PATH PATH=$(RUNTIME_DIR)/bin:$$PATH PYTHONPATH=$(RUNTIME_DIR)/lib/python2.5/site-packages/; $(RUNTIME_DIR)/bin/python2.5 setup.py install --prefix=$(RUNTIME_DIR)
touch $(MYSQLDB_TARGET)
$(DOWNLOAD_DIR)/$(MYSQLDB_PACKAGE):
for package in $(MYSQLDB_PACKAGE_URL); \
do \
echo -n "Downloading $$package... "; \
cd $(DOWNLOAD_DIR); curl -L -O $$package; \
echo "done"; \
done
touch $@
ALL_RUNTIME_TARGETS+=$(MYSQLDB_TARGET)
ALL_DOWNLOAD_TARGETS+=$(DOWNLOAD_DIR)/$(MYSQLDB_PACKAGE)
还有一个补丁
$ more mysqldb-1.2.3c1_x86_64-apple-darwin10_patch-000
diff -Naur MySQL-python-1.2.3c1/setup.py MySQL-python-1.2.3c1.new/setup.py
--- MySQL-python-1.2.3c1/setup.py 2008-10-18 02:12:31.000000000 +0200
+++ MySQL-python-1.2.3c1.new/setup.py 2009-10-08 22:59:05.000000000 +0200
@@ -13,6 +13,8 @@
from setup_windows import get_config
metadata, options = get_config()
+options["extra_compile_args"].remove("-arch")
+options["extra_compile_args"].remove("x86_64")
metadata['ext_modules'] = [Extension(sources=['_mysql.c'], **options)]
metadata['long_description'] = metadata['long_description'].replace(r'\n', '')
setup(**metadata)
这个方法对我有效。我不能保证它一定适合你,但...也许你能在里面找到一些有趣的线索。
请注意,我使用的是一个自定义编译器(因为一些过时的原因,不想深入讨论)
最可能的原因是你在尝试把64位的MySQL库和仅支持32位的Python版本连接在一起(目前,python.org提供的OS X安装包都是32位的)。你可以通过在/usr/local/mysql/
目录下对库文件使用file
命令来确认这一点。
这里有一些解决方案:
使用苹果自带的
python2.6
,它是64位的,适用于Snow Leopard。安装一个32位版本的MySQL库。
使用
MacPorts
安装一个完整的解决方案:先安装MacPorts的基础设施,然后安装python 2.6的MySQLdb适配器(或者2.5),这将会安装所有必要的依赖,包括一个新的Python和MySQL客户端库,它们应该能正常工作(并且可以通过MacPorts
进行更新):sudo port install py26-mysql # 或者 py25-mysql
如果你想在OS X
上使用MySQL
和Python,我推荐最后一个解决方案,除非你真的喜欢并有时间去管理和安装软件包。这样做在长远来看会省去你很多麻烦。
附注:MacPorts还包括django
和PIL
的版本:
sudo port install py26-django py26-pil
编辑:
如果选择MacPorts
的方式,请按照我在这里提供的说明,去除python.org安装的Python的影响。不要尝试删除或修改在/usr/bin
或/System/Library
中的苹果自带的Python文件;它们是OS X
的一部分。然后按照上面的说明安装MacPorts
。为了避免与苹果或第三方的安装冲突,MacPorts
会将所有文件安装到一个完全独立的目录结构中,根目录是/opt/local
。因此,你需要修改你的.bash_profile
,将/opt/local/bin
添加到你的$PATH
中。如果你希望MacPorts
的版本优先被找到,可以添加类似以下内容:
export PATH="/opt/local/bin:${PATH}"
当你启动一个新的终端会话时,你应该能找到MacPorts
的python2.6,命令是python2.6
。如果你还想让python
命令指向那里:
$ sudo port install python_select
$ sudo python_select -l
Available versions:
current none python26 python26-apple
$ sudo python_select python26