如何在MAMP堆栈中使用Django和MySQL?

时间:2010-05-24 20:27:09

标签: python django mysql mamp

我特别难以安装MySQLdb模块(MySQL-python-1.2.3c1),以连接到MAMP堆栈中的MySQL。 我做了很多事情,例如将mysql包含目录和库(包括插件)从全新安装的mysql(版本5.1.47)复制到MAMP内部(版本5.1.37)。

现在,MySQLdb模块的构建和安装并没有给我错误。

当我从python shell(版本2.6)调用'import MySQLdb'时会发生错误。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in <module>

  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/rhenru/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Users/rhenru/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
  Expected in: flat namespace
 in /Users/rhenru/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so

任何想法,我还需要做些什么来使其有效?

非常感谢, 罗伯特

====== 按照Hank Gay建议使用virtualenv后添加系统响应......

(MyDjangoProject)MyMacPro:MyDjangoProject rhenru$ which python
/Users/rhenru/Workspace/django/MyDjangoProject/bin/python

在virtualenv中运行python后,导入MySQLdb:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in <module>

  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/rhenru/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Users/rhenru/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
  Expected in: flat namespace
 in /Users/rhenru/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so

import sys and sys.path

>>> import sys
>>> print sys.path
['', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/site-packages/distribute-0.6.10-py2.6.egg', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/site-packages/pip-0.7.1-py2.6.egg', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python26.zip', '/Library/Python/2.6/site-packages/PyXML-0.8.4-py2.6-macosx-10.6-universal.egg', '/Library/Python/2.6/site-packages/pydot-1.0.2-py2.6.egg', '/Library/Python/2.6/site-packages/pyparsing-1.5.2-py2.6.egg', '/Library/Python/2.6/site-packages/vobject-0.8.1c-py2.6.egg', '/Library/Python/2.6/site-packages/pytz-2010h-py2.6.egg', '/Library/Python/2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg', '/Library/Python/2.6/site-packages/distribute-0.6.12-py2.6.egg', '/Library/Python/2.6/site-packages/pip-0.7.1-py2.6.egg', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/plat-darwin', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/plat-mac', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/plat-mac/lib-scriptpackages', '/Users/rhenru/Workspace/django/MyDjangoProject/Extras/lib/python', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/lib-tk', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/lib-old', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/Users/rhenru/Workspace/django/MyDjangoProject/lib/python2.6/site-packages', '/Library/Python/2.6/site-packages', '/Library/Python/2.6/site-packages/PIL', '/Library/Python/2.6/site-packages/setuptools-0.6c11-py2.6.egg-info', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode']

2 个答案:

答案 0 :(得分:1)

您如何安装MySQL-Python?我刚刚尝试了一个新的virtualenv,pip install mysql-python似乎已经完成了这个伎俩。

更新:

pip有点像Python包的包管理器。

默认情况下,pip会安装到您site-packages上的当前$PYTHONPATH目录中。这使得其他库/应用程序(如Django)可以访问它。 pip也适用于virtualenv(它应该; Ian Bicking同时编写它们),这是一个非常棒的库,可以让您对应用程序进行沙盒处理。这很好,因为这意味着您可以尝试新事物而不会污染全局site-packages目录(甚至需要写入权限)。

现在可能看起来像牦牛皮,但我认为值得努力加快pipvirtualenv的速度(您可能还想研究{{3}但是,我们暂时不会跳过它;它只是virtualenv的糖。这将导致略微更复杂的部署方案,而不是将所有内容放在全局site-packages中,但是对于开发来说,实际上并不困难,并且有很多很好的指南可以使用{{{ 1}}。

我建议如下:

  1. virtualenv
  2. curl -0 http://python-distribute.org/distribute_setup.py
  3. python distribute_setup.py
  4. easy_install pip
  5. pip install virtualenv
  6. virtualenv --distribute MyDjangoProject --no-site-packages
  7. cd MyDjangoProject(这会激活source bin/activate创建的沙箱
  8. virtualenv
  9. 此时,你应该有一个完全功能的Django + MySQL安装(如果我错过任何步骤,只需评论,我会尝试添加它)。你可以像这样开始你的Django项目:pip install django mysql-pythondjango-admin.py startproject MyDjangoProject进入项目目录,编辑cd文件以指向MySQL数据库,并运行开发服务器进行测试,如下所示:settings.py(您可能需要{{1}你的./manage.py runserver文件。瞧!您应该可以通过chmod u+x访问您的网站。完成项目后,您可以使用manage.py退出localhost:8000沙箱。

    尽量不要对Django持有这一切:很多只是使用Python库的最佳实践。你可以用更少的东西来解决这个问题,但这种方式更可重复,并且在处理不同的项目时,你不太可能意外地弄乱了这个项目的依赖项之一。

答案 1 :(得分:0)

我遇到了这个问题,原因是错误的配置:     export VERSIONER_PYTHON_PREFER_32_BIT = yes 我不记得我启用了什么(一些需要32位的软件包),可能与谷歌AppEngine有关。但是将问题设置为“否”会解决问题。

否则我只是用homebrew和pip安装了所有东西。