Getting script folder path in High Level Analyzer Python script?

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 ?

@Tim, Thanks for letting us know your need for this! This is quite a unique use case. I’ve got this on our software team’s backlog to review. We’ll follow up with you soon.

I think this sort of feature is something that potentially opens a lot of doors. Being able to save and load configuration data is the tip of the iceberg. How about being able to save key metrics recorded in one session then compare them in a following recording? How about averaging some interesting values over multiple recordings? Find the minimum and maximum values for a batch of recordings? Lots and lots of things I haven’t thought of yet.

As a general thing, the closer you can get the Python install to behave as a native install the easier it is for us to do cool stuff. Ideal of course would be to use a designated system Python if available. And yes, I know that way leads to a potential support nightmare. You can always quote: “Caveat emptor” or: “You made the bed, you lie in it” for those of us that create issues with a non-Saleae provided Python…

1 Like

Thanks for all the feedback! This was a quick change, the __file__ variable will be defined in the next release.

1 Like

Awesome, thanks a lot !

@Tim The the __file__ variable mentioned by Mark is now defined and ready!
https://www.saleae.com/downloads/