在Mac OS 10.6.2 Snow Leopard上使用Django和MySQL

9 投票
3 回答
11063 浏览
提问于 2025-04-15 17:01

这个问题之前已经有一些很好的回答,但现在这些回答已经过时了。

我已经成功安装了这个模块,但当我运行“python manage.py runserver”时出现了错误:

 iMac:myproject drhoden$ python manage.py runserver
 Validating models...
 Unhandled exception in thread started by <function inner_run at 0x10496f0>
 Traceback (most recent call last):
   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run
     self.validate(display_num_errors=True)
   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 249, in validate
     num_errors = get_validation_errors(s, app)
   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/validation.py", line 22, in get_validation_errors
     from django.db import models, connection
   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 41, in <module>
     backend = load_backend(settings.DATABASE_ENGINE)
   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 17, in load_backend
     return import_module('.base', 'django.db.backends.%s' % backend_name)
   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
     __import__(name)
   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 13, in <module>
     raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
 django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dynamic module does not define init function (init_mysql)

 ^CiMac:segisys drhoden$ 

同样,在python命令行中也出现了问题:

iMac:myproject drhoden$ python
Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) 
[GCC 4.0.1 (Apple Inc. build 5493)] 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.3-fat/egg/MySQLdb/__init__.py", line 19, in <module>

  File "build/bdist.macosx-10.3-fat/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.3-fat/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)
>>> 

我在使用MySQL-python-1.2.3c1和setuptools-0.6c11-py2.6.egg。

如果有人能提供帮助,我将非常感激。

3 个回答

1

这个问题发生在你同时使用了32位和64位的软件时。

简单来说,对于Snow Leopard系统,你需要安装64位的MySQL软件包(虽然它的版本显示为10.5,但这没关系),然后再简单地重新安装一下python-mysql。这样一切就能正常工作了。

4

几个月前,我写了一篇博客,分享了我在Snow Leopard上成功安装MySQL的经历:

http://jboxer.com/2009/09/installing-mysql-on-snow-leopard/

如果你按照那些步骤操作,理论上应该能解决你的问题(听起来是因为32位和64位软件混用导致的)。

顺便说一下,我并不是想在这里自我宣传;博客里的内容基本上就是我在这里想说的,我只是想在生活的更多方面应用“不要重复自己”的原则 :)

21

我最终解决了自己的问题,这当然离不开我在网上看过的很多帖子、博客和邮件记录的潜移默化和显性帮助。如果我能记得的话,我会给出链接。

简单来说,我使用MacPorts重新安装了所有东西。

在编辑了~/.bash_profile文件并注释掉之前对${PATH}的所有修改后,我下载了适用于Snow Leopard的dmg文件并完成了安装。

然后打开终端,进行了自我更新。

sudo port selfupdate
sudo port install python26

安装Python 2.6的过程花了我很长时间。不过当它完成后,给我提示了以下内容:

To fully complete your installation and make python 2.6 the default, please run

sudo port install python_select  
sudo python_select python26

我做了这两件事,速度很快。

我忘了提到'port search '命令有多方便。我搜索了'mysql',找到了在'install'后面要输入的内容。但我还是重新安装了MySQL的客户端和服务器。也许我这个顺序搞反了,但最后的结果很好。

sudo port install mysql5
...
--->  Installing mysql5 @5.1.41_0
The MySQL client has been installed.
If you also want a MySQL server, install the mysql5-server port.

所以自然地:

sudo port install mysql5-server

我很喜欢很多macports的安装过程会给你反馈,告诉你接下来该做什么。在服务器安装结束时,它说了以下内容:

******************************************************
* In order to setup the database, you might want to run
* sudo -u _mysql mysql_install_db5
* if this is a new install
******************************************************

这是我全新的安装(没有任何本地的数据库模式)。为了完整性和我自己的参考,这里是运行那个命令的输出:

Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h iMac.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /opt/local/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/

快完成了。在我之前的'port search'中,我发现了这个有趣的端口:

py26-mysql @1.2.2 (python, devel, databases) Python与mysql的接口

我满怀希望地安装了它,希望它能提供MySQLdb包,结果它确实提供了。

sudo port install py26-mysql

之后我启动了Python解释器,尝试导入MySQLdb,这一直是我遇到的障碍。

iMac:~ drhoden$ python
Python 2.6.4 (r264:75706, Dec 15 2009, 18:00:14) 
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import ImmutableSet
>>> 

虽然有个警告,但它成功了!!

还有一件事:

sudo port install py26-django

经过这一切,我终于能够启动我的Django项目,并远程连接到我公司的MySQL服务器!!虽然可能没必要通过MacPorts重新安装Django,但我不想冒任何麻烦的风险。

撰写回答