Where to set the cwd?

149 views
Skip to first unread message

Michael Hipp

unread,
Feb 23, 2008, 5:02:04 PM2/23/08
to django...@googlegroups.com
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.

I'm on Apache and mod_python on Windows.

Thanks,
Michael

Malcolm Tredinnick

unread,
Feb 23, 2008, 6:01:36 PM2/23/08
to django...@googlegroups.com

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.

Malcolm

--
Save the whales. Collect the whole set.
http://www.pointy-stick.com/blog/

Michael Hipp

unread,
Feb 23, 2008, 7:08:45 PM2/23/08
to django...@googlegroups.com
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?

Thanks,
Michael


Graham Dumpleton

unread,
Feb 23, 2008, 7:20:17 PM2/23/08
to Django users
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

Drew Raines

unread,
Feb 23, 2008, 7:35:52 PM2/23/08
to django...@googlegroups.com
Michael Hipp wrote:

[...]

> 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 use specific paths throughout your code. Set variables in
settings.py and scatter those around.

To avoid the proliferation of os.path.* everywhere, I use something
like this in my settings files:

import os
HOME = os.path.abspath(os.path.dirname(__file__))

TEMPLATE_DIRS = (
HOME + "/templates",
)

UPLOADS = HOME + "/files"

and so on.

-Drew

Michael Hipp

unread,
Feb 23, 2008, 7:42:07 PM2/23/08
to django...@googlegroups.com

Ok. Thanks. The idea of putting it in settings.py is probably the path
of least litter.

Michael

Michael Hipp

unread,
Feb 23, 2008, 7:43:37 PM2/23/08
to django...@googlegroups.com

Me like. Thanks.

Michael

Michael Hipp

unread,
Feb 25, 2008, 10:43:40 AM2/25/08
to django...@googlegroups.com

Just to close the loop on this, Graham's explanation makes perfect sense.

But why are these instructions even included in the book and in the
documentation?

------
http://www.djangoproject.com/documentation/fastcgi/
http://www.djangobook.com/en/1.0/chapter20/

'Running Django on a shared-hosting provider with Apache'

"Create a file mysite.fcgi ..."

# Switch to the directory of your project. (Optional.)
# os.chdir("/home/user/myproject")
------

Shouldn't this be removed (or at least padded with some warnings) since
it is arguably encouraging a (new?) user to do something that is poor
practice or even dangerous.

Thanks,
Michael

Reply all
Reply to author
Forward
0 new messages