Hi,
TL;DR: How to get the path to the Python script folder from a High Level Analyzer script ? Usual Python approaches seem to fail.
Full:
I’m working on a High Level Analyzer and I want to make it a bit more customizable. The Analyzer is working well and it was fun to develop it, thanks for this awesome tool and the flexibility it offers.
I want to add the possibility for the users to select an optional configuration file, which the script would then parse to add more information to the HLA. Of course I can do it by using a StringSetting parameter and asking them the path to the file (I did it, it works), but I was thinking it would be even a better experience if I can first parse the script folder to find configuration files, then propose the list in a ChoicesSetting parameter.
The problem is that I didn’t find a way to get the path to the Python script’s folder. This is normally quite basic, but here it doesn’t work.
If I try this:
print(os.path.dirname(os.path.realpath(__file__)))
I get this error:
NameError("name '__file__' is not defined")
For these ones:
print(sys.path[0])
print(os.getcwd())
I get a path in the Saleae install folder, which is not where my extension is (it’s deployed in a one of my company’s tool repository), e.g.
C:\Programs\Logic\resources\windows\pythonlibs\python38.zip
I also tried this:
import inspect
print(inspect.getframeinfo(inspect.currentframe()).filename)
And ended up with that:
NotImplementedError()
I suppose this is due to how the Python script is called, which I have no idea. Writing the analyzer went smoothly, but I’m stuck on that part.
Is there any way I can get the absolute path to my Python High Level Analyzer script, from within the script ?