Home Download Buy Blog Forum Support

Current plugin folder path

Current plugin folder path

Postby ol_ol_3388 on Wed Feb 06, 2013 4:34 pm

Hello:

I'm writing my first ST plugin in python. There is a need to get a path to the folder where this plugin is installed. I can get path to package directory by doing:
sublime.packages_path(), but I do not want to append my plugin custom folder name to this path in order to get what I want.

Is there a way to get current/running/active plugin folder path in python ?
ol_ol_3388
 
Posts: 18
Joined: Mon Feb 04, 2013 3:29 pm

Re: Current plugin folder path

Postby facelessuser on Wed Feb 06, 2013 4:52 pm

One easy way is to just pick a folder name and stick with it so you can do this:

Code: Select all
from os.path import join
package_path = join(sublime.packages_path(), "MyFolderName")


But, if you really just gotta have the directory name from Python, there is a Python variable that is called __file__ which will give the path to the current script file. Then you can just strip off the file name.
Code: Select all
from os.path import dirname, realpath
print(dirname(realpath(__file__)))
facelessuser
 
Posts: 1567
Joined: Tue Apr 05, 2011 7:38 pm

Re: Current plugin folder path

Postby ol_ol_3388 on Wed Feb 06, 2013 5:05 pm

Thank you very much for your reply.

I tried
facelessuser wrote:
Code: Select all
from os.path import dirname, realpath
print(dirname(realpath(__file__)))


But it prints back
Code: Select all
/


What am I doing wrong ?
ol_ol_3388
 
Posts: 18
Joined: Mon Feb 04, 2013 3:29 pm

Re: Current plugin folder path

Postby facelessuser on Wed Feb 06, 2013 5:11 pm

Don't know. This works for me in ST2 and ST3. I would have to see your code and know where you are putting your code.
facelessuser
 
Posts: 1567
Joined: Tue Apr 05, 2011 7:38 pm

Re: Current plugin folder path

Postby ol_ol_3388 on Wed Feb 06, 2013 10:07 pm

Thanks for looking into this !

This is my code:

Code: Select all
import commands, subprocess
import sublime, sublime_plugin
from os.path import dirname, realpath

class MyPluginCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.save()
        self.myplugin(edit)

    def save(self):
        self.view.run_command("save")

    def myplugin(self, edit):
        scriptPath = sublime.packages_path() + "/MyPlugin/scripts/myscript.js"
        print("1: " + self.view.file_name())
        print("2: " + dirname(realpath(__file__)))
        print("3: " + sublime.packages_path())


Output 2 & 3 is identical and on my system is:
/Users/MacBookPro/Library/Application Support/Sublime Text 2/Packages

I was expecting Output 2 to look like this:
/Users/MacBookPro/Library/Application Support/Sublime Text 2/Packages/MyPlugin

So I do not need to hardcode "MyPlugin" inside python script. Possible ?
ol_ol_3388
 
Posts: 18
Joined: Mon Feb 04, 2013 3:29 pm

Re: Current plugin folder path

Postby facelessuser on Wed Feb 06, 2013 11:27 pm

Calling it from a function is probably the problem. __file__ is probably only valid at init time. I would save the path to some global variable, and then just reference the global.

Code: Select all
MY_PLUGIN = dirname(realpath(__file__))

...

    def myplugin(self, edit):
        scriptPath = sublime.packages_path() + "/MyPlugin/scripts/myscript.js"
        print("1: " + self.view.file_name())
        print("2: " + MY_PLUGIN)
        print("3: " + sublime.packages_path())
facelessuser
 
Posts: 1567
Joined: Tue Apr 05, 2011 7:38 pm

Re: Current plugin folder path

Postby ol_ol_3388 on Thu Feb 07, 2013 2:33 am

Thank you very much for your help !!! I really appreciate it.
It worked just fine, after I moved it outside the function.
ol_ol_3388
 
Posts: 18
Joined: Mon Feb 04, 2013 3:29 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 6 guests