tags:

views:

32

answers:

1

I like Eclipse for a number of reasons. One of them is, when I'm doing a java project, I can copy and paste my project or rename it, to make a new one, and it will update all the names of everything.

I'm trying to do something similar for a django project.

I can set an APPNAME variable with the following code

APPNAME = os.path.basename(os.path.dirname(__file__))  

and then I could import this in any file that needed to know the name of the application. This isn't an ideal solution however but it would work for my settings.py file as well as my urls.py files.

It won't work for situations where I need to import something from somewhere like so:

from myproject.someapp import forms

Is there a way for django/python to know what "myproject" is? Or can I use an import statement relative to the current app?

Or maybe there's a better way to copy django projects.

EDIT: I imagine there are also database dependencies as well that I'd have to deal with.

+1  A: 

I follow a couple of rules to keep my applications portable. I'll list them below in the hope that someone finds them useful.

  1. Include my apps in the PYTHONPATH rather than my projects. This way I can execute from app import forms rather than from project.app ....
  2. Following #1, I always import from app only. This means I can reuse my apps in other projects without having to change import statements within the app or in other dependent apps.

If you stick to #1 and #2 you can generally copy and paste projects without too much trouble. You'll still have to modify settings.py though.

Manoj Govindan
where do you make the change to the PYTHONPATH? Thanks!
JPC
I read an article here: http://codespatter.com/2009/04/10/how-to-add-locations-to-python-path-for-reusable-django-apps/ but it says I need to make an "app" folder for my apps. I'd like to not do that, if possible.
JPC
Well I did this in my settings.py:PROJECT_ROOT = os.path.dirname(__file__)sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))and put all of my apps in a folder called apps so my project structure is djangoproject1 --> apps --> appnamebut none of my imports can be resolved when I do from appname import *
JPC
and the apps folder has an __init__.py file
JPC