简体   繁体   中英

Possible to switch between python 2 and 3 in Sublime Text 3 build systems? (Windows)

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!

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.

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 #! python or
    • #! python3

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM