Linux下sublime3 REPL插件 调用python3解释python文件

张宣
2023-12-01

在Package Control: Install Package中,输入SublimeREPL就会自动安装好REPL插件。REPL插件配合分屏功能使用体验更好。

1、Linux下Sublime3默认调用的是python2,而不是python3,这就导致在编译python程序的时候,可能会报一些错误,比如在python文件中使用input函数,由于input函数是python3的函数,而REPL插件如果默认调用python2解析python文件,那么就会导致程序报错:

     File "<string>", line 1, in <module> NameError: name 'xxxx' is not defined

这时候就需要手动设置sublimeREPL调用python3来解析python的文件。

2、设置快捷键

Sublime Text 3-> Preferences-> Key Bindings

3、输入下列内容并保存

[
    {  
      
    "keys":["f5"],  
    "caption": "SublimeREPL: Python - RUN current file",  
    "command": "run_existing_window_command", 
    "args": {
        "id": "repl_python_run",  
        "file": "config/Python3/Main.sublime-menu"}  
      
    } ,

    {
    "keys": ["f8"],
    "caption": "SublimeREPL: Python - PDB current file",
    "command": "run_existing_window_command",
    "args": {
        "id": "repl_python_pdb",
        "file": "config/Python3/Main.sublime-menu"}
    }
]

4、配置编译系统

    Preferences-> Browse Packages

    打开SublimeREPL->config文件夹

    新建Python3文件夹,并添加下列文件

Default.sublime-commands
Main.sublime-menu

5、修改Default.sublime-commands,内容如下:

[
    {
        "caption": "SublimeREPL: Python3",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python3",
            "file": "config/Python3/Main.sublime-menu"
        }
    },
    {
        "caption": "SublimeREPL: Python - PDB current file",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python_pdb",
            "file": "config/Python3/Main.sublime-menu"
        }
    },
    {
        "caption": "SublimeREPL: Python3 - RUN current file",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python3_run",
            "file": "config/Python3/Main.sublime-menu"
        }
    },
    {
        "command": "python_virtualenv_repl",
        "caption": "SublimeREPL: Python - virtualenv"
    },
    {
        "caption": "SublimeREPL: Python - IPython",
        "command": "run_existing_window_command", "args":
        {
            "id": "repl_python_ipython",
            "file": "config/Python3/Main.sublime-menu"
        }
    }
]

6、修改Main.sublime-menu,内容如下:

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

                 "children":[
                    {"command": "repl_open",
                     "caption": "Python3",
                     "id": "repl_python3",
                     "mnemonic": "P",
                     "args": {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": ["python3", "-i", "-u"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python3/Python.tmLanguage",
                        "external_id": "python3",
                        "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": ["python3", "-i", "-u", "-m", "pdb", "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python3/Python.tmLanguage",
                        "external_id": "python3",
                        "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": ["python3", "-u", "$file_basename"],
                        "cwd": "$file_path",
                        "syntax": "Packages/Python3/Python.tmLanguage",
                        "external_id": "python3",
                        "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": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
                            "linux": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
                            "windows": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"]
                        },
                        "cwd": "$file_path",
                        "syntax": "Packages/Python3/Python.tmLanguage",
                        "external_id": "python3",
                        "extend_env": {
                            "PYTHONIOENCODING": "utf-8",
                            "SUBLIMEREPL_EDITOR": "$editor"
                        }
                    }
                    }
                ]}
            ]
        }]
    }
]

7、最后打开sublime,先后点击Preferences-> Browse Packages,打开SublimeREPL->config文件夹,把Python目录下的ipy_repl.py拷贝到上一级config目录下的Python3目录中。这样就可以直接按F5快捷键运行python程序了,默认调用的是python3。

 类似资料: