261

对于 OSX 10.6 用户来说,这是一个备受讨论的问题,但我一直无法找到可行的解决方案。这是我的设置:

Python 2.6.1 64 位 Django 1.2.1 MySQL 5.1.47 osx10.6 64 位

我使用 --no-site-packages 创建了一个 virtualenvwrapper,然后安装了 Django。当我激活 virtualenv 并运行 python manage.py syncdb 时,我收到此错误:

Traceback (most recent call last):
File "manage.py", line 11, in <module>
  execute_manager(settings)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
  utility.execute()
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 257, in fetch_command
  klass = load_command_class(app_name, subcommand)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
  module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/sql.py", line 5, in <module>
from django.contrib.contenttypes import generic
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/contrib/contenttypes/generic.py", line 6, in <module>
  from django.db import connection
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/__init__.py", line 75, in <module>
  connection = connections[DEFAULT_DB_ALIAS]
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 91, in __getitem__
  backend = load_backend(db['ENGINE'])
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 32, in load_backend
  return import_module('.base', backend_name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
  raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

我还安装了MySQL for Python适配器,但无济于事(也许我安装不正确?)。

以前有人处理过这个吗?

4

23 回答 23

330

我有同样的错误并pip install MySQL-python为我解决了。

备用安装:

  • 如果你没有点子,easy_install MySQL-python应该可以工作。
  • 如果您的 python 是由包装系统管理的,您可能必须使用该系统(例如sudo apt-get install ...

下面,Soli 指出,如果您收到以下错误:

EnvironmentError: mysql_config not found

...然后你有一个进一步的系统依赖问题。解决这个问题会因系统而异,但对于 Debian 派生系统:

sudo apt-get install python-mysqldb

于 2010-11-12T22:55:07.430 回答
62

运行 Ubuntu,我必须这样做:

sudo apt-get install python-mysqldb
于 2011-02-02T23:59:36.057 回答
58

添加到其他答案,以下帮助我完成安装mysql-python:

virtualenv、mysql-python、pip:有人知道怎么做吗?

在 Ubuntu 上...

apt-get install libmysqlclient-dev
apt-get install python-dev
pip install mysql-python

如果您没有适当的权限,请不要忘记在命令的开头添加“sudo”。

于 2011-08-11T19:03:23.833 回答
23

试试下面的命令。他们为我工作:

brew install mysql-connector-c 
pip install MySQL-python
于 2015-05-11T08:26:24.650 回答
19

mysql_config必须在路径上。在 Mac 上,执行

export PATH=$PATH:/usr/local/mysql/bin/
pip install MySQL-python
于 2012-06-14T20:04:34.663 回答
14
pip install mysql-python

引发错误:

环境错误:找不到 mysql_config

sudo apt-get install python-mysqldb

解决了这个问题。

于 2011-11-30T17:05:25.007 回答
11

我是如何让它工作的:

virtualenv -p python3.5 env/test

采购我的环境后:

pip install pymysql
pip install django

然后,我运行 startproject 并在 manage.py 中添加了以下内容:

+ try:
+     import pymysql
+     pymysql.install_as_MySQLdb()
+ except:
+     pass

此外,更新了这个内部设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'foobar_db',
        'USER': 'foobaruser',
        'PASSWORD': 'foobarpwd',
    }
}

我也configparser==3.5.0安装在我的virtualenv中,不确定是否需要...

希望能帮助到你,

于 2016-06-28T17:45:04.883 回答
8

试试这个:这为我解决了这个问题。

点安装 MySQL-python

于 2014-03-06T05:42:23.680 回答
8

以下对我来说非常有效,运行 Ubuntu 13.10 64 位:

sudo apt-get install libmysqlclient-dev
sudo apt-get install python-dev

现在,导航到您的 virtualenv(例如 env 文件夹)并执行以下命令:

sudo ./bin/pip install mysql-python

我实际上在一个单独的问题中找到了解决方案,并在下面引用它:

如果您使用 --no-site-packages 开关(默认)创建了 virtualenv,则系统范围内安装的附加组件(例如 MySQLdb)不包含在虚拟环境包中。

您需要使用与 virtualenv 一起安装的 pip 命令安装 MySQLdb。使用 bin/activate 脚本激活 virtualenv,或者使用 virtualenv 中的 bin/pip 在本地安装 MySQLdb 库。

或者,使用 --system-site-package 开关创建一个包含系统站点包的新虚拟环境。

我认为这也应该适用于 OSX。唯一的问题是获得一个等效的安装命令,libmysqlclient-dev因为我猜python-dev它们需要编译 。mysql-python

希望这可以帮助。

于 2014-02-22T19:44:28.770 回答
7

此问题是 MySQL for Python 适配器安装不完整/不正确的结果。具体来说,我必须编辑 mysql_config 文件的路径以指向 /usr/local/mysql/bin/mysql_config - 在本文中有更详细的讨论:http: //dakrauth.com/blog/entry/python-and- django-setup-mac-os-x-leopard/

于 2010-09-23T22:35:03.343 回答
7

sudo apt-get install python-mysqldb在 ubuntu 中完美运行

pip install mysql-python引发环境错误

于 2015-10-22T21:21:26.390 回答
5

这适用于 Red Hat Enterprise Linux Server 6.4 版

sudo yum install mysql-devel
sudo yum install python-devel
pip install mysql-python
于 2014-04-26T17:42:32.743 回答
4

您可以安装为 pip install mysqlclient

于 2016-10-25T17:50:36.287 回答
3

