This is the fifth and final post in a dummies guide to getting stared with Python, Django, & MySQL on Windows 7.
- Part 1: getting started
- Part 2: virtual environments
- Part 3: making iPython recognize virtual environments
- Part 4: installing Django
- Part 5: installing MySQL (you are here)
By now, you should have Django installed into a virtual environment. These tutorials aren’t meant to cover building a django app, just to point out the quirks involved with getting a project up and running on Windows. These tutorials also assume you want to construct real applications using a real development environment.
To that end, you’ll want a heftier database than sqlite. We use MySQL at the office, so these instructions cover installing it and using it with Django.
- Download and install MySQL.
- Once MySQL is installed, proceed through the configuration wizard. Check Include Bin Directory in Windows PATH box.
- When prompted, set a password for the MySQL root account.
- Once the installation wizard is done, open a command window and log in to MySQL with the root account:
mysql -uroot -p(you’ll be prompted for the password).
- After logging in, run the following commands to create a database, create a user for your Django project, and grant the user database access.
You’ll need the MySQL-python package, a Python interface to MySQL.
- Download the windows MySQL-python distribution here. The author has some instructions about the appropriate version; assuming a 32-bit version of Python 2.7, you’d download this package (.exe).
- After downloading, do not run the Windows installer. Doing so will install MySQL-python to your root python, which virtual environments created via –no-site-packages won’t be able to see.
- Instead, install the downloaded package to your virtual environment by using easy_install, which can install from Windows binary installers:
easy_install file://c:/users/you/downloads/mysql-python-1.2.3.win32-py2.7.exe(modify to reflect the location of the downloaded installer and its name).
Next, you’ll need to update the database-related settings of your Django project.
- From the directory of your Django project, open settings.py using your favorite editor.
- Update the default key in the DATABASES dictionary. Set ENGINE to django.db.backends.mysql and set NAME, USER, and PASSWORD to the database name, username, and password you chose when installing MySQL. See Part I of the Django tutorial for more information about database settings.
- Open a command window, activate your virtual environment, and change to the directory of your Django project.
python manage.py syncdb. This command creates the underlying tables required for your Django project.
- If the syncdb worked, you have Python, Django, and MySQL communicating in harmony. Congratulations! You can now proceed through the Django tutorial and create your first application.