FunnyWii
FunnyWii
Published on 2024-08-08 / 85 Visits
0
0

Linux切换Python版本

软连接

一般Ubuntu系统中默认有两个Python,一个Python2.7,一个Python3.8。

这个可以在 /usr/bin 目录中找到:

(base) jetson@ubuntu:~$ cd /usr/bin/
(base) jetson@ubuntu:/usr/bin$ ls python*
python   python2.7         python2-config  python3.8         python3.8-coverage  python3-config    python3-futurize    python-argcomplete-check-easy-install-script3
python2  python2.7-config  python3         python3.8-config  python3.9           python3-coverage  python3-pasteurize  python-argcomplete-tcsh3

其中的 python 和 python3 都是软连接,具体链接到哪可以用下述命令查看:

(base) jetson@ubuntu:/usr/bin$ ls -l python
lrwxrwxrwx 1 root root 7 Apr 15  2020 python -> python3
(base) jetson@ubuntu:/usr/bin$ ls -l python3
lrwxrwxrwx 1 root root 9 Mar 13  2020 python3 -> python3.8

一般来说 python 应该指向 python2.x, python3 指向 python3.x。不过这个无所谓,软连接都是可以修改的。同时也可以使用 which python 命令查看当前系统使用的 python 路径:

(base) jetson@ubuntu:~$ which python
/home/jetson/anaconda3/bin/python

可以看到我的系统中,默认python是anaconda的python。虽然conda的虚拟环境可以为不同类型的项目提供诸多便利,但是有些时候也会导致C++代码编译出现问题。先用软连接方式把/usr/bin 目录的python修改为python2,之所以这样操作,不是说一定要这样,只是记录一下操作流程。

(base) jetson@ubuntu:/usr/bin$ sudo rm python    #删除原有软链接
(base) jetson@ubuntu:/usr/bin$ sudo ln -s python2 python   # ln -s [源文件或目录] [符号链接名]
(base) jetson@ubuntu:/usr/bin$ ls -l python
lrwxrwxrwx 1 root root 7 Aug  8 11:14 python -> python2

完事。

update-alternatives

如果想要修改系统的python,并实现方便的python多版本管理,可以使用update-alternatives 命令:

update-alternatives --install <链接名称> <名称> <路径> <优先级>

然后我们试一下,这里把python2.7和python3.8都归为python,使用update-alternatives --config python即可切换py2和py3:

(base) jetson@ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
(base) jetson@ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in auto mode
(base) jetson@ubuntu:~$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.8   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.8   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

现在系统的python3应该就变成 /usr/bin/python3.8啦:

(base) jetson@ubuntu:~$ which python
/home/jetson/anaconda3/bin/python
(base) jetson@ubuntu:~$ which python3
/home/jetson/anaconda3/bin/python3

怎么没改过来...emmm忘了关conda的base环境了。使用这条命令conda config --set auto_activate_base false 即可不让Terminal自动打开虚拟环境,再看看:

jetson@ubuntu:~$ which python
/home/jetson/anaconda3/envs/py37/bin/python
jetson@ubuntu:~$ which python3
/home/jetson/anaconda3/envs/py37/bin/python3

黑人问号.webp

发生甚麽事了,我们打开bashrc看看gedit ~/.bashrc

export PATH=/home/jetson/anaconda3/envs/py37/bin/:$PATH
export MVCAM_SDK_PATH=/opt/MVS
export MVCAM_COMMON_RUNENV=/opt/MVS/lib
export MVCAM_GENICAM_CLPROTOCOL=/opt/MVS/lib/CLProtocol
export ALLUSERSPROFILE=/opt/MVS/MVFG
# export LD_LIBRARY_PATH=/opt/MVS/lib/aarch64:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/home/jetson/libmodbus/install/lib/pkgconfig:$PKG_CONFIG_PATH
# HKVision ARM64 SDK LIB PATH
export LD_LIBRARY_PATH=/home/jetson/libHKVision:$LD_LIBRARY_PATH

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/jetson/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/jetson/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/jetson/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/jetson/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

看第一行!!!谁给我加的!!!不会是我吧。。。

第一行的内容给注释掉,然后source一下就可以了。

jetson@ubuntu:~$ which python
/usr/bin/python
jetson@ubuntu:~$ which python3
/usr/bin/python

在这部分,如已经关闭了默认打开base环境,运行update-alternatives --install 命令之后,就可以了,后面需要修改bashrc文件纯粹是我个人的问题。


Comment