Skip to content

Instantly share code, notes, and snippets.

@density215
Created July 19, 2012 19:42
Show Gist options
  • Save density215/3146297 to your computer and use it in GitHub Desktop.
Save density215/3146297 to your computer and use it in GitHub Desktop.
Django on OSX Snow Leopard with virtualenv

download

python 2.7 & mysql installation

  • install MacPython dmg

  • install MySQL dmg

  • verify ~/.profile

    $ export PATH="/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/:${PATH}" $ export PATH="/usr/local/mysql/bin:${PATH}"

  • make sure python shows:

Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type "help", "copyright", "credits" or "license" for more information.
sudo sh setuptools-0.6c11-py2.7.egg --install-dir=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
sudo /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/easy_install pip

Python generic packages

  • check pip --version shows

pip 0.8.2 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-0.8.2-py2.7.egg (python 2.7)

You need django outside of virtualenvirontment for django-admin

pip install ipython
pip install virtualenv
pip install django

Create virtualenv environment

I use a folder in ~/Django not in Sites (static only)

django-admin startproject mydjangoproject
virtualenv -vv --python=python2.7 mydjangoproject/env

you can also use --no-site-packages. Each virtualenv environment will only have access to its own packages.

cd mydjangoproject
pip -E env install django

should work, but due to some bug in pip it's better to use

source mydjangoproject/env/bin/activate
pip install django

again you can check with pip --version whether it uses the virtualenv created pip in env/bin

thanks to this article

You can re-install all modules and dependencies from another virtualenv by

copied-from virtualenv

pip freeze > deps.txt

copied-to virtualenv copy the deps.txt to new virtualenv

pip install -U -r deps.txt

Installing mysql-python inside virtualenv environment

still in (env)

pip install mysql-python

will get you this error

EnvironmentError: mysql_config not found


Command python setup.py egg_info failed with error code 1

Storing complete log in /Users/jasper/.pip/pip.log

change in mydjangoproject/env/build/mysql-python/setup_posix.py

mysql_config.path = "mysql_config"

in

mysql_config.path = "/usr/local/mysql/bin/mysql_config"

again run pip install mysql-python

Installing PIL inside virtualenv environment

still inside (env) type Freetype2 support

curl -O -L http://downloads.sourceforge.net/project/freetype/freetype2/2.4.4/freetype-2.4.4.tar.bz2
tar xjvf freetype-2.4.4.tar.bz2
cd freetype-2.4.4
./configure && make && sudo make install clean

found here

pip install PIL gives you:


PIL 1.1.7 SETUP SUMMARY


version 1.1.7

platform darwin 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)

             [GCC 4.2.1 (Apple Inc. build 5664)]

--- TKINTER support available

--- JPEG support available

--- ZLIB (PNG/ZIP) support available

*** FREETYPE2 support not available

*** LITTLECMS support not available


.... Successfully installed PIL Cleaning up...

Seems ok, check ./manage.py shell

from PIL import Image
i = Image.open('/Users/you/Picture/test.jpg')
i.save('/Users/you/test_save.jpg')

gives :

ImportError: The _imaging C module is not installed

hmm, import _imaging gives:

ImportError: dlopen(./_imaging.so, 2): Symbol not found: _jpeg_resync_to_restart

Referenced from: /Users/you/Django/youdjangoproject/env/lib/python2.7/site-packages/PIL/_imaging.so

Expected in: flat namespace

in /Users/you/Django/mydjangoproject/env/lib/python2.7/site-packages/PIL/_imaging.so

libjpeg has a problem.

tar -xzvf jpegsrc.v7.tar.gz
cd jpeg-7/
./configure --enable-shared --enable-static
make
sudo make install

found here and this comment about libjpeg7

Make django work with virtualenv

manage.py first line :

#!/usr/bin/env env/bin/python

install mod_wsgi with apache OSX

tar -xzvf mod_wsgi-3.3.tar.gz cd mod_wsgi-3.3 ./configure make sudo make install

[http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz] [http://jessenoller.com/2009/07/24/django-mod_wsgi-apache-and-os-x-do-it/]

Make virtualenv work with mod_wsgi

in httpd.conf (osx) :

# wsgi stuff
NameVirtualHost *:80
WSGIPythonHome /usr/local/pythonenv/BASELINE
Include "/Users/jasper/Sites/nlarchitects/conf/apache_django_wsgi.conf"
Include "/Users/jasper/Sites/hcg/conf/apache_django_wsgi.conf"

in mod.wsgi file in django project :

import site
site.addsitedir('/Users/jasper/Sites/nlarchitects/env/lib/python2.7/site-packages')

create new mysql database

if you come from linux osx is a bit peculiar in using logins for mysql.

It's sort of recommended to set the root password /usr/local/mysql/bin/mysqladmin -u root password sniggle sudo mysql -p

gives you root access.

in mysql :

create database DATABASE_NAME;
grant all privileges on 'DATBASE_NAME' to 'YOURUSERNAME' identified by 'YOURPASSWORD';
exit

creates database and username/password combo, except password don't work (whyowhy), set it with sequel pro (freeware) or other program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment