繁体   English   中英

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

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

我当前所有的编码都在 python 3 中,我是通过 Anaconda 包安装的。 但是,我需要在 python 2 中同时处理一些代码。有没有办法在 Sublime 中添加构建系统,以便我可以在两者之间流畅地切换? 我同时安装了 python 2 和 3,但是看不到简单地编辑构建系统以在两种语言之间切换的方法。 我用于 python 3 的构建系统是:

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

非常感谢!

指定 Python 版本的绝对路径。 例如,如果 Python 版本 3 位于/usr/bin/python3并且 Python 版本 2 位于/usr/bin/python2

Python 3 构建配置:

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

Python 2 构建配置:

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

查找通过Python的位置, which

$ which python
/usr/bin/python

$ which python3
/usr/bin/python3

$ which python2
/usr/bin/python2

请参阅Sublime Text 3 的构建系统执行不同版本的 PHP ,它是相同的原理。

老问题,但是...

在 Windows 上,您不需要创建新的构建系统或指定绝对路径。 编辑现有的 python 构建系统。

  1. SublimeText\\Data\\Packages\\创建文件夹Python -> 像这样SublimeText\\Data\\Packages\\Python\\
  2. 在此文件夹中,创建文件Python.sublime-build ,它将覆盖现有的 python 构建系统。
  3. 在此文件中写入( python 启动器文档):

     { "shell_cmd": "py -u \\"$file\\"", "file_regex": "^[ ]*File \\"(...*?)\\", line ([0-9]*)", "selector": "source.python", "env": {"PYTHONIOENCODING": "utf-8"}, }
  4. 对于选择版本的python解释器,在脚本的第一行写( shebang行

    • #! python #! python
    • #! python3

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

2、找到文件“python.exe”并重命名为“python3.exe”

3、在Windows命令行中:

where python3.exe

将这个 ubication 保存在任何你想要的地方。

4、在sublime goto -> Tools -> Build system -> New build system(这样打开一个文件)

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
}

将文件保存为“python3”。

6、在sublime中去工具->构建系统->选择python3,你可以在python 3中运行这段代码。

7、显示你的python版本:

import sys
print(sys.version)

如果你想在python 2中运行,那么去工具->构建系统->选择“Python”

暂无
暂无

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

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