问题
I am trying to get started on working with Python on Django I am by profession a PHP developer and have been told to set up django and python on my current apache and mysql setup however I am having trouble getting the Mysqldb module for python to work, I must of followed about 6 different set of instructions, I am running snow leopard and have mysql installed natively it is not part of MAMP or similar. Please can some tell me where I need to start and what steps I need to follew I would be most grateful.
Thanks
回答1:
I'd recomend installing macports (latest svn trunk) and installing mysql from there.
sudo port install mysql5-server
Download the MySQL-python-1.2.2 source
make sure /opt/local/lib/mysql5/bin is in your path or edit site.cfg to include:
mysql_config = /opt/local/lib/mysql5/bin/mysql_config
Comment out line 38 of _mysql.c
// #define uint unsigned int
Then run:
sudo python setup.py install
should be all good.
回答2:
On MAC OS X 10.6, Install the package as usual. The dynamic import error occurs because of wrong DYLD path. Export the path and open up a python terminal.
$ sudo python setup.py clean
$ sudo python setup.py build
$ sudo python setup.py install
$ export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
$python
import MySQLdb
Now import MySQLdb should work fine.
You may also want to manually remove the build folder, before build and install. The clean command does not do a proper task of cleaning up the build files.
回答3:
[Partial Answer]
You'll have more fun pulling out your teeth. MySQL/Django/Mac is a disaster. This is the farthest I've gotten:
Get MySQLDB 1.2.3 Go into that and modify setup_posix.py:
Change:
mysql_config.path = "mysql_config"
To (depending on the version number of your MySQL):
mysql_config.path = "/usr/local/mysql-5.1.34-osx10.5-x86_64/bin/mysql_config"
python setup.py build python setup.py install
Here's a good article
回答4:
First and foremost, make sure that XCode is installed. Without XCode, many pieces of the built-in Apache2 server important to developers are missing; most notably, the GNU Compiler Collection, which I would think to be requisite for MySQL bindings.
回答5:
One of the key things here is to make sure you're running both MySQL and the Python adaptor in 64 bit. The default Python 2.6.1 install on Snow Leopard is 64 bit so you must install MySQL in 64 bit and build the MySQLdb adapter in 64 bit.
- Make sure you have the latest Xcode installed
- Install 64-bit MySQL from DMG - http://dev.mysql.com/downloads/mirror.php?id=401941#mirrors
- Download MySQL-python-1.2.3 from http://download.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.3.tar.gz
Extract and make the following edit to site.cfg:
mysql_config = /usr/local/mysql/bin/mysql_config
Then from your terminal run
ARCHFLAGS="-arch x86_64" python setup.py build sudo python setup.py install
You should then open a Python shell and type:
import MySQLdb
If you don't get any errors you're golden.
回答6:
Xcode was installed.
Follow these instructions for setting up apache/php/mysql:
http://maestric.com/doc/mac/apache_php_mysql_snow_leopard
I installed the free 32-bit version of EPD (this is optional but I wanted numpy/scipy).
Make sure you have these lines in your ~/.profile (the second line is only if you installed EPD):
export PATH=/usr/local/mysql/bin/:/usr/local/bin:/usr/local/sbin:$PATH export PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
Downloaded mysqldb and run:
python setup.py build sudo python setup.py install
Install the official release of Django or web2py. Everything worked after that. You can try the "Writing Your First Django App, Part 1" to test it out where in settings.py use:
DATABASE_ENGINE = 'mysql' DATABASE_NAME = 'Django Polls' DATABASE_USER = '*****' DATABASE_PASSWORD = '*****' DATABASE_HOST = '127.0.0.1' DATABASE_PORT = '3306'
I also use Navicat Lite and Sequel Pro for working with MySQL.
Pydev is a nice IDE to use with Django.
回答7:
- Install the newest 64bit DMG version of MySQL. Remember to backup your databases if you already have MySQL installed.
enter this line in terminal:
sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql
Edit the file setup_posix in the mysql-python installation directory.
Change the line
mysql_config.path = "mysql_config"
to
mysql_config.path = "/usr/local/mysql/bin/mysql_config"
- Myslq-Python needs a 64bit Version of Python. The new Python 2.7 64bit version creates an alias in /usr/local/bin/python.
Enter the following lines in the mysql-python folder
sudo /usr/local/bin/python setup.py clean
sudo ARCHFLAGS="-arch x86_64"
sudo /usr/local/bin/python setup.py build
sudo /usr/local/bin/python setup.py install
And finally try it out:
python
import MySQLdb
回答8:
You could make your life a lot easier (especially on Lion) by just installing Mariadb instead:
http://mariadb.org/
It's a drop-in replacement for MySQL and is plug&play on OSX. The only thing you don't get is the MySQL system setting.
来源:https://stackoverflow.com/questions/1465846/install-mysqldb-on-snow-leopard