在 mac 上安装 MySQL-python

Posted

技术标签:

【中文标题】在 mac 上安装 MySQL-python【英文标题】:Installing MySQL-python on mac 【发布时间】:2013-07-10 02:32:21 【问题描述】:

我正在使用 OSX 10.8 和 PyCharm 进行 Python 开发项目。我已经使用网站上的说明为 mac 安装了 mysql-python

http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found

但是,运行项目给我这个错误:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
  Expected in: flat namespace
 in /Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so

错误中提到的文件存在于位置 - /Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so

整个错误信息是-

/usr/local/bin/python2.7-32 /Users/ashishagarwal/Optimus/MashPotato/backend/mashpotato/manage.py testserver --addrport 8000
Running on development server
Traceback (most recent call last):
  File "/Users/ashishagarwal/Optimus/MashPotato/backend/mashpotato/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/south/management/commands/__init__.py", line 10, in <module>
    import django.template.loaders.app_directories
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loaders/app_directories.py", line 23, in <module>
    mod = import_module(app)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 3, in <module>
    from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/helpers.py", line 4, in <module>
    from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/util.py", line 6, in <module>
    from django.db import models
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
    backend = load_backend(connection.settings_dict['ENGINE'])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 27, in load_backend
    return import_module('.base', backend_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
  Expected in: flat namespace
 in /Users/ashishagarwal/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so

Process finished with exit code 1

【问题讨论】:

按照这个答案在macos上使用python3+sqlalchemy+mysql ***.com/a/54031440/4252616 【参考方案1】:

您应该先通过Homebrew 安装 MySQL,以使 python-mysql 在 OS X 上正常工作。

pip uninstall MySQL-python
brew install mysql
pip install MySQL-python

【讨论】:

你能给出原因吗? 包从源码编译,需要mysql开发头文件。当您使用 homebrew 安装 mysql 软件包时,这些也会被安装。 你不知道你为我节省了多少时间!非常感谢! 捕获了部分安装的 brew 和 pip 之间的决斗配置。我必须执行以下操作: $ brew link mysql Linking /usr/local/Cellar/mysql/5.7.20... 错误:无法符号链接 bin/my_print_defaults 目标 /usr/local/bin/my_print_defaults 是属于 mysql 的符号链接-连接器-c。您可以取消链接: brew unlink mysql-connector-c $ brew unlink mysql-connector-c Unlinking /usr/local/Cellar/mysql-connector-c/6.1.11... 删除 48 个符号链接 $ brew link mysql Linking /usr /local/Cellar/mysql/5.7.20... 创建了 93 个符号链接 $ pip install MySQL-python【参考方案2】:

确保您拥有最新版本的 Xcode,包括命令行工具。我只是从应用商店下载了 2GB 的更新。我有一个类似的问题,特别是this,并且解决了它。

干杯!

【讨论】:

【参考方案3】:

我很久以前用自制软件安装了 mysql,所以当我在升级 OSX 后遇到问题时,我用 brew reinstall mysql@5.7 重新安装了它(我运行了 5.7.10)并重新安装了 MySQL-python,我希望它会得到解决。出现上述错误。

它真的让我很头疼,在花了几个小时之后,我不知道该做什么,从源代码和所有东西重建。为 mysql 尝试了其他 python 应用程序,但它是同一件事。

我不知何故意识到 MySQL-python 构建的客户端是针对错误版本的,所以我检查了 /usr/local/opt/mysql 链接到的位置,它链接到了我通过 dmg 安装的先前版本,例如7年前!一旦我删除了那个链接,然后运行brew link mysql,然后创建了一个新的 virtualenv 并从头开始安装了所有的包,它终于可以工作了!

【讨论】:

【参考方案4】:

如果你使用 python 喜欢:

import mysql.connector

你要先下载mysql Connector 这里是链接link

【讨论】:

以上是关于在 mac 上安装 MySQL-python的主要内容,如果未能解决你的问题,请参考以下文章

怎么在Mac上安装Tomcat 7

Mac - 如何在Mac上安装多个版本openjdk并有效管理

mac系统上怎么安装java?

mac如何安装python

在 mac 上安装 cocoapods 失败

如何在 Mac 上安装 pip?