python软链接设置

当前环境:

系统

  • CentOS7.6

当前系统下python相关命令

[lemon@iZbp159egvvktszc82dr5xZ vendors]# whereis python
python: /usr/bin/python2.7-config 
/usr/bin/python2.7 
/usr/bin/python 
/usr/lib/python2.7 
/usr/lib64/python2.7 
/etc/python 
/usr/local/bin/python3.7-config 
/usr/local/bin/python3.7 
/usr/local/bin/python3.7m 
/usr/local/bin/python3.7m-config 
/usr/local/lib/python3.7 
/usr/include/python2.7 
/usr/share/man/man1/python.1.gz
[lemon@iZbp159egvvktszc82dr5xZ ~]$ cd /usr/bin
[lemon@iZbp159egvvktszc82dr5xZ bin]$ ls -al python*
[lemon@iZbp159egvvktszc82dr5xZ bin]$ ls -al python*
lrwxrwxrwx 1 root root    7 Aug 26 19:08 python -> python2
lrwxrwxrwx 1 root root    9 Aug 26 19:08 python2 -> python2.7
-rwxr-xr-x 1 root root 7144 Nov 17  2020 python2.7
-rwxr-xr-x 1 root root 1835 Nov 17  2020 python2.7-config
lrwxrwxrwx 1 root root   16 Aug 26 19:08 python2-config -> python2.7-config
lrwxrwxrwx 1 root root   24 Aug 26 19:53 python3 -> /opt/python/python-3.7.3
lrwxrwxrwx 1 root root   14 Aug 26 19:08 python-config -> python2-config

当前系统有两个版本python

  • python2.7.5
  • python3.7.3

参考文章

安装其他版本python

下载地址:https://www.python.org/ftp/python/

这里选择python 3.6.3 版本

1.创建存放目录
[lemon@iZbp159egvvktszc82dr5xZ bin]$ sudo mkdir /usr/local/python3.6
[lemon@iZbp159egvvktszc82dr5xZ bin]$ cd /usr/local/python3.6
[lemon@iZbp159egvvktszc82dr5xZ python3.6]$ pwd
/usr/local/python3.6
[lemon@iZbp159egvvktszc82dr5xZ python3.6]$ ll
total 0
2.下载,解压
[lemon@iZbp159egvvktszc82dr5xZ python3.6]$ sudo wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
[lemon@iZbp159egvvktszc82dr5xZ python3.6]$ sudo tar -xvf Python-3.6.3.tgz
[lemon@iZbp159egvvktszc82dr5xZ python3.6]$ ll
total 22148
drwxr-xr-x 16  501  501     4096 Oct  3  2017 Python-3.6.3
-rw-r--r--  1 root root 22673115 Oct  3  2017 Python-3.6.3.tgz
3.编译:
[lemon@iZbp159egvvktszc82dr5xZ python3.6]$ cd Python-3.6.3/
##########|当前目录:/usr/local/python3/Python-3.6.3|######################################
##########|python3.6Dir文件夹为临时创建|##########################################
[lemon@iZbp159egvvktszc82dr5xZ Python-3.6.3]$ sudo ./configure --prefix=/usr/local/python3.6Dir
[lemon@iZbp159egvvktszc82dr5xZ Python-3.6.3]$ sudo make
[lemon@iZbp159egvvktszc82dr5xZ Python-3.6.3]$ sudo make install
4.编译完成,查看命令
[lemon@iZbp159egvvktszc82dr5xZ bin]$ cd /usr/local/python3.6Dir/bin/
[lemon@iZbp159egvvktszc82dr5xZ bin]$ ls
2to3              idle3    pip3.6    python3           python3.6m         pyvenv
2to3-3.6          idle3.6  pydoc3    python3.6         python3.6m-config  pyvenv-3.6
easy_install-3.6  pip3     pydoc3.6  python3.6-config  python3-config
5.验证

当前python3.6命令并不在全局中:

[lemon@iZbp159egvvktszc82dr5xZ bin]$ python3.6
-bash: python3.6: command not found

