简体   繁体   English

可以在 Sublime Text 3 构建系统中在 python 2 和 3 之间切换吗? (窗户)

[英]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.我当前所有的编码都在 python 3 中,我是通过 Anaconda 包安装的。 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?但是,我需要在 python 2 中同时处理一些代码。有没有办法在 Sublime 中添加构建系统,以便我可以在两者之间流畅地切换? 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.我同时安装了 python 2 和 3,但是看不到简单地编辑构建系统以在两种语言之间切换的方法。 The build system I'm using for python 3 is:我用于 python 3 的构建系统是:

{
    "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.指定 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 位于/usr/bin/python3并且 Python 版本 2 位于/usr/bin/python2

Python 3 Build Configuration: Python 3 构建配置:

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

Python 2 Build Configuration: Python 2 构建配置:

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

Find the location of Python via which :查找通过Python的位置, 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.请参阅Sublime Text 3 的构建系统执行不同版本的 PHP ,它是相同的原理。

Old question, but...老问题,但是...

On Windows, you dont need create a new build system or specify the absolute path.在 Windows 上,您不需要创建新的构建系统或指定绝对路径。 Edit already existing python build system.编辑现有的 python 构建系统。

  1. In SublimeText\\Data\\Packages\\ create folder Python -> like this SublimeText\\Data\\Packages\\Python\\SublimeText\\Data\\Packages\\创建文件夹Python -> 像这样SublimeText\\Data\\Packages\\Python\\
  2. In this folder, create file Python.sublime-build , it will overwrite the existing python build system.在此文件夹中,创建文件Python.sublime-build ,它将覆盖现有的 python 构建系统。
  3. In this file write ( python launcher documentation ):在此文件中写入( python 启动器文档):

     { "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解释器,在脚本的第一行写( shebang行

    • #! python #! python or #! python
    • #! python3

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

2, find the file "python.exe" and rename to "python3.exe" 2、找到文件“python.exe”并重命名为“python3.exe”

3, in Windows command line: 3、在Windows命令行中:

where python3.exe

Save this ubication wherever you want.将这个 ubication 保存在任何你想要的地方。

4, In sublime goto -> Tools -> Build system -> New build system (this open a file) 4、在sublime goto -> Tools -> Build system -> New build system(这样打开一个文件)

5, finally the ubication file of number 3, only change the ubication: 5、最后是3号的ubication文件,只更改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".将文件保存为“python3”。

6, In sublime go to Tools -> Build system -> Select python3 and you can run this code in python 3. 6、在sublime中去工具->构建系统->选择python3,你可以在python 3中运行这段代码。

7, Show your python version: 7、显示你的python版本:

import sys
print(sys.version)

If you want to run in python 2, then go to Tools -> Build system -> Select "Python"如果你想在python 2中运行,那么去工具->构建系统->选择“Python”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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