wiltomap wiltomap - 1 month ago 10
Python Question

Change Python version for evaluating file with SublimREPL plugin

I am using Sublim Text 3 on Mac OS X El Capitan. What I need to do is to evaluate a Python file within Sublime Text 3.

I have installed

Package Control
and then
SublimREPL
plugins.

I have set up a 2 rows layout (
View > Layout > Rows: 2
) in order to display a Python interpreter in the second part of the screen.

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

enter image description here

The interpreter starts correctly and I get this:

enter image description here

I can't find how to start with Python 3.5 that I have downloaded manually (thus installed in
/usr/local/bin/
). I have tried to modify this file:
/Library/Application Support/Sublime Text 3/Packages/SublimeREPL/Config/Python/Main.sublime-menu
following this post instructions, but this did not change anything (Python 2.7.10 still launched).

Here is the content of my
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"
}
}
}
]}
]
}]
}
]


Still following this post advices, I modified the part of code below, but I can't find any exe file in folder
/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"}
}
}


When I press
Ctrl
+
,
+
f
(according to the doc), the interpreter still starts with Python 2.7.10.

Answer

It appears you are modifying the incorrect configuration files and perhaps a few things are causing issues.

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

There is no command palette item "SublimeREPL: Python", so I'm assuming you mean Tools > SublimeREPL > Python > Python. That opens in a tab something like the following:

# *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.
>>> 

In fact Tools > SublimeREPL > Python displays a menu something like the following:

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

So far so good. But how to change the Python version?

Ideally we could either configure the global python to use (that doesn't seem possible) or add a version variant to the menus desccribed above (that doesn't seem possible either).

The most straight forward workaround is adding a custom version variant.

Create a file named Packages/User/Menu.sublime-menu (if it doesn't already exist). Find the User packages directory via Menu > Preferences > Browse Packages...). And create your menu in that file. This menu will be added to the existing menus.

For example:

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"}
                    }
                }
            ]
        }]
    }
]

It is important that:

  1. The caption and id are not the same as any other menu otherwise it will replace the other menu, hence I've named the one above "SublimeREPL Variants".

  2. The "cmd" uses an absolute path to a valid binary. You can check you are using the correct path with a commands like which:

    Find the location of Python via which:

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

    See also Possible to switch between python 2 and 3 in Sublime Text 3 build systems? (Windows) for additional details.

Comments