3

I am somewhat new to coding and wanted to start my first Django app with a MySQL backend. I've used this setup for almost a year now on my windows machine, but it was an inherited codebase - and I unfortunately never tried to build a new app from scratch. Furthermore, I recently purchased a used MacBook to familiarize myself with the OS and get more accustomed to using Unix (since all of our code runs from Ubuntu/RedHat servers) - so I'm still in the 'finding out how to get around town' phase.

Below are some of the difficulties/errors I ran into, and the answers that worked for me (and weren't necessarily that easy to find). This is not 'This is how you should solve this problem', but merely a repository for how I resolved some issues - and hopefully learn how I should have done it. Feel free to add more!

  • Change the default Python Version

    • I needed to use Python 2.4 for a short time. When I no longer needed to use Python 2.4 consistently, I wanted to change it back to Python 2.7.
      One way I was able to do this was to change my export via export PATH=/usr/bin:$PATH. This only worked for my current terminal shell - not that helpful.
      I finally found that changing the symlink in /Library/Frameworks/Python.framework/Versions/Current from -> 2.4 to 2.7 solved my problem. I tried ln -f -s 2.7/ Current but this didn't work. I ultimately moved my Current dir to Current_back then created the new symlink ln -s 2.7/ Current
  • MySQL

    • Problem 1: The installer couldn't find the correct path for MySQL_config "EnvironmentError: mysql_config not found" I found several people pointing out that you need to edit the 'setup_posix.py' file and set mysql_config.path = "/usr/local/mysql/bin/mysql_config" but I ultimately had to change this in the site.cfg file.
    • Problem 2: I consistently received a 'Library Not Loaded' error anytime I tried to import MySQLdb. To resolve this, I updated my bash profile and set DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
  • Django

    • Segregating and Separating models.py:
      I like putting things into containers, and I wanted to use multiple 'models' files in a 'models' directory rather than one large models.py file. The trick is, you need to inform everyone where the models are and how to access them.
    • Add an init.py file to your new 'models' directory. In this file, make sure you import all of the classes from your models. If you have data1.py and data2.py, you will need something like this in init.py:
      from data1 import *
      from data2 import *
    • Add the app name to the metadata for each model:

    o

    class Data(models.Model):
        foo = models.CharField(max_length=200)    
    
        class Meta:
            app_label = 'app_name'  
    
Esteban
  • 2,444
  • 2
  • 19
  • 19
  • 3
    Im sorry but is this a question or a blog post? – jdi Jan 30 '12 at 02:53
  • 1
    Instead of playing with the default python of your system; use [virtualenv](http://stackoverflow.com/questions/5844869/comprehensive-beginners-virtualenv-tutorial). This is a forum to ask questions; not discussions (as pointed out by @jdi). – Burhan Khalid Jan 30 '12 at 05:49
  • Just as a side note, I never saw this process for installing mysql be required in my experience. I can at least say its probably not a common case for people. – jdi Jan 30 '12 at 06:54
  • Thanks, I'll check out virtualenv to hopefully make things simpler going forward. Sorry about the format of the post - I'll keep it to questions going forward. – Esteban Jan 30 '12 at 08:11
  • @Esteban: I appreciate what you're trying to do, but StackOverflow is not for stuff like this. Get a blog from some place like Wordpress.com and go to town. It's not bad info, just wrong forum for it. – Chris Pratt Jan 31 '12 at 17:51

2 Answers2

2

Learn how to use Vagrant + Virtual Box, and develop your Django sites in the same environment as your production.

Alexander Artemenko
  • 21,378
  • 8
  • 39
  • 36
0
  1. Install Homebrew
  2. brew install python mysql
  3. easy_install pip
  4. pip install virtualenv virtualenvwrapper
  5. echo "source /usr/local/bin/virtualenvwrapper.sh > /dev/null" >> ~/.bash_profile
  6. source ~/.bash_profile
  7. mkvirtualenv --no-site-packages <your env name>
  8. pip install django
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Michael Samoylov
  • 2,933
  • 3
  • 25
  • 33