[lemon@iZbp159egvvktszc82dr5xZ bin]$ ./python3.6
Python 3.6.3 (default, Sep  4 2021, 11:19:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

其他python命令版本及位置:

python2.7:
[lemon@iZbp159egvvktszc82dr5xZ ~]$ python -V
Python 2.7.5
[lemon@iZbp159egvvktszc82dr5xZ ~]$ which python
/usr/bin/python
python3.7:
[lemon@iZbp159egvvktszc82dr5xZ ~]$ python3 -V
Python 3.7.3
[lemon@iZbp159egvvktszc82dr5xZ ~]$ which python3
/usr/local/bin/python3

添加软链接:

为python3.6添加别名/软链接:

1.保留usr/bin下的python(原2.7版本),创建至/usr/local/bin目录下:
[lemon@iZbp159egvvktszc82dr5xZ bin]$ cd /usr/local/bin
[lemon@iZbp159egvvktszc82dr5xZ bin]$ sudo ln -s /usr/local/python3.6Dir/bin/python3 /usr/local/bin/python3.6
[lemon@iZbp159egvvktszc82dr5xZ bin]$ pwd
/usr/local/bin
[lemon@iZbp159egvvktszc82dr5xZ bin]$ ls python*
python3  python3.6  python3.7  python3.7-config  python3.7m  python3.7m-config  python3-config
2.可以看到/usr/local/bin目录下的python的相关指令:
[lemon@iZbp159egvvktszc82dr5xZ bin]$ ls -al python*
lrwxrwxrwx 1 root root        9 Aug 26 19:52 python3 -> python3.7
lrwxrwxrwx 1 root root       35 Sep  4 12:47 python3.6 -> /usr/local/python3.6Dir/bin/python3
-rwxr-xr-x 2 root root 12635064 Aug 26 19:52 python3.7
lrwxrwxrwx 1 root root       17 Aug 26 19:52 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 12635064 Aug 26 19:52 python3.7m
-rwxr-xr-x 1 root root     3105 Aug 26 19:52 python3.7m-config
lrwxrwxrwx 1 root root       16 Aug 26 19:52 python3-config -> python3.7-config
3.退出至根目录,验证一下软链接是否可行:
[lemon@iZbp159egvvktszc82dr5xZ bin]$ cd /
[lemon@iZbp159egvvktszc82dr5xZ /]$ pwd
/
[lemon@iZbp159egvvktszc82dr5xZ /]$ python3.6
Python 3.6.3 (default, Sep  4 2021, 11:19:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[lemon@iZbp159egvvktszc82dr5xZ /]$ python3.7
Python 3.7.3 (default, Aug 26 2021, 19:51:41)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[lemon@iZbp159egvvktszc82dr5xZ /]$ python
Python 2.7.5 (default, Nov 16 2020, 22:23:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
4.相同操作,再搞一下pip指令:
[lemon@iZbp159egvvktszc82dr5xZ bin]$ sudo ln -s /usr/local/python3.6Dir/bin/pip3.6 /usr/local/bin/pip3.6
[sudo] password for lemon:
[lemon@iZbp159egvvktszc82dr5xZ bin]$ pip3.6 -V
pip 9.0.1 from /usr/local/python3.6Dir/lib/python3.6/site-packages (python 3.6)
[lemon@iZbp159egvvktszc82dr5xZ bin]$ pip3.7 -V
pip 21.2.4 from /home/lemon/.local/lib/python3.7/site-packages/pip (python 3.7)
[lemon@iZbp159egvvktszc82dr5xZ bin]$ pip -V
pip 20.3.4 from /home/lemon/.local/lib/python2.7/site-packages/pip (python 2.7)

为python3.7添加别名/软链接:

which python3知其命令所在位置,通过ln -s 添加软链接,也放在/bin目录下

[lemon@iZbp159egvvktszc82dr5xZ ~]$ sudo ln -s /usr/local/bin/python3 /usr/local/bin/paisen3
[sudo] password for lemon:
[lemon@iZbp159egvvktszc82dr5xZ ~]$ paisen3
Python 3.7.3 (default, Aug 26 2021, 19:51:41)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
posted @ 2021-10-09 16:47  Lemon_guess  阅读(1073)  评论(0编辑  收藏  举报