What is `sys.path` supposed to be?

8 views
Skip to first unread message

cool-RR

unread,
Sep 16, 2010, 12:06:57 PM9/16/10
to Django users
Hello!

There's something that's bothering me:

When developing a Django application, what is `sys.path` supposed to
contain? The directory which contains the project, or the directory of
the project, or both?

What led me to this question is a failure when using
`urlresolvers.reverse`. It's giving me a `NoReverseMatch` error, and
it seems that the reason is that there are two different versions of
my view function; One with a `__module__` of `my_app.views` and one
with a `__module__` of `my_project.my_app.views`. Because of Python
weirdness, these two different versions of the same function are not
considered identical to each other:

http://bugs.python.org/issue9872


Please help...


Ram.

bruno desthuilliers

unread,
Sep 17, 2010, 4:13:27 AM9/17/10
to Django users
On 16 sep, 18:06, cool-RR <ram.rac...@gmail.com> wrote:
> Hello!
>
> There's something that's bothering me:
>
> When developing a Django application, what is `sys.path` supposed to
> contain? The directory which contains the project, or the directory of
> the project, or both?

sys.path always starts with the directory where the current script
lives - so when running './manage.py runserver', sys.path will always
starts with your project's directory. This is teh default Python
behaviour. Also and IIRC, django adds the project's parent directory
to sys.path too.

> What led me to this question is a failure when using
> `urlresolvers.reverse`. It's giving me a `NoReverseMatch` error, and
> it seems that the reason is that there are two different versions of
> my view function; One with a `__module__` of `my_app.views` and one
> with a `__module__` of `my_project.my_app.views`.

I bet you have a full import (=> 'myproject.my_app.views') somewhere
in your sources.

As far as I'm concerned, I *never* use any reference to the project's
package in my imports - this gives much more flexibility (you can
rename your project's directory, move apps outside the project's
directory etc without having to update all your imports). The only
caveat is that, when deploying with mod_wsgi, you have to make sure
you add the project's directory itself to sys.path (which is no bige
deal).

cool-RR

unread,
Sep 17, 2010, 7:04:35 AM9/17/10
to Django users
On Sep 17, 10:13 am, bruno desthuilliers
Bruno, look at this line in `django/core/management/__init__.py`:

project_module = import_module(project_name)

It's importing the project. Isn't this a problem? I mean, every object
obtained from the imported project will be different from its
counterpart which was imported through an app. So what's going on?


Ram.
Reply all
Reply to author
Forward
0 new messages