4

All my current coding has been in python 3, which I've installed via the Anaconda package. However I need to work on some code simultaneously in python 2. Is there a way I can add a build system in Sublime so I can switch between the two fluidly? I have both python 2 and 3 installed, however can't see a way of simply editing the build system slightly to switch between the two languages. The build system I'm using for python 3 is:

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

Thanks very much!

Plaetean
  • 163
  • 2
  • 12
  • change environment variable PATH to point to python 2 as well, but change executable name to something like `python2.exe`. Now you can call python from cmd with pyhon2 – Виталик Бушаев Oct 13 '16 at 15:11
  • I thought about this but there is no python entry in PATH (which I don't understand), only two Anaconda entries. – Plaetean Oct 13 '16 at 15:25
  • launch `where python` in command line, this will tell which python you're using when calling `python` from cmd. That probably gonna be one of your Anacondas. Then you can rename second one to what you want to call python 2 with. – Виталик Бушаев Oct 13 '16 at 15:30
  • Ok I have added a PATH to python 2, renamed the executable and edited the build command. When I put python2 --version into cmd it finds everything fine, but when I run the new build I'm getting: 'python2' is not recognized as an internal or external command, operable program or batch file. Any idea how to fix? – Plaetean Oct 13 '16 at 15:34
  • Thanks very much, working now. – Plaetean Oct 13 '16 at 15:45

3 Answers3

4

Specify the absolute path to the version of Python. For example, if Python version 3 is located at /usr/bin/python3 and Python version 2 is located at /usr/bin/python2:

Python 3 Build Configuration:

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

Python 2 Build Configuration:

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

Find the location of Python via which:

$ which python
/usr/bin/python

$ which python3
/usr/bin/python3

$ which python2
/usr/bin/python2

See Build system of Sublime Text 3 executes a different version of PHP, it is the same principal.

Community
  • 1
  • 1
Gerard Roche
  • 6,162
  • 4
  • 43
  • 69
2

Old question, but...

On Windows, you dont need create a new build system or specify the absolute path. Edit already existing python build system.

  1. In SublimeText\Data\Packages\ create folder Python -> like this SublimeText\Data\Packages\Python\
  2. In this folder, create file Python.sublime-build , it will overwrite the existing python build system.
  3. In this file write (python launcher documentation):

    {
        "shell_cmd": "py -u \"$file\"",
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python",
        "env": {"PYTHONIOENCODING": "utf-8"},
    }
    
  4. For choose version of python interpretator, in the first line of your scripts write (shebang lines)

    • #! python or
    • #! python3
FMarkus
  • 54
  • 4
0

1, intall pyhton3: https://www.python.org/downloads/

2, find the file "python.exe" and rename to "python3.exe"

3, in Windows command line:

where python3.exe

Save this ubication wherever you want.

4, In sublime goto -> Tools -> Build system -> New build system (this open a file)

5, finally the ubication file of number 3, only change the ubication:

{
    /*replace this*/"cmd": ["C:/Users/youruser/AppData/Local/Programs/Python/Python37-32/python3.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "shell": true
}

Save the file like "python3".

6, In sublime go to Tools -> Build system -> Select python3 and you can run this code in python 3.

7, Show your python version:

import sys
print(sys.version)

If you want to run in python 2, then go to Tools -> Build system -> Select "Python"

qarly_blue
  • 380
  • 3
  • 7