量体截衣 發表於 2021-12-5 23:15:00

Debian 11上安装Python 3.10,并切换系统默认Python版本

<h1 id="更新程序包并安装变异依赖环境">更新程序包并安装变异依赖环境</h1>
<pre><code>sudo apt update &amp;&amp; sudo apt upgrade
sudo apt install wget build-essential libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
</code></pre>
<h1 id="开始正式安装">开始正式安装</h1>
<h2 id="1下载python源码包">1.下载Python源码包</h2>
<p>可以前往Python官网获取最新源码</p>
<pre><code>cd ~
wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
</code></pre>
<h2 id="2解压python源码">2.解压Python源码</h2>
<p>将下载好的源码包进行解压,默认放在当前文件夹下的压缩包同名文件夹内</p>
<pre><code>tar xzf Python-3.10.0.tgz
</code></pre>
<h2 id="3编译python源码">3.编译Python源码</h2>
<p>进入解压后的文件夹内,进行选项配置</p>
<pre><code>cd Python-3.10.0
./configure --enable-optimizations

#--enable-optimizations为优化性能选项,其余类似的还有 --prefix=PATH 指定安装目录……,可根据需要进行选择。
#默认安装路径为 /usr/local/bin
</code></pre>
<h2 id="4安装python-310">4.安装Python 3.10</h2>
<pre><code>make altinstall

#altinstall用于防止编译器覆盖默认Python版本
</code></pre>
<h2 id="5验证安装">5.验证安装</h2>
<pre><code>root@raspberrypi:~ # python3.10
Python 3.10.0 (default, Dec5 2021, 22:46:09) on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt;
</code></pre>
<hr>
<p>至此,已完成Python3.10的安装<br>
接下来可以根据需要选择是否需要更改默认Python为Python3.10</p>
<h1 id="切换python版本">切换Python版本</h1>
<p>可以使用以下两个命令 <em>whereis或which</em> 确定已安装python的版本和路径:</p>
<pre><code>#whereis:适用于查看目前已安装的所有Python版本及路径
root@raspberrypi:~ # whereis python
python: /usr/bin/python2.7-config /usr/bin/python /usr/bin/python3.9 /usr/bin/python2.7 /usr/bin/python3.9-config /usr/lib/python3.9 /usr/lib/python2.7 /etc/python3.9 /etc/python2.7 /usr/local/bin/python3.10-config /usr/local/bin/python3.10 /usr/local/lib/python3.9 /usr/local/lib/python2.7 /usr/local/lib/python3.10 /usr/include/python3.9m /usr/include/python3.9 /usr/include/python2.7 /usr/share/man/man1/python.1.gz
</code></pre>
<pre><code>#which:适用于查看具体某个python版本的安装路径
root@raspberrypi:~ # which python3.10
/usr/local/bin/python3.10
</code></pre>
<h2 id="为单个用户切换python版本">为单个用户切换Python版本</h2>
<p>只需要在该用户home目录下的 <em>.bashrc</em> 文件下新增 <em>Alias</em> 即可</p>
<pre><code>alias python='/usr/local/bin/python3.10'
#python具体版本和路径可根据个人需要确定
</code></pre>
<p>修改完毕后,使用<code>source ~/.bashrc</code>命令,重新加载 <em>.bashrc</em> 文件,使其生效</p>
<h2 id="系统级切换python版本">系统级切换Python版本</h2>
<p>使用<code>update-alternatives --list python</code>命令,为整个系统更改Python版本</p>
<h3 id="1列出所有可用python替代版本">1.列出所有可用Python替代版本</h3>
<pre><code>root@raspberrypi:~ # update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.9
/usr/local/bin/python3.10
</code></pre>
<h3 id="2添加替代版本列表">2.添加替代版本列表</h3>
<p>如果运行后出现错误信息:<code>update-alternatives: error:no alternatives for python</code><br>
则为没有更新替代版本列表,使用以下命令添加:</p>
<pre><code>#注意:update-alternatives --install &lt;link&gt; &lt;name&gt; &lt;path&gt; &lt;priority&gt;
#1.&lt;link&gt;一般情况下,直接使用 /usr/bin/python 即可
#2.&lt;name&gt;即为需要更换的python
#3.&lt;path&gt;为需要添加的python版本的安装路径,可以在上文中确定
#4.&lt;priorit&gt;为优先级。数字越大,优先级越高

root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: 使用 /usr/bin/python2.7 来在自动模式中提供 /usr/bin/python (python)
root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
update-alternatives: 使用 /usr/bin/python3.9 来在自动模式中提供 /usr/bin/python (python)
root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/local/bin/python3.10 3
update-alternatives: 使用 /usr/local/bin/python3.10 来在自动模式中提供 /usr/bin/python (python)
</code></pre>
<p>至此,系统已默认Python版本为3.10,验证如下:</p>
<pre><code>root@raspberrypi:~ # python
Python 3.10.0 (default, Dec5 2021, 22:46:09) on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt;
</code></pre>
<h3 id="3进行版本切换">3.进行版本切换</h3>
<p>使用<code>update-alternatives --config python</code>命令即可</p>
<pre><code>root@raspberrypi:~ # update-alternatives --config python
有 3 个候选项可用于替换 python (提供 /usr/bin/python)。

选择       路径                     优先级状态
------------------------------------------------------------
* 0            /usr/local/bin/python3.10   3         自动模式
1            /usr/bin/python2.7          1         手动模式
2            /usr/bin/python3.9          2         手动模式
3            /usr/local/bin/python3.10   3         手动模式

要维持当前值[*]请按&lt;回车键&gt;,或者键入选择的编号:2
update-alternatives: 使用 /usr/bin/python3.9 来在手动模式中提供 /usr/bin/python (python)
root@raspberrypi:~ # python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt;
</code></pre>
<h1 id="参考链接">参考链接:</h1>
<ol>
<li>月灯依旧:怎样在Debian 10上安装Python 3.9</li>
<li>weixin_39634876:python升级命令debian_如何将 Debian Linux 中的默认的 Python 版本切换为替代版本</li>
<li>YanniZhang的博客:linux 查看python安装路径,版本号</li>
</ol><br><br>
来源:https://www.cnblogs.com/STangQL/p/15647583.html
頁: [1]
查看完整版本: Debian 11上安装Python 3.10,并切换系统默认Python版本