Sublime Forum

Select which version of Python to run/build?

#1

Hello,

I’m curious if there’s a way to select which version of Python I can choose to run.

As of now it’s only running the 3x branch (I do have both versions installed, 2.7.1 and 3.2).

Is there a way for me to be able to select which version runs?

Thanks!

0 Likes

#2

Hi TVo,

The default “Python.sublime-build” file should look something like this (Packages\User\Packages\Python\Python.sublime-build):

{
	"cmd": "python", "-u", "$file"],
	"file_regex": "^ ]*File \"(...*?)\", line ([0-9]*)",
	"selector": "source.python"
}

To use a different version of python, change the “cmd” part of this build system (or better yet create a new build system) so it looks like this:

{
	"cmd": "C:\path\to\desired\version\python.exe", "-u", "$file"],
	"file_regex": "^ ]*File \"(...*?)\", line ([0-9]*)",
	"selector": "source.python"
}

Hope it helps.

Cheers,
Josh

0 Likes

#3

Thank you very much, Josh!

That’s exactly what I was looking for. I didn’t see where the defaults were stored so I had nothing to look at to guide me.

I don’t know Mac OSX or Linux file systems well enough, or how to properly type them out.

Could you perhaps show me an example of how to do this with a mac and linux as well?

I have been messing around with the settings but so far I haven’t been able to get it to work.

0 Likes

#4

Just so you know, I’m getting an error on my machine when I try using the full path.

The error says:

“Error trying to parse build system: Invalid escape in C:\Users\Me\AppData\Roaming\Sublime Text 2\Packages\User\Python27.sublime-build:2:10”

This is what I have written as my command:

{ "cmd": "C:\Python27\python.exe", "-u", "$file"], "file_regex": "^ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }

Any ideas?

Do I need to add a “$(FULL_CURRENT_PATH)” in there anywhere?

Thanks for the help

0 Likes

#5

I think the error is coming because the path you entered isn’t correctly escaped. Try using:

    {
       "cmd": "C:\\Python27\\python.exe", "-u", "$file"],
       "file_regex": "^ ]*File \"(...*?)\", line ([0-9]*)",
       "selector": "source.python"
    }

Sorry, I didn’t even think about it when I posted.

Hopefully that helps…

0 Likes

#6

[quote=“TVo”]Thank you very much, Josh!
Could you perhaps show me an example of how to do this with a mac and linux as well?
[/quote]

It should be basically the same for linux or a mac - edit or create a new build system. You will need to include the appropriate path to the python executable. In linux you should be able to get the path by typing “which python” or “locate python” in a terminal window. Not sure on a mac, but those linux commands should also work.

The nice thing in linux/mac is that you don’t have to worry about escaping the paths, since “/” is used (as opposed to “”).

0 Likes

#7

That did it for the Windows version!

Thanks for that. Seeing if I can figure out the Mac/Linux version. I’m just not familiar enough with the file system.

Will the executable have a file extension on the Mac/Linux versions, or will I just write: “cmd”: “/Whatever/whatever/python”, …

?

0 Likes

#8

Normally linux/unix binaries (programs) do not have an extension (unlike windows that uses .exe). The default location for python should be in ‘/usr/bin/python’, so in that case you would have:

{ "cmd": "/usr/bin/python", "-u", "$file"], "file_regex": "^ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }

I’m not sure where other versions of python would be installed in linix so you will have to search for that using ‘locate python’ or something like that and update the build system accordingly.

0 Likes

#9

That did it! Thanks again!

0 Likes

#10

I think there is a square bracket “[” missing in the "cmd " ligne

Thank you jbjornson

0 Likes

#11

Indeed there is; a ways back the forum was migrated to different software and along the way some square brackets got dropped out of existing posts as a result. In fact there is one missing from the file_regex as well.

The build system from above would look like this:

{
    "cmd": ["/usr/bin/python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}
0 Likes