Friday, December 16, 2011

tofigureout: Django apps prefix

If all of all my Django apps are in the "apps" dir, is there something I can set so that I can directly write the app name instead of "apps.app_name" everytime.

It seems like I can do this only with sys.path or PYTHONPATH, which applies to all the django projects in the environment. This doesn't seem like a satisfactory solution. ... my summer project MozSecWorld didn't have this problem, I wonder what magic the setup for that did.

Setting up new Django project (with virtualenv(wrapper))

Download Django for Ubuntu in root:

1. cd /usr/local/lib, mkdir django-1.3.1 and cd into it

2a. wget http://www.djangoproject.com/download/1.3.1/tarball/ -O Django-1.3.1.tar.gz, got this step from here

2b. cd Django-1.3.1.tar.gz

2c. sudo python setup.py install

So my Django path is:
/usr/local/lib/django-1.3.1/Django-1.3.1

In user:
1. mkvirtualenv --no-site-packages vocab this is virtualenvwrapper setting up a new environment. Setup is logged in this old post.

$ mkvirtualenv --no-site-packages vocab
New python executable in vocab/bin/python
Installing setuptools............done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/preactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/postactivate
virtualenvwrapper.user_scripts creating /home/my_username/.virtualenvs/vocab/bin/get_env_details
mkdir: cannot create directory `/home/my_username/Projects/vocab': No such file or directory
-bash: cd: /home/my_username/Projects/vocab: No such file or directory
Searching for readline
Reading http://pypi.python.org/simple/readline/
Reading http://www.python.org/
Reading http://github.com/ludwigschwardt/python-readline
Best match: readline 6.2.1
Downloading http://pypi.python.org/packages/source/r/readline/readline-6.2.1.tar.gz#md5=9604527863378512247fcaf938100797
Processing readline-6.2.1.tar.gz
Running readline-6.2.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-9h67Mv/readline-6.2.1/egg-dist-tmp-nNoSrj
/usr/bin/ld: cannot find -lncurses
collect2: ld returned 1 exit status
error: Setup script exited with error: command 'gcc' failed with exit status 1
Downloading/unpacking Jinja2==2.3.1
  Downloading Jinja2-2.3.1.tar.gz (428Kb): 428Kb downloaded
  Running setup.py egg_info for package Jinja2
    
    warning: no previously-included files matching '*' found under directory 'docs/_build/doctrees'
Downloading/unpacking MySQL-python==1.2.3c1
  Downloading MySQL-python-1.2.3c1.tar.gz (89Kb): 89Kb downloaded
  Running setup.py egg_info for package MySQL-python
    
Downloading/unpacking lxml==2.2.6
  Downloading lxml-2.2.6.tar.gz (2.9Mb): 2.9Mb downloaded
  Running setup.py egg_info for package lxml
    Building lxml version 2.2.6.
    NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' needs to be available.
    ERROR: /bin/sh: xslt-config: not found
    
    ** make sure the development packages of libxml2 and libxslt are installed **
    
    Using build configuration of libxslt
    
    warning: no previously-included files found matching '*.py'
Downloading/unpacking PIL
  Downloading PIL-1.1.7.tar.gz (506Kb): 506Kb downloaded
  Running setup.py egg_info for package PIL
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py
    
Obtaining check from git+git://github.com/jbalogh/check.git#egg=check
  Cloning git://github.com/jbalogh/check.git to /home/my_username/.virtualenvs/vocab/src/check
  Running setup.py egg_info for package check
    
Downloading/unpacking pyflakes (from check)
  Downloading pyflakes-0.5.0.tar.gz
  Running setup.py egg_info for package pyflakes
    
Downloading/unpacking pep8 (from check)
  Downloading pep8-0.6.1.tar.gz
  Running setup.py egg_info for package pep8
    
Downloading/unpacking path.py (from check)
  Error  while getting http://pypi.python.org/packages/source/p/path.py/path.py-2.2.2.tar.gz#md5=3a3262b9049d294ddbd6f7453d45e699 (from http://pypi.python.org/simple/path.py/)
Exception:
Traceback (most recent call last):
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/basecommand.py", line 126, in main
    self.run(options, args)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/commands/install.py", line 223, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 961, in prepare_files
    self.unpack_url(url, location, self.is_download)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 1079, in unpack_url
    return unpack_http_url(link, location, self.download_cache, only_download)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 429, in unpack_http_url
    resp = _get_response_from_url(target_url, link)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 465, in _get_response_from_url
    resp = urlopen(target_url)
  File "/home/my_username/.virtualenvs/vocab/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/download.py", line 84, in __call__
    response = urllib2.urlopen(self.get_request(url))
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1185, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1160, in do_open
    raise URLError(err)
URLError: 

Storing complete log in /home/my_username/.pip/pip.log

2. Since I installed with no-site-packages, I need to add Django to the virtualenv
add2virtualenv /usr/local/lib/django-1.3.1/Django-1.3.1

3. follow the Django tutorial

3b. In python manage.py sql app_name I got this error:
...
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
so I did
pip install MySQL-python

3c. I installed my app inside an "apps" folder, so here is what I did:
mkdir apps; touch apps/__init__.py
python manage.py startapp app_name
mv app_name apps
vim settings.py and add into INSTALLED_APPS 'apps.apps_name'
python manage.py sql app_name
Note that you should have 'apps.app_name' in INSTALLED_APPS or else it's not going to find it and give you this error instead:
Error: No module named app_name