How to use MySQLdb with Python and Django in OSX 10.6?

286,594

Solution 1

This issue was the result of an incomplete / incorrect installation of the MySQL for Python adapter. Specifically, I had to edit the path to the mysql_config file to point to /usr/local/mysql/bin/mysql_config - discussed in greater detail in this article: http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/

Solution 2

I had the same error and pip install MySQL-python solved it for me.

Alternate installs:

  • If you don't have pip, easy_install MySQL-python should work.
  • If your python is managed by a packaging system, you might have to use that system (e.g. sudo apt-get install ...)

Below, Soli notes that if you receive the following error:

EnvironmentError: mysql_config not found

... then you have a further system dependency issue. Solving this will vary from system to system, but for Debian-derived systems:

sudo apt-get install python-mysqldb

Solution 3

Running Ubuntu, I had to do:

sudo apt-get install python-mysqldb

Solution 4

Adding to other answers, the following helped me finish the installation mysql-python:

virtualenv, mysql-python, pip: anyone know how?

On Ubuntu...

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

Don't forget to add 'sudo' to the beginning of commands if you don't have the proper permissions.

Solution 5

Try this the commands below. They work for me:

brew install mysql-connector-c 
pip install MySQL-python
Share:
286,594

Related videos on Youtube

Joe
Author by

Joe

Updated on July 05, 2020

Comments

  • Joe
    Joe almost 4 years

    This is a much discussed issue for OSX 10.6 users, but I haven't been able to find a solution that works. Here's my setup:

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

    I create a virtualenvwrapper with --no-site-packages, then installed Django. When I activate the virtualenv and run python manage.py syncdb, I get this error:

    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
    

    I've also installed the MySQL for Python adapter, but to no avail (maybe I installed it improperly?).

    Anyone dealt with this before?

    • Joe
      Joe almost 14 years
      This issue was the result of an incomplete / incorrect installation of the MySQL for Python adapter. Specifically, I had to edit the path to the mysql_config file to point to /usr/local/mysql/bin/mysql_config - discussed in greater detail in this article: dakrauth.com/blog/entry/…
    • shadow0359
      shadow0359 over 7 years
      just do this----------apt-get install python-mysqldb
    • Ivan Carrasco Quiroz
      Ivan Carrasco Quiroz almost 7 years
      pip install mysql worked for me!
  • Tomasz Iniewicz
    Tomasz Iniewicz about 13 years
    yea, i used Synaptic Package Manager to search for mysql-python and install and it worked great. Thanks.
  • Facundo Casco
    Facundo Casco almost 13 years
    He is running OS X, not Ubuntu
  • Twirrim
    Twirrim about 12 years
    Also that wouldn't put it in the virtualenv
  • g19fanatic
    g19fanatic over 11 years
    Under Fedora 17: sudo yum install MySQL-python :)
  • a coder
    a coder over 11 years
    error: Can't download osdn.dl.sourceforge.net/sourceforge/mysql-python/…: 404 Not Found
  • Cerin
    Cerin over 11 years
    Last note is incorrect. There's no pip or system package named "python-mysqldb".
  • anders
    anders over 11 years
    $ mysql_config The program 'mysql_config' is currently not installed. You can install it by t yping: sudo apt-get install libmysqlclient-dev
  • George Stocker
    George Stocker about 11 years
    The last step doesn't work for Mac OS X clients, as they don't have apt-get.
  • user798719
    user798719 almost 11 years
    the key is apt-get install python-dev. No idea why I'm installing a dev version of python 2 years later, but this works as of today using python 2.7.3. Only 3 hours wasted.
  • Siwei
    Siwei almost 11 years
    should add this answer to the django official document
  • andilabs
    andilabs over 10 years
    easy_install MySQL-python on Mac OS X Mountain Lion 10.8.4 was what I needed to get rid off: django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
  • filistea
    filistea over 10 years
    I've tried this and still get the same error: 'raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb'
  • napstercake
    napstercake almost 10 years
    I think I'm the unique person with this error 'Command python setup.py egg_info failed with error code 1' after run 'pip install MySQL-python'
  • dobs
    dobs almost 10 years
    Thanks! You need to install MySQLdb with the pip command installed with the virtualenv. - Helps
  • S..
    S.. over 9 years
    remember to 'sudo service apache2 restart' for these changes to take affect
  • theblackpearl
    theblackpearl over 9 years
    Thanks. sudo ./bin/pip install mysql-python is very helpful!
  • Ryan Walton
    Ryan Walton over 9 years
    Thanks. This worked to fix the issue for me with heroku and cleardb:ignite on django 1.7.1.
  • LearnAWK
    LearnAWK over 8 years
    This method solved my problem. All other suggestions before give me an error message of Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/r8/c_tscq492sn641w5vtvqcvkw0000gp/T/pip‌​-build-JYY1du/MySQL-‌​python
  • Paullo
    Paullo over 8 years
    Thanks Greatly, try all stated above. None worked on OSX 10.11 expect this
  • hudolejev
    hudolejev about 8 years
    CentOS 7, option 1: yum install MySQL-python; option 2 (if using virtualenv): yum install mysql-devel; venv/bin/pip install MySQL-python.
  • A Star
    A Star over 7 years
    this sorted it for me, as i believe this is the version to use for Python 3
  • Stony
    Stony over 6 years
    @LearnAWK it doesn't work. I still get the error Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/3m/97mbsh5x2bnghhk55130bh_h0000gn/T/pip‌​-build-FFU1vL/MySQL-‌​python/
  • Kamaldeep singh Bhatia
    Kamaldeep singh Bhatia about 6 years
    I tried this : stackoverflow.com/questions/25459386/… but failed, this saved me.. (Y)