Python的环境配置

描述

Ubuntu操作系统自带Python,18.04的版本分别为Python2.7.17和Python3.6.9。

root@linux:/# python --version
Python 2.7.17
root@linux:/# python3 --version
Python 3.6.9
root@linux:/# ll /usr/bin/ | grep python*
-rwxr-xr-x  1 root   root        1056 Apr 16  2018 dh_python2*
lrwxrwxrwx  1 root   root          23 Sep 30  2020 pdb2.7 -> ../lib/python2.7/pdb.py*
lrwxrwxrwx  1 root   root          23 Oct  8  2020 pdb3.6 -> ../lib/python3.6/pdb.py*
lrwxrwxrwx  1 root   root          31 Oct 25  2018 py3versions -> ../share/python3/py3versions.py*
lrwxrwxrwx  1 root   root           9 Apr 16  2018 python -> python2.7*
lrwxrwxrwx  1 root   root           9 Apr 16  2018 python2 -> python2.7*
-rwxr-xr-x  1 root   root     3628976 Sep 30  2020 python2.7*
lrwxrwxrwx  1 root   root           9 Oct 25  2018 python3 -> python3.6*
-rwxr-xr-x  2 root   root     4526456 Oct  8  2020 python3.6*
-rwxr-xr-x  2 root   root     4526456 Oct  8  2020 python3.6m*
-rwxr-xr-x  1 root   root        1018 Oct 29  2017 python3-jsondiff*
-rwxr-xr-x  1 root   root        3661 Oct 29  2017 python3-jsonpatch*
-rwxr-xr-x  1 root   root        1342 May  2  2016 python3-jsonpointer*
-rwxr-xr-x  1 root   root         398 Nov 16  2017 python3-jsonschema*
lrwxrwxrwx  1 root   root          10 Oct 25  2018 python3m -> python3.6m*
lrwxrwxrwx  1 root   root          29 Apr 16  2018 pyversions -> ../share/python/pyversions.py*
root@linux:/#

如何设置Python的默认版本?

可以通过修改软链接的方式进行:

root@linux:/# rm /usr/bin/python
root@linux:/# ln -s /usr/bin/python3.6 /usr/bin/python
root@linux:/# python --version
Python 3.6.9

还可以通过update-alternatives进行设置,update-alternatives进行软件版本的切换:

root@linux:/# update-alternatives --list python
update-alternatives: error: no alternatives for python
root@linux:/# 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
root@linux:/#  update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python (python) in auto mode
root@linux:/# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.6
root@linux:/# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).


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


Press  to keep the current choice[*], or type selection number: 2
root@linux:/# python --version
Python 3.6.9

alternatives的管理目录 /etc/alternatives,指向可执行的文件:

root@linux:/# cd /etc/alternatives/
root@linux:/etc/alternatives# ll
......
lrwxrwxrwx   1 root root    18 Feb  7 16:51 python -> /usr/bin/python3.6*
......

如何安装Python新版本?

可以从源码进行安装

(1)环境准备
apt update
apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Python的解释器是用C语言编写的,从源码开始安装需要C语言的编译环境,Ubuntu提供了build-essential软件包,该软件包依赖gcc\\g++,因此安装build-essential,这些依赖包一起安装了。
root@linux:~# apt-cache depends build-essential
build-essential
 |Depends: libc6-dev
  Depends: 
    libc6-dev
  Depends: gcc
  Depends: g++
  Depends: make
    make-guile
  Depends: dpkg-dev


(2)下载源代码
wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
root@linux:~# wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
--2022-02-07 21:29:38--  https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
Resolving www.python.org (www.python.org)... 151.101.72.223, 2a04:4e42:1a::223
Connecting to www.python.org (www.python.org)|151.101.72.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23949883 (23M) [application/octet-stream]
Saving to: âPython-3.8.0.tgzâ


Python-3.8.0.tgz                       100%[===========================================================================>]  22.84M  16.8KB/s    in 29m 4s  


2022-02-07 21:58:42 (13.4 KB/s) - âPython-3.8.0.tgzâ saved [23949883/23949883]


