更改 Python 版本以使用 SublimREPL 插件评估文件

Change Python version for evaluating file with SublimREPL plugin

我在 Mac OS X El Capitan 上使用 Sublim Text 3。我需要做的是评估 Sublime Text 3 中的 Python 文件。

我已经安装了 Package Control 然后 SublimREPL 插件。

我设置了 2 行布局 (View > Layout > Rows: 2) 以便在屏幕的第二部分显示 Python 解释器。

然后我用 Tools > Command Palette... > SublimeREPL: Python 启动 Python 解释器。

解释器正确启动,我得到这个:

我找不到如何从我手动下载的 Python 3.5 开始(因此安装在 /usr/local/bin/ 中)。我尝试修改此文件:/Library/Application Support/Sublime Text 3/Packages/SublimeREPL/Config/Python/Main.sublime-menu 按照 this post 说明,但这并没有改变任何东西(Python 2.7.10 仍在启动)。

这是我的Main.sublime-menu的内容:

[
     {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "R",
            "id": "SublimeREPL",
            "children":
            [
                {"caption": "Python",
                "id": "Python",

                 "children":[
                    {"command": "repl_open",
                     "caption": "Python",
                     "id": "repl_python",
                     "mnemonic": "P",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python", "-i", "-u"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python/Python.tmLanguage",
                        "external_id": "python",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    },
                    {"command": "python_virtualenv_repl",
                     "id": "python_virtualenv_repl",
                     "caption": "Python - virtualenv"},
                    {"command": "repl_open",
                     "caption": "Python - PDB current file",
                     "id": "repl_python_pdb",
                     "mnemonic": "D",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python", "-i", "-u", "-m", "pdb", "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python/Python.tmLanguage",
                        "external_id": "python",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    },
                    {"command": "repl_open",
                     "caption": "Python - RUN current file",
                     "id": "repl_python_run",
                     "mnemonic": "R",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python", "-u", "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python/Python.tmLanguage",
                        "external_id": "python",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                        }
                    },
                    {"command": "repl_open",
                     "caption": "Python - IPython",
                     "id": "repl_python_ipython",
                     "mnemonic": "I",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "autocomplete_server": true,
                        "cmd": {
                            "osx": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
                            "linux": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
                            "windows": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"]
                        },
                        "cwd": "$file_path",
                        "syntax": "Packages/Python/Python.tmLanguage",
                        "external_id": "python",
                        "extend_env": {
                            "PYTHONIOENCODING": "utf-8",
                            "SUBLIMEREPL_EDITOR": "$editor"
                        }
                    }
                    }
                ]}
            ]
        }]
    }
]

仍然按照this post的建议,我修改了下面的部分代码,但是在文件夹/usr/local/bin/中找不到任何exe文件:

{"command": "repl_open",
"caption": "Python - PDB current file",
"id": "repl_python_pdb",
"mnemonic": "D",
"args": {
    "type": "subprocess",
    "encoding": "utf8",
    "cmd": ["/usr/local/bin/python3", "-i", "-u", "-m", "pdb", "$file_basename"],
    "cwd": "$file_path",
    "syntax": "Packages/Python/Python.tmLanguage",
    "external_id": "python",
    "extend_env": {"PYTHONIOENCODING": "utf-8"}
    }
}

当我按 Ctrl + , + f(根据 doc)时,解释器仍然从 Python 2.7.10 开始。

看来您正在修改不正确的配置文件,可能有几件事导致了问题。

I then launch the Python interpreter with Tools > Command Palette... > SublimeREPL: Python

没有命令选项板项 "SublimeREPL: Python",所以我假设你的意思是 Tools > SublimeREPL > Python > Python。这会在类似以下内容的选项卡中打开:

# *REPL* [python]
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

实际上 Tools > SublimeREPL > Python 显示如下所示的菜单:

Python - execnet
Python
Python - virtualen
Python - PDB current file
Python - RUN current file
Python - IPython

到目前为止一切顺利。但是如何更改Python版本?

理想情况下,我们可以配置全局 python 来使用(这似乎不可能)或向上面描述的菜单添加版本变体(这似乎也不可能)。

最直接的解决方法是添加自定义版本变体。

创建一个名为 Packages/User/Menu.sublime-menu 的文件(如果它不存在)。通过 Menu > Preferences > Browse Packages...) 找到用户包目录。并在该文件中创建您的菜单。此菜单将添加到现有菜单。

例如:

Packages/User/Menu.sublime-menu

[
    {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL Variants",
            "id": "SublimeREPLVariants",
            "children":
            [
                {
                    "command": "repl_open",
                    "caption": "Python - PDB current file",
                    "id": "repl_python_pdb",
                    "mnemonic": "D",
                    "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["/usr/bin/python3", "-i", "-u", "-m", "pdb", "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python/Python.tmLanguage",
                        "external_id": "python",
                        "extend_env": {"PYTHONIOENCODING": "utf-8"}
                    }
                }
            ]
        }]
    }
]

重要的是:

  1. caption 和 id 与其他菜单不同,否则它将替换其他菜单,因此我将上面的菜单命名为 "SublimeREPL Variants".

  2. "cmd" 使用有效二进制文件的绝对路径。您可以使用 which:

    等命令检查您使用的路径是否正确

    找到 Python 的位置:

    $ which python
    /usr/bin/python
    
    $ which python3
    /usr/bin/python3
    
    $ which python2
    /usr/bin/python2
    

    另请参阅 Possible to switch between python 2 and 3 in Sublime Text 3 build systems? (Windows) 了解更多详细信息。