os.getcwd() frente a os.path.abspath(os.path.dirname(__file__))

I am using the os module to have relative paths in my Django projects settings.py file. The variable SITE_ROOT is set to the current working directory of the settings.py file and then used to reference all of the static/media directories also located in that same directory.

Heres my issue:

print os.getcwd()
print os.path.abspath(os.path.dirname(__file__))

In settings.py, the above statements both have identical outputs. but my template will only load if I use SITE_ROOT = os.path.abspath(os.path.dirname(__file__))

Django looks for the templates here:

    os.path.join(SITE_ROOT, 'templates'),

SITE_ROOT set to os.getcwd() seems to make Django look for the templates folder in the directory ABOVE the settings.py presentar

I can just as easily not use os.getcwd() and my site runs fine, but I am curious what may be going on here :)

¿Nadie sabe?

preguntado el 30 de junio de 12 a las 13:06

Current working directory and __file__ are unrelated. The first can change, depending on where you run your program from and whether you use os.chdir within, the other depends only where the module is placed in the filesystem. -

4 Respuestas

As mouad said, os.getcwd() won't give you exactly what you're expecting.

os.getcwd() does a bit more than returning the current working directory. It defaults to $PWD in your env. It's not where the script is located but where you were when you ejecutado la secuencia de comandos.

Estar en /home/user y haciendo python manage.py, os.getcwd() regresará /home/user Estar en /home/ y haciendo python user/manage.py, os.getcwd() regresará /home

But it's still won't be always true since it's possible to use os.chdir(). It is in other word like doing cd. It will also change the return value of os.getcwd().

Por otra parte. __file__ is the path of the module file. So you have to use this to be certain to have a path relative to your module instead of the current working directory that may change.

As ShawnFumo said, __file__ might not be always absolute. To get a better idea on how it works, you can check that answer: Python __file__ atributo. Also, as of Python3.4 __file__ should always be an absolute path.

Respondido 18 ago 18, 13:08

AH ok, that makes perfect sense with where django is looking for the template. THanks - Darko

Keep in mind that what __file__ returns isn't always an absolute path so you should use os.path.abspath on it to be sure. - shawnfumo

@Loic Faure-Lacroix: Nice explanation..Thank u :-) - mahendra kamble

El comando os.path.abspath(os.path.dirname(__file__)) returns the directory in which the code file is stored, but os.getcwd() gives you your current working directory which is by default where the code was executed, the latter can be changed using the os.chdir() mando.

Respondido el 30 de junio de 12 a las 14:06

os.getcwd()no give you the path where the settings.py is located rather it will give you the path from where the script (in your case manage.py) es ejecutado.

Respondido el 30 de junio de 12 a las 13:06

If the two statements you show truly have the same output, then either should work. So either: 1) they are subtly different, for example, one has a trailing slash, the other doesn't, or 2) you are testing in one environment and running in another.

Respondido el 30 de junio de 12 a las 13:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.