0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Python的环境配置

汽车电子技术 来源:码农与软件时代 作者:码农与软件时代 2023-02-15 11:44 次阅读

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
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 操作系统
    +关注

    关注

    37

    文章

    6288

    浏览量

    121886
  • Ubuntu
    +关注

    关注

    5

    文章

    533

    浏览量

    28840
  • python
    +关注

    关注

    51

    文章

    4677

    浏览量

    83468
收藏 人收藏

    评论

    相关推荐

    python计算库环境配置(二)及烧写模式介绍

    本文介绍云芯一号(rk3399) 微型服务器的python环境配置一些记录。本Arm服务器算力不是很高主要面向服务类应用,但是可以跑一些简单的模...
    的头像 发表于 12-15 00:55 442次阅读

    python环境变量的配置pip

    Python环境变量的配置和使用是每个Python开发者都需要了解和掌握的基本技能之一。在本文中,我们将详细介绍如何正确配置
    的头像 发表于 12-15 15:41 990次阅读

    2021最新Python爬虫+数据可视化-3.python环境配置以及编辑器的安装 #硬声创作季

    编程语言python
    Hello,World!
    发布于 :2022年08月29日 20:19:21

    【HarmonyOS HiSpark AI Camera】开箱以及环境配置分享

    /third_party/ffmpeg/ffmpeg-y清空ffmpeg-y里面的所有文件,然后把从gitee上下的替换进去就行综上,如果确定自己的python环境配置没问题的话,遇到编译错误可以尝试去gitee
    发表于 11-25 17:54

    如何对RK3399Pro python环境进行配置

    RK3399Pro python环境有哪些配置呢?如何对RK3399Pro python环境进行配置
    发表于 02-11 07:20

    python基础教程之如何使用python进行环境搭建

    PythonPython 2 和 Python 3 两个版本。 语法有些区别。 保险起见, 我安装Python配置pydev解释器安装
    发表于 10-25 16:55 32次下载

    python如何配置虚拟环境

    python 的虚拟环境可以为一个 python 项目提供独立的解释环境、依赖包等资源,既能够很好的隔离不同项目使用不同 python 版本
    发表于 01-07 17:12 811次阅读

    如何使用docker和python工具包datmo为数据科学和AI框架快速配置环境

    如果让你说出软件开发最烦人的事情,那么环境配置必然是其中之一。例如开始编写Python应用程序,那么你的第一个步骤就是在您的计算机上安装Python
    的头像 发表于 03-20 08:57 1689次阅读

    配置Python开发环境的DeepStream容器

    范例,并不适用于 DeepStream 的 Python 环境,因为还需要安装 Gstreamer 的 Gst-Python 与 DeepStream 的 PyBinding 与两个元件。
    的头像 发表于 08-12 11:05 1237次阅读

    pycharm如何配置Python解释器

    刚学Python,你肯定遇到过这个问题刚学Python时,拿到一个Python项目,想用pycharm打开运行,pycharm界面却显示No Python Interpreter co
    的头像 发表于 10-14 15:48 1.2w次阅读
    pycharm如何<b class='flag-5'>配置</b><b class='flag-5'>Python</b>解释器

    如何测试Python环境

    在编程中,测试是一项重要的工作,可以帮助我们验证代码的正确性和稳定性。在Python编程环境中,同样需要进行测试来确保Python的安装和配置是正确的。在本篇文章中,我们将介绍如何测试
    的头像 发表于 04-14 12:14 3870次阅读

    如何配置Python环境变量

    配置Python环境变量是在安装Python解释器后的一项重要步骤,它允许您在任何位置都可以通过命令行或脚本运行Python解释器,使
    的头像 发表于 04-14 12:16 2w次阅读

    pycharm怎么配置python环境变量

    PyCharm 是一种以 Python 为主的集成开发环境 (IDE),它提供了一系列的功能用于开发、调试、测试和部署 Python 程序。在使用 PyCharm 开发 Python
    的头像 发表于 11-29 14:56 933次阅读

    python软件对电脑配置要求

    Python是一种流行的编程语言,它在许多不同的领域中被广泛使用,例如网站开发、数据科学和机器学习等。对于使用Python的开发者来说,了解Python软件的电脑配置要求是非常重要的。
    的头像 发表于 11-29 14:58 4453次阅读

    python运行环境的安装和配置

    Python是一种非常流行的编程语言,广泛应用于科学计算、Web开发、人工智能等领域。为了能够正常运行Python程序,我们需要先安装和配置Python运行
    的头像 发表于 11-29 16:17 545次阅读