我已经尝试了好几个星期,在 Mac OS X 10.6 上安装 MySQL-python,但完全没有成功,因为使用的是 Enthought Python Distribution 中的 Python 7.0。我遇到的许多问题都已在其他帖子中描述过,我确信我已经阅读了所有这些帖子,但仍然无法解决这个问题。我尝试安装并重新安装适用于 64 位 OS X 的各种 MySQL 包,但最终只是从头开始编译它。据我所知,MySQL 运行并位于目录 /usr/local/mysql-5.5.9-osx10.6-x86/ 中。
我能够使用“sudo setup.py install”安装 MySQL-python-1.2.3。但是,当我导入它时,我收到错误:
$ python
Enthought Python Distribution -- www.enthought.com
Version: 7.0-2 (64-bit)
Python 2.7.1 |EPD 7.0-2 (64-bit)| (r271:86832, Dec 3 2010, 15:56:20)
[GCC 4.0.1 (Apple Inc. build 5488)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 7, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so
Reason: image not found
正如这里的其他海报所建议的那样,我查看了 _mysql.so 使用 otool 引用的内容:
$ otool -L /Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so
/Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so:
libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 438.0.0)
正确的文件似乎存在于这里:
$ ls -al /usr/local/mysql-5.5.9-osx10.6-x86/lib/libmysqlclient.16.dylib
-rwxr-xr-x 1 root wheel 3539752 Jan 20 2011 /usr/local/mysql-5.5.9-osx10.6-x86/lib/libmysqlclient.16.dylib
我尝试了几种方法让 _mysql.so 识别这个文件:
按照其他人的建议,建立指向正确文件的符号链接:
$ sudo ln -s /usr/local/mysql-5.5.9-osx10.6-x86/lib/libmysqlclient.16.dylib /usr/lib/libmysqlclient.18.dylib
当我执行此操作时,出现一个新错误:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 7, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so
Reason: no suitable image found. Did find:
/usr/lib/libmysqlclient.18.dylib: mach-o, but wrong architecture
设置 DYLD_LIBRARY_PATH,如下所示(删除前一个符号链接后):
$ 导出 DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql-5.5.9-osx10.6-x86/lib/
然后我得到与最初相同的错误:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 7, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so
Reason: image not found
我也尝试按照 Ned 的建议使用 install_name_tool 来修改引用
来自 ~/.python-eggs 目录:
尝试将 18 更改为 16: $ sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.9-osx10.6-x86/libmysqlclient.16.dylib _mysql.so
方法 2:尝试将 18 更改为 18: sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.9-osx10.6-x86/lib/mysql/libmysqlclient.18.dylib _mysql.so
当我尝试这些时,我收到错误:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.5-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
ImportError: dynamic module does not define init function (init_mysql)
有什么办法可以解决这个问题吗?我真的很想让这个包工作……我试了一段时间,但没能让它工作。我知道这是一个常见问题,但我无法让任何发布的解决方案发挥作用。
非常感谢您的帮助。
答案1
首先,我要打开一个终端并:
python <enter>
import sys <enter>
print sys.path <enter>
你的相关库在路径上吗?
关于 MacPorts 理论(上文@fideli),还有另一个名为 Homebrew 的 PMS关联. 它在 Mac 上的包管理效果出奇的好。
但是,我过去曾经这样做过(完全编译),但在 G5 PPC64 上这样做时更加麻烦。我让它工作的方式是从源代码编译 Python、Python-MySQL、MySQL 和 mod_wsgi。这是可能的,但人们遇到的主要陷阱是软件包之间的架构不匹配。这是我在 PPC 上执行此操作的笔记。从中提取您能提取的内容,如果您需要额外帮助,请告诉我们。要注意的关键部分是在每次编译之前导出 ARCHFLAGS。这将确保您具有架构匹配。正如我所说,下面是我为 PPC64 环境所做的,但是您可以将 archs 替换为相关平台,例如 x86_64,因为您运行的是 10.6,我们知道您有一个英特尔芯片。您可以忽略确切的软件版本,因为它们应该不是那么相关。我希望这对你有用。
旁注:看来您的 MySQL 是 32 位,但您的 Python 是 64 位。
前期详细信息:
- 目标平台是OSX 10.5.8 (PPC64)。
- 目标是将所有发行版编译为“Fat”二进制文件,这意味着它将支持 x86、PPC、PPC64 和 x86_64。
- 按照您认为适合目标平台的方式进行操作。
This file will go through the steps of how to
install Python, MySQL, MySQLdb-Python, and
mod_wsgi on clean MacOSX 10.5.8 all 64Bit
#################################################
1.Download Python Source -> Plan 2.7
2.Download MySQL Source -> Plan 5.1.50
3.Download MySQLdb-Python Source -> Plan 1.2.3
4.Download mod_wsgi -> Plan 1.3.3
Untar all of the above and prep for Python
#################################################
1. export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
2. ./configure --with-framework-name=Python64 --with-universal-archs=all --enable-framework --enable-universalsdk=/ MACOSX_DEPLOYMENT_TARGET=10.5
3. make
4. sudo make install
5. Add to /etc/paths
-> /usr/local/bin
6. Modify /etc/profile
-> if [ -x /usr/libexec/path_helper ]; then
-> PATH=""
-> eval `/usr/libexec/path_helper -s` <- Be sure to note these are backticks ie Shell expansion
-> fi
7. Reboot the system
8. Test Python Intereter is @2.7
Compile and Install mod_wsgi
#################################################
1. export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
2. ./configure --with-apxs=/usr/sbin/apxs --with-python=/usr/local/bin
3. make
4. sudo make install
5. Add to /etc/apache2/httpd.conf
-> LoadModule wsgi_module libexec/apache2/mod_wsgi.so
6. tail -f /var/log/apache2/error_log
7. sudo apachectl stop
8. sudo apachectl start
Compile and Install MySQL
#################################################
NOTE: It has been said to also set CFLAGS="-arch i386 -arch ppc -arch ppc64 -arch x86_64"
NOTE: It has been said to also set CXXFLAGS="-arch i386 -arch ppc -arch ppc64 -arch x86_64"
1. export ARCHFLAGS='-arch ppc64'
2. CFLAGS="-arch ppc64' CXXLAGS="-arch ppc64' ./configure -prefix=/usr/local/mysql -with-server-suffix=-standard -enable-thread-safe-client -enable-local-infile -enable-shared -with-zlib-dir=bundled -with-big-tables -with-readline -with-plugins=all -without-docs
3. make
4. sudo make install
5. cd /usr/local/mysql
6. sudo ./bin/mysql_install_db --user=mysql
7. sudo chown -R mysql ./var
8. create file com.mysql.mysqld.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>Program</key>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>mysql</string>
<key>WorkingDirectory</key>
<string>/usr/local/mysql</string>
</dict>
</plist>
9. sudo mv ~/Desktop/com.mysql.mysqld.plist /Library/LaunchDaemons
10. sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist
11. sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist
12. edit /etc/paths
-> /usr/local/mysql/bin
13. Quit all running terminals
14. run mysql_secure_installation
Compile and Install MySQLdb-Python
#################################################
NOTE: According to the compilation flags above it "should" be safe to leave Threadsafe at true
NOTE: setuptools must be installed prior to attempting the below
1. Set Threadsafe=False in site.cfg
2. export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
3. python setup.py build
4. sudo python setup.py install
5. python-all <-Universal app, python is aliased to python32 so the next step will fail
6. import MySQLdb
7. hopefully no errors
0 comments
October 4, 2010 by Chris Barker
Django Deployment on Leopard PPC64 Research
See Step By Step HOWTO.txt
#################################################
Links that have proven useful
#################################################
http://tickletik.blogspot.com/2008/12/python-and-mysql-on-64bit.html <-Best all inclusive article
http://mysql-python.blogspot.com/2008/03/i-am-not-dead.html
http://mail.python.org/pipermail/pythonmac-sig/2008-June/020123.html
http://www.stereoplex.com/2008/feb/5/mac-os-x-command-and-manual-search-path-path-and-m/
http://www.softec.lu/site/DevelopersCorner/MasteringThePathHelper
http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
http://diymacserver.com/installing-mysql/compiling-mysql-51x-in-64-bits-mode-on-leopard/
http://mysql-python.sourceforge.net/FAQ.html
Native MacOSX Leopard Bit Levels
#################################################
-Native Python (2.51) 32Bit
-Native Apache (2.2.14) 64Bit
-Native MySQL (N/A)
Build Out Python 64bit
#################################################
To Perform Universal Compilation
Execute the below prior to compiling
export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
./configure --with-framework-name=Python64 --with-universal-archs=all --enable-framework --enable-universalsdk=/ MACOSX_DEPLOYMENT_TARGET=10.5
Adjusting The Path
------------------
Add /usr/local/bin to the top of /etc/paths
Modify /etc/profile to:
if [ -x /usr/libexec/path_helper ]; then
PATH=""
eval `/usr/libexec/path_helper -s`
fi
Build Out MySQL 64Bit
#################################################
CFLAGS="-arch ppc64" <-Perhaps not needed if export ARCHFLAGS is set first
CXXFLAGS="-arch ppc64" <-Perhaps not needed if export ARCHFLAGS is set first
export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
./configure -prefix=/usr/local/mysql -with-server-suffix=-standard -enable-thread-safe-client -enable-local-infile -enable-shared -with-zlib-dir=bundled -with-big-tables -with-readline -with-plugins=all -without-docs
make
sudo make install
Build Out MySQLdb
#################################################
Set Threadsafe=False in site.cfg
export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
python setup.py build
sudo python setup.py install
Build Out Apache 64Bit ??? Or Not
#################################################
No need to rebuild apache, it is native universal binary (Runs in 64bit Navtively)
Vhost notes
-----------
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin [email protected]
DocumentRoot /usr/local/www/documents
Alias /robots.txt /usr/local/www/documents/robots.txt
Alias /favicon.ico /usr/local/www/documents/favicon.ico
Alias /media/ /usr/local/www/documents/media/
<Directory /usr/local/www/documents>
Order allow,deny
Allow from all
</Directory>
WSGIPythonHome /Path/to/Python/
WSGIDaemonProcess example.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup example.com
WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi
<Directory /usr/local/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
HTTPD.conf notes
----------------
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
Build Out mod_wsgi
#################################################
export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
./configure --with-apxs=/usr/sbin/apxs --with-python/path/to/python
make
sudo make install
Cleaning Up After Compiling
#################################################
make clean
make distclean
答案2
Enthought EPD 实际上确实有 MySQLdb,只是默认情况下未安装。要安装它:
enpkg MySQL_python
这没有很好的记录。以下是所有可选包:http://www.enthought.com/products/repository.php 祝你好运!
答案3
如果这使其变得复杂,那么只需保留 mysqldb 并改用 pymysql - 配置起来非常容易。我想使用 django 并尝试使用 MySQL,但它给出了一个错误,所以我通过使用 pymysql 解决了它。以下是在几分钟内安装和配置 pymysql 的简单方法:
http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for-python-26-and.html
如果你想知道 pymysql 和 mysqldb 之间的区别,那么你可以参考这个:
它们的用途几乎相同。