如何在OSX 10.6中将MySQLdb与Python和Django一起使用?


259

对于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适配器,但无济于事(也许我安装不正确?)。

有人处理过吗?


5
此问题是由于MySQL for Python适配器安装不完整/错误导致的。具体来说,我不得不编辑路径为/ usr /地方/ MySQL的/斌/ mysql_config的mysql_config文件点-在本文中详细讨论:dakrauth.com/blog/entry/...

只要做到这一点-------------- apt-get install python-mysqldb
shadow0359'1

pip install mysql为我工作!
伊万·卡拉斯科

Answers:


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


2
是的,我使用Synaptic软件包管理器搜索mysql-python并安装,并且效果很好。谢谢。
Tomasz Iniewicz

6
在Fedora 17下:sudo yum install MySQL-python:)
g19fanatic 2012年

20
$ mysql_config当前未安装程序“ mysql_config”。您可以通过键入以下sudo apt-get install libmysqlclient-dev
命令

1
应该这个答案添加到Django的官方文件
四维沉申思维

1
我认为我是唯一的人,在运行“ pip install MySQL-python”后出现此错误“命令python setup.py egg_info失败,错误代码为1”
RicardoGonzales 2014年

62

运行Ubuntu,我必须做:

sudo apt-get install python-mysqldb

8
他正在运行OS X,而不是Ubuntu
Facundo Casco

8
同样,这也不会放在virtualenv中
Twirrim'2

58

除了其他答案,以下内容还帮助我完成了mysql-python的安装:

virtualenv,mysql-python,pip:有人知道吗?

在Ubuntu上...

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

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


1
关键是apt-get install python-dev。不知道为什么两年后我要安装python的开发版本,但是从今天开始使用python 2.7.3仍然可以使用。仅浪费了3个小时。
user798719 2013年

23

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

brew install mysql-connector-c 
pip install MySQL-python

这种方法解决了我的问题。之前的所有其他建议给我一个错误消息Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/r8/c_tscq492sn641w5vtvqcvkw0000gp/T/pip-build-JYY1du/MySQL-python
LearnAWK

2
非常感谢,尝试上述所有步骤。没有人在OSX 10.11上工作过
-Paullo

1
@LearnAWK,它不起作用。我仍然收到错误命令“ python setup.py egg_info”,失败,错误代码为/ private / var / folders / 3m / 97mbsh5x2bnghhk55130bh_h0000gn / T / pip-build-FFU1vL / MySQL-python /
Stony,2010年

19

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

export PATH=$PATH:/usr/local/mysql/bin/
pip install MySQL-python

4
我已经尝试过此操作,但仍然收到相同的错误:'raise ImproperlyConfigured(“加载MySQLdb模块时出错:%s”%e)django.core.exceptions.ImproperlyConfigured:加载MySQLdb模块时出错:没有名为MySQLdb的模块'
filistea

14
pip install mysql-python

提出了一个错误:

EnvironmentError:找不到mysql_config

sudo apt-get install python-mysqldb

解决了问题。


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中安装了,不确定是否需要...

希望能帮助到你,


8

以下对我来说运行64位Ubuntu 13.10的完美工作:

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开关创建包含系统站点包的新virtualenv。

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

希望这可以帮助。


2
谢谢!您需要使用随virtualenv一起安装的pip命令安装MySQLdb。-帮助
dobs

谢谢。sudo ./bin/pip install mysql-python非常有帮助!
theblackpearl

8

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

pip安装MySQL-python


2
谢谢。这可以在Django 1.7.1上使用heroku和cleardb:ignite为我解决此问题。
Ryan Walton



5

这适用于Red Hat Enterprise Linux Server 6.4版

sudo yum install mysql-devel
sudo yum install python-devel
pip install mysql-python

CentOS的7,选项1:yum install MySQL-python; 选项2(如果使用的virtualenv): yum install mysql-devel; venv/bin/pip install MySQL-python
hudolejev's


3

我进行了OSX Mavericks和Pycharm 3的升级,并开始出现此错误,我使用了pip并易于安装,并得到了以下错误:

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

所以我需要更新到Xcode 5,然后再次尝试使用pip进行安装。

pip install mysql-python

那解决了所有问题。


2

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

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

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


我尝试了此操作:stackoverflow.com/questions/25459386/… 但是失败了,这救了我。(Y)
Kamaldeep singh Bhatia,

2

如果您使用的是python3,请尝试以下操作(我的操作系统是Ubuntu 16.04):

sudo apt-get install python3-mysqldb

2

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


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的解决方案。在这里检查


0

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

pip uninstall mysql-python
pip install mysql-python


0

我通过MySQL-python使用pip安装库克服了相同的问题。当我第一次在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$ 


0

我遇到了类似的情况,例如您在Mac OS X上的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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.