I have been trying to figure out how to consistently get an absolute path to the directory of one of my modules so that I can open files in that same directory, regardless of whether the module is being run directly or being imported from somewhere else.
Am I just misunderstanding the way os.path and _ file _ work, or is there some quirk in Django that is causing this?
views.py:
def pathtest(request):
return HttpResponse(mymodule.getpath())
mymodule.py:
def getpath():
return os.path.realpath(__file__)
Expected Output:
/home/user/projects/myproject/mymodule/mymodule.py
Actual Output:
/home/user/projects/myproject/views.py
Can you do this?
mymodule.py: