Please explain why it os.path.dirname(__file__)works in Django but does not work in python? This is a strange situation that I cannot understand.
I made a python script and placed it in the test_file.py file:
import os
dirname = os.path.dirname(os.path.dirname(__file__))
realpath = os.path.dirname(os.path.realpath(__file__))
abspath = os.path.dirname(os.path.abspath(__file__))
print dirname
print realpath
print abspath
When I run the script, I get the results:
(env)user@ubuntubox:~/srv/django_pro$ python test_file.py
/home/srv/django_pro
/home/srv/django_pro
(env)user@ubuntubox:~/srv/django_pro$
I get results that realpath and abspath work , but dirname is empty. However, in Django settings.py, I have:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
If I go into python shell and print BASE_DIR, it will not be empty:
(env)user@ubuntubox:~/srv/django_pro$ python manage.py shell
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>>
>>> print 'BASE_DIR:\n%s' % settings.BASE_DIR
BASE_DIR:
/home/srv/django_pro
>>>
And that means os.path.dirname (os.path.dirname ( file )) is not empty. I use the same python in both cases. And my version of Django:
Django==1.7b4
So what is the trick?