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?
views:
72answers:
2
A:
As far as I know, it's just a matter of personal taste. I go with the directory which contains the project, but that's just my preference.
Dominic Rodger
2010-09-16 12:42:12
Doesn't it make the apps non-portable? I mean, in this case the apps will always need to be aware of which package they are living under, no?
cool-RR
2010-09-16 12:44:35
If apps don't live under the project directory they need to on the sys.path to use them in the project, if you put them as modules inside of project it is enough to have the project in sys.path, also apps should never need to know in what project (project is the package) they are in in order to be portable, it's the project that should know how to find apps not other way around.
rebus
2010-09-16 13:16:08
If an app wants to import stuff from the app directory without knowing the project directory, they can use the new-style `from . import modulename` syntax. (Requires python 2.5 or higher, I think.)
apenwarr
2010-09-16 23:42:30
+1
A:
sys.path
should and will have the directory of the project. Depending on what your setup is, it may also contain the directory which contains the project.
However, if the motivation behind this question is to ensure that certain files can be found, then you should note that sys.path
is just like a normal list and can be appended to. Therefore, you can add a new location to sys.path
like so:
sys.path.append('/home/USER/some/directory/')
where your files can be found.
Hope this helps
inspectorG4dget
2010-09-16 12:49:38
*it may also contain the directory which contains the project*. But that would mean there would be two different identities to objects defined in the apps: http://bugs.python.org/issue9872
cool-RR
2010-09-16 12:56:46