On Feb 24, 11:08 am, Michael Hipp <[EMAIL PROTECTED]> wrote:
> Malcolm Tredinnick wrote:
>
> > On Sat, 2008-02-23 at 16:02 -0600, Michael Hipp wrote:
> >> Where in my Django code files can I set the current working directory
> >> (so that it applies to all my code)?
>
> >> I'm trying to make sure that all paths in my Python code are relative
> >> paths. But I think I need to know where I can put the cwd change so that
> >> it runs when Django first comes up.
>
> > No, you don't. What you want to do is set the Python path variable in
> > your environment. Have a look at Django's documentation on mod_python
> > setup to see how to set that environment variable. That's the standard
> > way to do this.
>
> I think we're talking about two different things. I have the PythonPath
> variable set in my httpd.conf file. No problem with imports.
>
> I wasn't speaking of imports.
>
> But I have lots of "data" files that live in and around my Django code
> and I have to access with them with stuff like:
>
>     f = open("somedir/myfile.dat", 'r')
>
> So how do I make those lines look like that instead of having a bunch of
>   absolute paths stuck in there or lots of messy stuff with
> os.path.join(os.path.dirname(__file__)...) in it?

Don't. You should never ever write a web application such that it will
only work if the current working directory is a specific directory, it
is just very bad practice to do so.

This is because in hosting systems like Apache there is absolutely no
guarantee that the working directory will be something in particular
and you can't even change it to be a specific value and expect it to
work reliably. This is because in any sort of system where the process
is shared by other code you didn't write or which is outside of your
control, you can't prevent that other code also wanting to change the
working directory and subsequently screwing up your code.

Python web frameworks or applications which have to one degree or
another made a choice to somehow rely on the current working directory
being a specific value are limiting the deployment choices for their
users by doing so. You are thus highly discouraged against doing it
and using __file__ as an anchor or a variable setting in main settings
configuration file is a better and more portable way of doing it.

Graham
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to