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

wil*_*map 5 python macos sublimerepl sublimetext3 osx-elcapitan

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

我已安装Package Control然后SublimREPL插件.

我已经设置了2行layout(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遵循这篇文章说明,但这并没有改变任何东西(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"
                        }
                    }
                    }
                ]}
            ]
        }]
    }
]
Run Code Online (Sandbox Code Playgroud)

仍然遵循这篇帖子的建议,我修改了下面的代码部分,但我在文件夹中找不到任何exe文件/usr/local/bin/:

{"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"}
    }
}
Run Code Online (Sandbox Code Playgroud)

当我按Ctrl+ ,+ f(根据文档),解释器仍然从Python 2.7.10开始.

Ger*_*che 5

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

然后我启动 Python 解释器 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.
>>> 
Run Code Online (Sandbox Code Playgroud)

实际上Tools > SublimeREPL > Python显示了一个类似于以下的菜单:

Python - execnet
Python
Python - virtualen
Python - PDB current file
Python - RUN current file
Python - IPython
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好。但是如何更改 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"}
                    }
                }
            ]
        }]
    }
]
Run Code Online (Sandbox Code Playgroud)

重要的是:

  1. 标题和 id 与任何其他菜单不同,否则它将替换其他菜单,因此我将上面的菜单命名为“SublimeREPL Variants”。

  2. “cmd”使用有效二进制文件的绝对路径。您可以使用以下命令检查您是否使用了正确的路径which

    通过以下方式找到 Python 的位置:

    $ which python
    /usr/bin/python
    
    $ which python3
    /usr/bin/python3
    
    $ which python2
    /usr/bin/python2
    
    Run Code Online (Sandbox Code Playgroud)

    另请参阅可能在 Sublime Text 3 构建系统中在 python 2 和 3 之间切换?(Windows)了解更多详情。