【问题标题】:Change Python version for evaluating file with SublimREPL plugin使用 SublimREPL 插件更改用于评估文件的 Python 版本
【发布时间】:2017-02-22 07:58:02
【问题描述】:

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

我已经安装了Package ControlSublimREPL 插件。

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

然后我使用 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 开始。

【问题讨论】:

  • 对 OSX 不是很熟悉,但对于 linux,Python3.x 安装的可执行文件将命名为 python3python35 之类的名称。如果/usr/local/bin/ 像您提到的那样为空,那么它是否有可能安装到/usr/bin 中,如果不是,/Library/Frameworks/ 甚至只是在应用程序中呢?
  • 另一件要尝试的事情,在命令行上,如果你可以让 python3 运行,whereis pythonwhich python 会告诉你它的位置。同样,在 Python 3 解释器 import site; print(site.USER_SITE)“应该”打印出 python3 的 site-packages 目录的位置。

标签: python macos sublimetext3 osx-elcapitan sublimerepl


【解决方案1】:

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

然后我使用 Tools > Command Palette... > SublimeREPL: Python 启动 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. 标题和 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)

【讨论】:

  • 确实有一个Tools > Command Palette... > SublimeREPL: Python 菜单:看我的帖子我已经编辑并添加了截图。
  • 我尝试了您的解决方案,无论如何它都能完美地完成工作。非常感谢!
  • 在我的系统(Windows)上,该文件需要命名为 Main.sublime-menu 而不是 Menu.sublime-menu。除此之外,这正是我正在寻找的答案!谢谢你。
猜你喜欢
  • 1970-01-01
  • 2019-04-10
  • 2019-04-24
  • 2011-11-09
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 2023-04-07
相关资源
最近更新 更多