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.