Adjusting PYTHONPATH for reusable django apps

1,779 views
Skip to first unread message

Opel

unread,
Dec 2, 2008, 6:07:57 PM12/2/08
to Django users
I have been following along to the excellent James Bennet Book,
Practical Django Projects and he suggests creating apps in a directory
that can be reusable for local development.

I am working on OSX (tiger and Leopard on different machines) and I
have created a Django director inside my /User/Sites folder. Al m
django projects will be developed here and as per James suggestion I
was going to create and "Apps" sub directory i.e /Users/Sites/Django/
Apps/

I have searched the web and even tried to use the pylink tool but I
cannot get my Python path to recognise this new "Apps" folder.
Whenever I try to include an app in my INSTALLED_APPS I get "module
not found error"

Could anyone please help out or post a link to a tutorial please.

Thanks

Dan

unread,
Dec 2, 2008, 6:32:28 PM12/2/08
to django...@googlegroups.com
I prefer to to just put the apps in the project's folder and put *that* folder on the PYTHONPATH then move it elsewhere only when I find out I actually need it elsewhere. To make it more portable, I find out where my project is in my filesystem at runtime so if I move it around, it's no problem.

So, I put this code into the __init__.py of my project's folder:

from os.path import dirname
from sys import path

path.append(dirname(__file__))


sys.path is a list that holds everything that is in your PYTHONPATH
__file__ is a magic variable that holds where the file that's currently running is in the filesystem
and os.path.dirname extract the directory name in an OS independant way.

You can of course add any folder you want in your PYTHONPATH this way, not just the project's folder as I do.

Opel

unread,
Dec 2, 2008, 6:45:25 PM12/2/08
to Django users
Thanks for the reply I'll give that a try.

Just one question does that mean you make a structure like so ?

Project
settings.py
urls.py etc
__init__.py (this is where above code goes)
>>>apps
>>>>>blog
>>>>>>>__init__.py
>>>>>>>models.py
>>>>>>>views.py


so that means in your installed_apps you can just call : "
'apps.blog', "

cheers

Andy

James Bennett

unread,
Dec 2, 2008, 6:59:46 PM12/2/08
to django...@googlegroups.com
For what it's worth, I'm a huge fan of virtualenv:

http://pypi.python.org/pypi/virtualenv

and of Doug Hellmann's virtualenvwrapper:

http://www.doughellmann.com/projects/virtualenvwrapper/

What virtualenv does, basically, is create an isolated Python
environment into which you can install stuff without affecting any
other Python software on your system. And virtualenvwrapper provides
tools for easily creating, removing and working with virtualenvs.

So my workflow these days goes something like this:

mkvirtualenv somepythonproject

(this creates a new virtualenv, named "somepythonproject", and drops
me into it; if I come back later in another shell, "workon
somepythonproject" will put me back in the virtualenv)

From there I have easy_install for installing Python packages I want
to try out, or I can use the "add2virtualenv" function in
virtualenvwrapper to add a directory to the virtualenv's Python path.
For example:

add2virtualenv ~/dev/my-django-apps/

Within the virtualenv, everything I've installed or added to it is
available, but it doesn't affect anything else on my system; I can
blow it away with no repercussions if I mess something up, or keep
adding/tweaking stuff as needed.

It's even getting easier to do real deployment with this, too;
mod_wsgi supports a directive that lets you point it at a virtualenv
to run a site out of a completely-sandboxed environment, so you can
simply use normal tools within the virtualenv to set things up and not
worry about clashing with anything else.

If you haven't looked into this, I highly recommend it. And maybe in
the 2nd edition of the book I'll try to explain this a bit more
clearly...


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Dan

unread,
Dec 2, 2008, 7:02:48 PM12/2/08
to django...@googlegroups.com
That would work but I put them right at the root of my project, not in an app folder so I just add 'blog' to my INSTALLED_APPS. Every distributable app I saw is meant to be put somewhere directly in the PYTHONPATH and not in a subpackage so I follow that rule.

If you put them inside an folder name apps in your project directory to be more organized, you can do:

from os.path import dirname, join
from sys import path

path.append(join(dirname(__file__), "apps"))

You could always just do dirname(__file__) + "/apps" but os.path.join is os independant too and will join okay on Windows too.

By the way, I also use the __file__ trick to put my database directly in my project's folder no matter where in the filesystem the project is and likewise with my templates.

Opel

unread,
Dec 2, 2008, 7:05:53 PM12/2/08
to Django users
Thanks for both tips. James I will trial the virtual environment and
if I get stuck can use Dan's method.

Reply all
Reply to author
Forward
0 new messages