Question

I have been teaching myself python and started out using python 2.7.5 and Sublime Text 2. I recently enrolled in a class and they are using python 3.4. I downloaded and installed python 3.4 onto my mac, but am having trouble figuring out how to make Sublime Text use python 3 instead of 2.7. I used..

import sys
print sys.version

to determine which version of python ST was using. It reports back with 2.7.5.

How do I force ST to use python 3?

If I have not included enough information, please don't hesitate to ask.

Was it helpful?

Solution

Found this on Google.

Create the file ~/.config/sublime-text-2/Packages/Python/Python3.sublime-build:

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

You should then be able to choose the Python3 build system.

If that doesn't work, try this:

{
    "cmd": ["python3", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "encoding": "utf8",
    "path": "/Library/Frameworks/Python.framework/Versions/3.3/bin/"
}

OTHER TIPS

For me worked creating the file ~/.config/sublime-text-2/Packages/Python/Python3.sublime-build:

{
    "cmd": ["python3", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "encoding": "utf8",
    "path": "/home/tercio/anaconda3/bin/"
}

The "path": "/home/tercio/anaconda3/bin/" is path that is installed the anaconda3 in my computer. Replace this information with her path.

Under Windows, it looks like Sublime(4) runs "py" instead of "python" or "python3".

If you would rather it just run what python.exe is 1st in the PATH, then extract the Python package (Command Palette -> PackageResourceViewer: Extract Package -> Python).

Then do Preferences -> Browse Packages -> Python and edit Python.sublime-build

Change:

"cmd": ["python3", "-u", "$file"],

to:

"cmd": ["python", "-u", "$file"],

and get rid of this entire block:

"windows": {
    "cmd": ["py", "-u", "$file"],
},

TLDR, you should be left with this:

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

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "cmd": ["python", "-m", "py_compile", "$file"]
        }
    ]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top