Sublime Forum

[SOLVED] Obtain a plugin's directory at runtime

#1

Is it possible to obtain the plugin’s directory at runtime? Suppose I write a plugin that has supporting files in the same directory how do I access them dynamically, that is, without hardcoding the expected path in the plugin?

Example:

I expect DetectSyntax to live in Packages/DetectSyntax, but what if someone puts it in Packages/Detect Syntax (notice the space)?

0 Likes

#2

The current directory will be set to the plugin’s directory when it’s loaded, so you can capture os.getcwdu() then (it must be captured when the module is loaded, it will be set to other values later).

It’s not ideal to rely on this, but I believe it’s the only option at the moment, apart from requiring a specific package name.

0 Likes

#3

When you speak of loading, are you meaning init? I tried

	def __init__(self):
		self.plugin_dir = os.getcwdu()

and then printed it in my routine and it was /. Trying to use / when loading a file later generated an error because it was looking for the file at the filesystem’s root. So I’m a bit confused.

Any chance you will consider adding something that let’s the plugin discover where it is?

0 Likes

#4

Nevermind. I got it sorted out. I put the call to os.getcwd() right after the import statements before the class definition and it worked.

Thanks for the support, Jon.

0 Likes