我升级到 OSX Mavericks 和 Pycharm 3 并开始收到此错误,我使用 pip 并轻松安装并收到错误:

命令'/usr/bin/clang' 失败,退出状态为 1。

所以我需要更新到 Xcode 5 并再次尝试使用 pip 安装。

pip install mysql-python

这样就解决了所有问题。

于 2013-11-27T20:33:54.633 回答
2

这里提出的错误是在导入 python 模块。这可以通过将 python site-packages 文件夹添加到 OS X 上的环境变量 $PYTHONPATH 来解决。因此我们可以将以下命令添加到 .bash_profile 文件中:

export PYTHONPATH="$PYTHONPATH:/usr/local/lib/pythonx.x/site-packages/"

*用你正在使用的python版本替换xx

于 2014-11-13T05:01:05.650 回答
2

pip在 Windows 8 64 位系统上对我不起作用。 easy_install mysql-python为我工作。easy_install如果 pip 不起作用,您可以使用它来避免在 Windows 上构建二进制文件。

于 2015-07-04T02:57:26.263 回答
2

如果您使用的是 python3,那么试试这个(我的操作系统是 Ubuntu 16.04):

sudo apt-get install python3-mysqldb
于 2017-03-08T02:46:42.480 回答
1

我在 OSX 10.6.6 上遇到了同样的问题。但是只是一个简单easy_install mysql-python的终端并没有解决它,因为随后出现了另一个问题:

error: command 'gcc-4.2' failed with exit status 1.

显然,从 XCode3(OSX 10.6 自带)升级到 XCode4 后会出现此问题。这个较新的版本删除了对构建 ppc 拱门的支持。如果是同样的情况,请尝试如下操作easy_install mysql-python

sudo bash
export ARCHFLAGS='-arch i386 -arch x86_64'
rm -r build
python setup.py build
python setup.py install

非常感谢 Ned Deily 提供的解决方案。在这里检查

于 2011-07-27T05:13:47.820 回答
0

运行此命令

sudo pip install mysql-python;

现在你可以运行你的命令了。

python manage.py startapp filename;
于 2017-11-28T06:01:11.143 回答
0

对我来说,只需重新安装 mysql-python 即可解决问题

pip uninstall mysql-python
pip install mysql-python
于 2017-01-04T16:07:25.293 回答
0

我遇到了类似的情况,我在 mac osx 上的 virtualenv 中使用 python3.7 和 django 2.1。尝试运行命令:

pip install mysql-python
pip install pymysql

并在您的项目文件夹中编辑__init__.py文件并添加以下内容:

import pymysql

pymysql.install_as_MySQLdb()

然后运行:python3 manage.py runserverpython manage.py runserver

于 2018-08-05T10:31:16.120 回答
0

我通过使用pipMySQL-python安装库克服了同样的问题。当我第一次在 settings.py 中更改数据库设置并执行 makemigrations 命令时,您可以看到控制台上显示的消息(解决方案遵循以下消息,请看)。

  (vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
    class Permission(models.Model):
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

最后我克服了这个问题如下:

(vir_env) admins-MacBook-Pro-3:src admin$ pip install MySQLdb
Collecting MySQLdb
  Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb
(vir_env) admins-MacBook-Pro-3:src admin$ pip install MySQL-python
Collecting MySQL-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    100% |████████████████████████████████| 112kB 364kB/s 
Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python ... done
  Stored in directory: /Users/admin/Library/Caches/pip/wheels/38/a3/89/ec87e092cfb38450fc91a62562055231deb0049a029054dc62
Successfully built MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5
(vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations
No changes detected
(vir_env) admins-MacBook-Pro-3:src admin$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, rest_framework, messages, crispy_forms
  Apply all migrations: admin, contenttypes, sessions, auth, PyApp
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying PyApp.0001_initial... OK
  Applying PyApp.0002_auto_20170310_0936... OK
  Applying PyApp.0003_auto_20170310_0953... OK
  Applying PyApp.0004_auto_20170310_0954... OK
  Applying PyApp.0005_auto_20170311_0619... OK
  Applying PyApp.0006_auto_20170311_0622... OK
  Applying PyApp.0007_loraevksensor... OK
  Applying PyApp.0008_auto_20170315_0752... OK
  Applying PyApp.0009_auto_20170315_0753... OK
  Applying PyApp.0010_auto_20170315_0806... OK
  Applying PyApp.0011_auto_20170315_0814... OK
  Applying PyApp.0012_auto_20170315_0820... OK
  Applying PyApp.0013_auto_20170315_0822... OK
  Applying PyApp.0014_auto_20170315_0907... OK
  Applying PyApp.0015_auto_20170315_1041... OK
  Applying PyApp.0016_auto_20170315_1355... OK
  Applying PyApp.0017_auto_20170315_1401... OK
  Applying PyApp.0018_auto_20170331_1348... OK
  Applying PyApp.0019_auto_20170331_1349... OK
  Applying PyApp.0020_auto_20170331_1350... OK
  Applying PyApp.0021_auto_20170331_1458... OK
  Applying PyApp.0022_delete_postoffice... OK
  Applying PyApp.0023_posoffice... OK
  Applying PyApp.0024_auto_20170331_1504... OK
  Applying PyApp.0025_auto_20170331_1511... OK
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying sessions.0001_initial... OK
(vir_env) admins-MacBook-Pro-3:src admin$ 
于 2017-04-01T06:03:47.340 回答
0

为我安装命令行工具:

xcode-select --install
于 2017-03-11T19:42:37.540 回答