In my previous post I talk about reusable apps, but I don’t really explain it that much. If you have an app that might be useful in another project, it’s best to not refer to the project name in the application so you don’t have to search and remove it when adding to another project. To never refer to your project name in your app’s code, you will need to put your app on the python path. I usually do project_folder/apps/app_folder so apps will need to be a location that python is checking when you are importing so that importing looks like the following:
from appname.filename import foo
There are a few places you might need to add an apps folder to the pythonpath.
Add to settings.py
This will add the apps directory in your project directory to the beginning of the path list. This will allow manage.py syncdb and manage.py runserver to know that the apps folder should be added.
import os import sys PROJECT_ROOT = os.path.dirname(__file__) sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))
That should be all you need to do to get most django projects working with your reusable apps, but if for any reason you need add to the path with mod python or mod wsgi, the following should work.
In the setting-up-everything post I show an example httpd.conf file. In your apache configuration you will probably see something similar to what is below. To add the location /var/www/myproject/apps to the PythonPath I added it in the list.
SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE myproject.settings PythonOption django.root /myproject PythonDebug On PythonPath "['/var/www','/var/www/myproject/apps'] + sys.path"
If you use mod wsgi instead of mod python, your apache config will be loading a wsgi file with a line like this WSGIScriptAlias /var/www/myproject/myproject.wsgi. You will need to edit that file to add to the path (django’s site has an example file).
You never know when you might want to use an app in another project, so always try to keep from mentioning the project name anywhere in the applications.
- Python Projects in Users’ Home Directories with wsgi Letting users put static files and php files in a public_html folder in their home directory has been a common...
- Getting Basecamp API Working with Python I found one library that was linked everywhere, but it wasn’t working for me. I was always getting 400 Bad...
- How to Write Reusable Apps for Pinax and Django Pinax is a collection of reusable django apps that brings together features that are common to many websites. It...