In PHP, the __DIR__ magic constant evaluates to the path to the directory containing the file in which that constant appears.
Is there an equivalent feature in Python?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Python 3.4 and newer, that’s it – you get an absolute path.
In earlier versions of Python,
__file__refers to the file location relative to the cwd at module import time. If you callchdir, the information will be lost. If this becomes an issue, you can add the following to the root of your module:But again, if you only target Python 3.4+, this is no longer necessary.