Django/Mysql on Mac OS X Mountain Lion

Log-like errors and solutions while starting a project in Django with Mysql Database:

Downloading Mysql for mac from:

http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.14-osx10.7-x86_64.dmg

Install mysql-5.6.14-osx10.7-x86_64.pkg and the .prefs file for system preference control

Installing django :

sudo pip install django

Installing MySQL-python :

sudo pip install MySQL-Python

ERROR:

/private/tmp/pip-build-root/MySQL-python/distribute-0.6.28-py2.7.egg

sh: mysql_config: command not found

Traceback (most recent call last):

File “<string>”, line 16, in <module>

File “/private/tmp/pip-build-root/MySQL-python/setup.py”, line 18, in <module>

metadata, options = get_config()

File “setup_posix.py”, line 43, in get_config

libs = mysql_config(“libs_r”)

File “setup_posix.py”, line 25, in mysql_config

raise EnvironmentError(“%s not found” % (mysql_config.path,))

EnvironmentError: mysql_config not found

—————————————-
Cleaning up…
Command python setup.py egg_info failed with error code 1 in /private/tmp/pip-build-root/MySQL-python
Storing complete log in /Users/sbeta/Library/Logs/pip.log

Solution:

export PATH=$PATH:/usr/local/mysql/bin

add this line to .bashrc of your current user for permanent solution (nano ~/.bashrc)

testing if django is installed properly:

python -c "import django; print(django.get_version())"
Respond:
1.6

creating the project for django:

django-admin.py startproject mysite

try it out:

python manage.py runserver

Respond:

Validating models…

0 errors found
November 26, 2013 – 01:03:07
Django version 1.6, using settings ‘InstaGate.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Create your database with whatever tools you need, some prefer the mysql command line but I use Sequel Pro cause it’s easy to do and for my project I don’t need that much database administration. I may go with PhpMyAdmin later on.

adding the database detail to settings.py

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’, # Add ‘postgresql_psycopg2’, ‘postgresql’, ‘mysql’, ‘sqlite3’ or ‘oracle’.
‘NAME’: ‘DBNAME’, # Or path to database file if using sqlite3.
‘USER’: ‘USER’, # Not used with sqlite3.
‘PASSWORD’: ‘PASSWORD’, # Not used with sqlite3.
‘HOST’: ”, # Set to empty string for localhost. Not used with sqlite3.
‘PORT’: ”, # Set to empty string for default. Not used with sqlite3.
}
}

(Also change the timezone to your timezone)

run the server one more time.

ERROR:

Traceback (most recent call last):
File “manage.py”, line 10, in <module>
execute_from_command_line(sys.argv)
File “/Library/Python/2.7/site-packages/django/core/management/__init__.py”, line 399, in execute_from_command_line
utility.execute()
File “/Library/Python/2.7/site-packages/django/core/management/__init__.py”, line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/Library/Python/2.7/site-packages/django/core/management/base.py”, line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File “/Library/Python/2.7/site-packages/django/core/management/base.py”, line 280, in execute
translation.activate(‘en-us’)
File “/Library/Python/2.7/site-packages/django/utils/translation/__init__.py”, line 130, in activate
return _trans.activate(language)
File “/Library/Python/2.7/site-packages/django/utils/translation/trans_real.py”, line 188, in activate
_active.value = translation(language)
File “/Library/Python/2.7/site-packages/django/utils/translation/trans_real.py”, line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File “/Library/Python/2.7/site-packages/django/utils/translation/trans_real.py”, line 159, in _fetch
app = import_module(appname)
File “/Library/Python/2.7/site-packages/django/utils/importlib.py”, line 40, in import_module
__import__(name)
File “/Library/Python/2.7/site-packages/django/contrib/admin/__init__.py”, line 6, in <module>
from django.contrib.admin.sites import AdminSite, site
File “/Library/Python/2.7/site-packages/django/contrib/admin/sites.py”, line 4, in <module>
from django.contrib.admin.forms import AdminAuthenticationForm
File “/Library/Python/2.7/site-packages/django/contrib/admin/forms.py”, line 6, in <module>
from django.contrib.auth.forms import AuthenticationForm
File “/Library/Python/2.7/site-packages/django/contrib/auth/forms.py”, line 17, in <module>
from django.contrib.auth.models import User
File “/Library/Python/2.7/site-packages/django/contrib/auth/models.py”, line 48, in <module>
class Permission(models.Model):
File “/Library/Python/2.7/site-packages/django/db/models/base.py”, line 96, in __new__
new_class.add_to_class(‘_meta’, Options(meta, **kwargs))
File “/Library/Python/2.7/site-packages/django/db/models/base.py”, line 264, in add_to_class
value.contribute_to_class(cls, name)
File “/Library/Python/2.7/site-packages/django/db/models/options.py”, line 124, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File “/Library/Python/2.7/site-packages/django/db/__init__.py”, line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File “/Library/Python/2.7/site-packages/django/db/utils.py”, line 198, in __getitem__
backend = load_backend(db[‘ENGINE’])
File “/Library/Python/2.7/site-packages/django/db/utils.py”, line 113, in load_backend
return import_module(‘%s.base’ % backend_name)
File “/Library/Python/2.7/site-packages/django/utils/importlib.py”, line 40, in import_module
__import__(name)
File “/Library/Python/2.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(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Library/Python/2.7/site-packages/_mysql.so
Reason: image not found

Solution:

Run and add this line to .bashrc:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

as far as the tuterial on django site goes now we should run

python manage.py migrate

However there is no migrate option. (In case you need south you have to install it now : pip install south)

instead of migrate you can run this:

python manage.py syncdb

Respond:

Creating tables …
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session

You just installed Django’s auth system, which means you don’t have any superusers defined.
Would you like to create one now? (yes/no): no
Installing custom SQL …
Installing indexes …
Installed 0 object(s) from 0 fixture(s)

So now everything is set to start! you can start adding apps or projects in the same directory as manage.py. now you have to create your database model and all that I may write in my next blog, if I don’t get lazy about writing it down. 😉

Let me know if this helped, it would be a great motivation for me to write more often!

%d bloggers like this: