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

后端 未结 3 912
攒了一身酷
攒了一身酷 2021-01-06 13:08

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

相关标签:
3条回答
  • 2021-01-06 13:11

    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
    0 讨论(0)
  • 2021-01-06 13:16

    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.

    0 讨论(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"

    0 讨论(0)
提交回复
热议问题