Depurando um mundo do inferno ao tentar instalar o MySQL-Python no Mac OS X

2

Eu tenho tentado por semanas e semanas para instalar o MySQL-python com absolutamente nenhuma sorte, no Mac OS X 10.6 com Python 7.0 do Enthought Python Distribution. Muitos dos problemas que encontrei foram descritos em outros posts e tenho certeza que li todos eles, mas ainda não consigo resolver esse problema. Eu tentei instalar e reinstalar vários pacotes MySQL para 64 bits OS X, mas finalmente apenas compilá-lo a partir do zero. O MySQL, até onde eu sei, trabalha e mora no diretório /usr/local/mysql-5.5.9-osx10.6-x86/.

Eu consegui instalar o MySQL-python-1.2.3 usando o "sudo setup.py install". No entanto, quando eu importo, recebo o erro:

$ 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

Eu olhei para ver, como outros pôsteres aqui sugeriram, o que _mysql.so está fazendo referência usando o 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)

O arquivo correto parece existir aqui:

$ 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

Eu tentei várias abordagens para fazer o _mysql.so reconhecer esse arquivo:

  1. Fazendo links simbólicos, como outros sugeriram, para o arquivo correto:

    $ sudo ln -s /usr/local/mysql-5.5.9-osx10.6-x86/lib/libmysqlclient.16.dylib /usr/lib/libmysqlclient.18.dylib

quando faço isso, recebo um novo erro:

>>> 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
  1. Definindo DYLD_LIBRARY_PATH, como segue (após remover o link simbólico anterior):

    $ export DYLD_LIBRARY_PATH = $ DYLD_LIBRARY_PATH: /usr/local/mysql-5.5.9-osx10.6-x86/lib /

Depois recebo o mesmo erro inicialmente:

>>> 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
  1. Eu também tentei como Ned sugeriu usar install_name_tool para modificar a referência

    do diretório ~ / .python-eggs:

    tentando mudar de 18 para 16: $ sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.9-osx10.6-x86/libmysqlclient.16.dylib _mysql.so

    abordagem 2: tentando mudar de 18 a 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

Quando eu tento isso, recebo o erro:

>>> 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)

Alguma idéia do que pode ser feito para corrigir isso? Eu realmente gostaria de ter este pacote funcionando ... Eu tentei por um tempo e não consegui fazê-lo funcionar. Eu entendo que é um problema comum, mas não consegui que nenhuma das soluções publicadas funcionasse.

Muito obrigado pela sua ajuda.

    
por user46976 01.09.2011 / 01:06

3 respostas

1

Primeiro eu abriria um terminal e:

python <enter>
import sys <enter>
print sys.path <enter>

Sua biblioteca relevante está no caminho?

Na teoria MacPorts (@fideli acima) há também outro PMS chamado Homebrew link . Funciona surpreendentemente bem para o gerenciamento de pacotes no Mac.

No entanto, eu fiz isso (totalmente compilado) no passado com mais dor que eu estava fazendo em um G5 PPC64. A maneira como eu consegui que funcionasse era compilar Python, Python-MySQL, MySQL e mod_wsgi todos a partir do código-fonte. É possível, mas a grande armadilha que as pessoas enfrentam é a falta de correspondência de arquitetura entre os pacotes. Aqui estão as minhas notas de fazê-lo em um PPC. Extraia o que puder e, se precisar de ajuda extra, por favor nos avise. A parte chave para prestar atenção é exportar o ARCHFLAGS antes de cada compilação. Isso garantirá que você tenha uma correspondência de arquitetura. Como eu disse o seguinte é o que eu fiz para um ambiente PPC64, no entanto, você pode simplesmente substituir os archs por sua plataforma relevante, por exemplo, x86_64 desde que você está executando 10.6 nós sabemos que você tem um chip Intel. Você pode ignorar as versões exatas do software, pois elas não devem ser tão relevantes. Espero que isso seja útil para você.

Sidenote: Parece que o seu MySQL é de 32 bits, mas o seu Python é de 64 bits.

Detalhes iniciais:

  • A plataforma de destino foi o OSX 10.5.8 (PPC64).
  • O objetivo era compilar todas as distribuições como binários "Fat", o que significa que ele suportará x86, PPC, PPC64 e x86_64.
  • Manipule isso como achar melhor para sua plataforma de segmentação.
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
    
por 03.09.2011 / 16:44
1

O EPD pensado realmente tem o MySQLdb disponível, ele simplesmente não é instalado por padrão. Para instalá-lo:

enpkg MySQL_python

Isso não está bem documentado. Aqui estão todos os pacotes opcionais: link Boa sorte!

    
por 27.01.2012 / 22:43
0

Se estiver tornando complexo, deixe o mysqldb e use o pymysql - que é realmente fácil de configurar. Eu queria trabalhar com o django e estava tentando usar o MySQL, mas estava dando um erro, então resolvi usando o pymysql. Aqui está uma maneira fácil de instalar e configurar o pymysql em poucos minutos:

http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for-python-26-and.html

Se você quer saber a diferença entre o pymysql e o mysqldb, então você pode consultar isto:

https://stackoverflow.com/questions/7224807/what-actually-is-pymysql-and-how-it-differs-from-mysqldb-and-how-can-it-affect

Eles servem quase o mesmo propósito.

    
por 03.09.2011 / 16:51