Isotropix Forums

Get path to script run from shelf

Clarisse Scripting related topics

Get path to script run from shelf

Unread postby bvz2000 » Mon Sep 02, 2019 4:21 am

I am trying to programatically find the path to the script that is being run from a shelf button.

For my standalone scripts (run from the command line) I use the following:

Code: Select all
import inspect
module_d = os.path.split(inspect.stack()[0][1])[0]


In the above example I get a path to the currently running python script.

But when I run this same code inside of Clarisse, I get nothing.

For example, from the command line:

Code: Select all
print inspect.stack()


gives me:

Code: Select all
[(<frame object at 0x7eff97c67050>, '/dev/item/modules/item/librarian/librarian.py', 55, '__init__', ['        print inspect.stack()\n'], 0), (<frame object at 0x564b3f746570>, '/dev/item/bin/item-publish', 175, '<module>', ['        librarian = librarian.Librarian(opts.language)\n'], 0)]


but running in Clarisse I get:

Code: Select all
[(<frame object at 0x7fe3c574c050>, '<string>', 23, '<module>', None, None)]


Is there any way I can access the current directory of the python shelf script running?

Thanks.
bvz2000
 
Posts: 427
Joined: Thu Nov 13, 2014 6:05 pm

Re: Get path to script run from shelf

Unread postby dcourtois » Mon Sep 02, 2019 9:03 am

Not that I know of. The reason is that we're executing from strings (when you create a script using the script editor, you can execute it even if it has not been saved to disk, so this is why we do it like this I guess)

What are you trying to do ? There might be some workarounds.
User avatar
dcourtois
 
Posts: 100
Joined: Tue Jul 25, 2017 3:15 pm

Re: Get path to script run from shelf

Unread postby bvz2000 » Mon Sep 02, 2019 8:53 pm

I often write scripts that refer back to configuration files that are stored in a relative path to the script. Without knowing where the script is running from, I cannot get the relative path to the configuration file.

That said, this is not a real big deal. What I am currently doing as a workaround is to have my shelf script calling a separate script which IS able to know where it is on disk (and as a result, refer to its configuration file).

In fact, from an organizational standpoint, this is even a better way of working. So... if you have a simple answer I'd be happy to hear it. But from an importance standpoint, it really isn't a big deal.
bvz2000
 
Posts: 427
Joined: Thu Nov 13, 2014 6:05 pm

Re: Get path to script run from shelf

Unread postby dcourtois » Tue Sep 03, 2019 10:19 am

Glad you found a better workaround :) And nope, I don't think there's a better way of doing this.
User avatar
dcourtois
 
Posts: 100
Joined: Tue Jul 25, 2017 3:15 pm


Return to Scripting