Get directory the add-on exists in

I’m currently writing an add-on and I would like to get the path the add-on is stored in so that I can create a log file in that directory containing relevant information of the internal things that my add-on is doing for debugging.

I’ve looked into __name__ and os.getcwd() but none of these get the directory of the add-on. The following is a minimal working example that I’m using to check that I’m actually getting the directory the add-on exists in (sharing it just in case anyone is interested in experiment with them).

import os
import aqt

def my_function():
  message = [__name__, os.getcwd()]
  aqt.utils.showText('\n'.join(message))

my_action = aqt.qt.QAction("My new action", aqt.mw)
my_action.triggered.connect(my_function)
aqt.mw.form.menuTools.addAction(my_action)
import os
import pathlib

# a Path object pointing to the directory where your add-on file is located
pathlib.Path(__file__).parent.resolve()

# a string representing the directory's full path
str(pathlib.Path(__file__).parent.resolve())

# same as above
os.path.dirname(os.path.realpath(__file__))
3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.