How can I get the absolute path of an imported module?
Share
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.
As the other answers have said, you can use
__file__. However, note that this won’t give the full path if the other module is in the same directory as the program. So to be safe, do something like this:Here’s an example with a module I made called
checkIPto illustrate why you need to get the abspath (checkIP.pyis in the current directory):