Django: Better way to install django apps into apps. (Handling django projects dependencies.)

Let's talk about common programming practice. You love to code yourself. Everyday you write tons of code. Making lots of common tasks is interesting and challenging. No?
A-ha! You love plugins too! :) I'm making a startup. Everyday there are some ideas coming to my head. Some are easy to implement, some are not, but I often download tons of plugins.
There are lots of ways to use plugins without installing them to main system. Python virtualenv is easy and common to use for this purposes. Soon I'll write an article about it too. But the main problem of Python Virtualenv is it's complexity. You basically need to install something, make an environment and so on... What if you just need to implement nice and easy "Django way".
Here direct copy comes in mind. It's the easiest way, as for me.
Basic concept is:
   - download plugin (be it in egg, zip, tar.gz or any other format)
   - unpack it 
   - copy to django app dir (simple drag and copy files to eclipse in my case)
   - modification (and/or any App post setup procedures)
Thats ALL!

Lets go into this way more deeply. Lets install django-pagination into my app.
First wee need to check plugin manual. Let's download it from google code HERE.
Unpack It. If you've done it in a period near to writing this article you'll probably see something like this.

We see a list of folders, containing one, like "docs" and a bunch of setup scripts like "", "setup.cfg" and so on. "Docs/install.txt" says about different install methods.
But you don't have to install it in order things to work properly!
The simplest way is to copy this main app "pagination" (it is usually a first dir in a tree containing "" file) into your project or app dir, containing "".  This way you can see apps or plugins, like they where installed on a python path. 
Setup script usually does this for you, but installing this directory to your system (or virtualenv) python path. And what if you'd like any other version of this django app installed? 
There some other thoughts here also. You may create dir, like "plugins" and copy installed plugins there. But i would not recommend this. You usually need up to 10 plugins per project. Why bother separating them? Bad side here is that we need to do some inside plugin modifications because you're renaming main plugin directory path. So it's possible, but unlikely simpler...

Now we must do plugin setup inside our App. You unfortunately can not escape this. :)
So let's make steps 1 and 2 from "docs/usage.txt" in our plugin dir. Let's add 'pagination', app to INSTALLED_APPS in our and add a middleware 'pagination.middleware.PaginationMiddleware', string to our MIDDLEWARE_CLASSES.

Let's add request to our TEMPLATE_CONTEXT_PROCESSORS too (if it isn't already there).
It will look something like so:

And thats pretty much all. Now we can add {% load pagination_tags %} somewhere in our template and use  {% autopaginate object_list %} and then {% paginate %} father. (see "docs/usage.txt" of this app for more info)

Now your app is really installed. You can use common from my_app import my_function like in "standard" installation way. Nothing more to do. 

We worked out most common situation with django plugins or redistributable apps. Maybe to some complex python apps, like lets say PIL, this method would be unsuitable, BUT for most common "django app installation" cases it will work like a charm.
Main plus of this way, that you can have all code related to you App/project etc. stored in one place.
This code would not affect main system also. So if you decide to use older version of plugin (for older ver of django for e.g.), maybe you'll remember yourself reading this article :)...

Bad thing here, that if your app has some more dependencies, you have to install them somehow too. Maybe you have to change your new app code, if you choose to copy them to your main dir way. But it's in not so common "complex" cases.

Comments? Suggestions? Please drop me a comment about what you think here...


  1. How is this simpler than just using pip/virtualenv?

    Any sane developer should have those installed anyway which means the process for adding an app is:
    1. Activate your virtualenv
    2. Add the app name to requirements.txt
    3. pip install -r requirements.txts

  2. Well, virtualenv is cool. But key thing here that you must install something in your system (Virtualenv in this case). But I don't use virtualenv in my Eclipse projects. It's possible of course.
    I'm having all code installed in one place. It's IMHO major advantage. When I'll have system crash I don't have to remember what to recover. (Backup of only one Developer dir for e.g.)

    I can have many versions of plugins installed without virtualenv. (It's not the best thing, because virtualenv is cool :) see higher). But I'll need it for my deployment in near future.
    I had a number of conflicts, while installing OSQA.

    You can access and change plugin code however you like. And you can easily access it in Eclipse...

    BTW I'll publish an article about virtualenv very soon. I'm actually writing it right now. So check back later on. It's the first method I tried in my "programmers life"...

  3. Virtualenv is pretty nice, and you can use it with virtualenvwrapper that is amazing!!


Post a Comment

Popular posts from this blog

Django: Resetting Passwords (with internal tools)

Python converting PDF to Image

Django: Beautiful multiple files Upload Plugin using jQuery UI.