在 Linux 配置 Python3 的环境

司徒池暝
2023-12-01

1. 安装 sublime

直接参考官网用命令安装 https://www.sublimetext.com/


2. 安装 Python 和 pip

2.1 安装 Python

进入到当前用户的 Downloads 目录:

su yin	# 切换用户
cd
cd Downloads

下载一个版本的源文件,注意根据需求修改版本号

wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz

解压并移动到 /usr/local 目录下,命名为 python3可能需要权限,加上 sudo 前缀即可

cd Downloads/
tar -zxvf Python-3.7.5.tgz
mv Python-3.7.5 /usr/local #目录可以自己选择
cd /usr/local/
mv Python-3.7.5 python3

进入 python3 目录,编译

cd python3
./configure --prefix=/usr/local/python3 #python3的所在目录绝对路径
make
make install

安装成功,创建软链接,方便使用:

ln -s /usr/local/python3/bin/python3.5 /usr/bin/python3
python3

出现下面的结果即安装成功:

Python 3.7.5 (default, Dec  4 2021, 10:35:11) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(123)
123

注意:此时系统有两个 Python 版本,一个是自带的 Python2.7.x,另一个是新的,在终端输入 python 会运行 Python 2.7.x,输入 python3 才会运行新安装的 Python !

接着安装 pip(参考博客 https://blog.csdn.net/A156348933/article/details/85321935



3. 配置 Sublime

3.1 Package Control

好像 package control 是自带的,如果没有可以试试在console 运行下面的代码,或者用其他办法

import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by) 

3.2 build system

点击 tools-build system-new build system,创建新的 build-system,输入下面的代码:

{
 "cmd": ["/usr/bin/python3", "-u", "$file"],
 "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
 "selector": "source.python" 
 }

文件另存为 python3.sublime-build,接着在 tools - build system 中选择 python3
此时,可以通过 tools - build 运行代码

3.2 Sublime REPL

step1:按快捷键 Ctrl+shidt+P 打开 package control,输入 install,安装 sublime repl
step2:打开 preferences-key Bindings,下面的代码粘贴进去

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

此时,使用 ctrl+b 就可以运行交互式地 python 了

step3:设置 REPL 使用 python 3
运行下面的代码:

import os
print(os.sys.version)

点击 tools - build 会得到 3.7.5

而通过ctrl + b 得到的结果确是 2.7.x,而且无法导入 pip 安装的库

分析:REPL 要设置使用 Python3!

首先,点击 Preferences - Browser Packages 打开目录,进入 Sublime REPL - config,可以看到里面有 Python 文件夹,这是默认的 Python2.7.x,复制这个 Python 文件夹,副本重命名为 Python3

进入Python3,修改文件 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"
        }
    }
]

接着修改 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/Python/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/Python/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/Python/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/Python/Python.tmLanguage",
                        "external_id": "python3",
                        "extend_env": {
                            "PYTHONIOENCODING": "utf-8",
                            "SUBLIMEREPL_EDITOR": "$editor"
                        }
                    }
                    }
                ]}
            ]
        }]
    }
]

最后,不要忘了修改 key Bindings中快捷键的设置,把原来调用 Python 改为 Python3:

"file": "config/Python3/Main.sublime-menu"}  

重启 sublime,使用 ctrl + B 就可以运行 Python3 啦!

REFERENCES

  1. https://blog.csdn.net/qq_42081843/article/details/86479691
  2. https://blog.csdn.net/qq_41107910/article/details/88234178
  3. https://blog.csdn.net/A156348933/article/details/85321935
 类似资料: