Sublime Forum

Current plugin folder path

#1

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 ?

0 Likes

#2

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

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.

from os.path import dirname, realpath print(dirname(realpath(__file__)))

0 Likes

#3

Thank you very much for your reply.

I tried

[quote=“facelessuser”]

from os.path import dirname, realpath print(dirname(realpath(__file__)))[/quote]

But it prints back

/

What am I doing wrong ?

0 Likes

#4

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.

0 Likes

#5

Thanks for looking into this !

This is my code:

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 ?

0 Likes

#6

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]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())

[/code]

0 Likes

#7

Thank you very much for your help !!! I really appreciate it.
It worked just fine, after I moved it outside the function.

0 Likes