(3)安装Python
tar -xf Python-3.8.0.tgz
cd Python-3.8.0
root@linux:~# cd Python-3.8.0
root@linux:~/Python-3.8.0# ll
total 1084
drwxr-xr-x 17 1000 1000   4096 Oct 14  2019 ./
drwx------ 19 root root   4096 Feb  7 22:06 ../
-rw-r--r--  1 1000 1000  11000 Oct 14  2019 aclocal.m4
-rw-r--r--  1 1000 1000    630 Oct 14  2019 CODE_OF_CONDUCT.md
-rwxr-xr-x  1 1000 1000  44166 Oct 14  2019 config.guess*
-rwxr-xr-x  1 1000 1000  36251 Oct 14  2019 config.sub*
-rwxr-xr-x  1 1000 1000 503509 Oct 14  2019 configure*
-rw-r--r--  1 1000 1000 166233 Oct 14  2019 configure.ac
drwxr-xr-x 18 1000 1000   4096 Oct 14  2019 Doc/
drwxr-xr-x  2 1000 1000   4096 Oct 14  2019 Grammar/
drwxr-xr-x  4 1000 1000   4096 Oct 14  2019 Include/
-rwxr-xr-x  1 1000 1000  15368 Oct 14  2019 install-sh*
drwxr-xr-x 33 1000 1000   4096 Oct 14  2019 Lib/
-rw-r--r--  1 1000 1000  12769 Oct 14  2019 LICENSE
drwxr-xr-x  2 1000 1000   4096 Oct 14  2019 m4/
drwxr-xr-x  8 1000 1000   4096 Oct 14  2019 Mac/
-rw-r--r--  1 1000 1000  67201 Oct 14  2019 Makefile.pre.in
drwxr-xr-x  2 1000 1000   4096 Oct 14  2019 Misc/
drwxr-xr-x 14 1000 1000   4096 Oct 14  2019 Modules/
drwxr-xr-x  4 1000 1000   4096 Oct 14  2019 Objects/
drwxr-xr-x  3 1000 1000   4096 Oct 14  2019 Parser/
drwxr-xr-x  6 1000 1000   4096 Oct 14  2019 PC/
drwxr-xr-x  2 1000 1000   4096 Oct 14  2019 PCbuild/
drwxr-xr-x  2 1000 1000   4096 Oct 14  2019 Programs/
-rw-r--r--  1 1000 1000  45194 Oct 14  2019 pyconfig.h.in
drwxr-xr-x  3 1000 1000   4096 Oct 14  2019 Python/
-rw-r--r--  1 1000 1000   9978 Oct 14  2019 README.rst
-rw-r--r--  1 1000 1000 104153 Oct 14  2019 setup.py
drwxr-xr-x 23 1000 1000   4096 Oct 14  2019 Tools/


./configure --enable-optimizations
......
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile
root@linux:~/Python-3.8.0# ls
aclocal.m4          config.log     configure     Grammar     Lib      Mac           Makefile.pre.in  Objects  PCbuild     pyconfig.h.in  setup.py
CODE_OF_CONDUCT.md  config.status  configure.ac  Include     LICENSE  Makefile      Misc             Parser   Programs    Python         Tools
config.guess        config.sub     Doc           install-sh  m4       Makefile.pre  Modules          PC       pyconfig.h  README.rst
root@linux:~/Python-3.8.0# 




make -j 8
........
unning build_scripts
copying and adjusting /root/Python-3.8.0/Tools/scripts/pydoc3 -> build/scripts-3.8
copying and adjusting /root/Python-3.8.0/Tools/scripts/idle3 -> build/scripts-3.8
copying and adjusting /root/Python-3.8.0/Tools/scripts/2to3 -> build/scripts-3.8
changing mode of build/scripts-3.8/pydoc3 from 644 to 755
changing mode of build/scripts-3.8/idle3 from 644 to 755
changing mode of build/scripts-3.8/2to3 from 644 to 755
renaming build/scripts-3.8/pydoc3 to build/scripts-3.8/pydoc3.8
renaming build/scripts-3.8/idle3 to build/scripts-3.8/idle3.8
renaming build/scripts-3.8/2to3 to build/scripts-3.8/2to3-3.8
make[1]: Leaving directory '/root/Python-3.8.0'
root@linux:~/Python-3.8.0# 


make altinstall
.......
Looking in links: /tmp/tmp84bibaoh
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-19.2.3 setuptools-41.2.0


root@linux:/usr/local/bin# ll
total 16936
drwxr-xr-x  2 root root     4096 Feb  7 22:19 ./
drwxr-xr-x 10 root root     4096 Apr 24  2019 ../
-rwxr-xr-x  1 root root      101 Feb  7 22:19 2to3-3.8*
-rwxr-xr-x  1 root root      241 Feb  7 22:19 easy_install-3.8*
-rwxr-xr-x  1 root root       99 Feb  7 22:19 idle3.8*
-rwxr-xr-x  1 root root      223 Feb  7 22:19 pip3.8*
-rwxr-xr-x  1 root root       84 Feb  7 22:19 pydoc3.8*
-rwxr-xr-x  1 root root 17307376 Feb  7 22:19 python3.8*
-rwxr-xr-x  1 root root     3087 Feb  7 22:19 python3.8-config*

如何卸载Python?

apt-get remove python3.8
apt-get remove --auto-remove python3.8
apt-get purge python3.8
apt-get purge --auto-remove python3.8
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分