You need to call os.path.realpath on __file__, so that when __file__ is a filename without the path you still get the dir path:
os.path.realpath
__file__
import os print(os.path.dirname(os.path.realpath(__file__)))
2.1m questions
2.1m answers
60 comments
57.0k users