ubuntu 安装管理多版本python3 相关问题解决
背景:使用ubuntu 22.04 默认python 未3.10.编译一些模块的时候发现需要降级到python3.9.于是下载安装
下载:
wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz
解压与编译
tar -xf Python-3.9.16.tgz
cd Python-3.9.16
./configure --enable-optimizations --with-lto --enable-shared
make -j4 //这里根据自己的cpu core 自行更新需要的数字
提示:
在编译完成后,可能会遇到一些缺少库的提示,尽量安装完成,参考如下
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_dbm _tkinter
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
可以使用相关的一些指令去安装,例如
sudo apt-get install xxx (或者libxxx-dev) //xxx 未上面报的缺少的内容
安装:
sudo make altinstall
安装后遇到的问题
1.error while loading shared libraries: libpython3.9.so.1.0: cannot open shared object file: No such file or directory
问题根因
系统默认加载/usr/lib,/lib下面库文件,python3.9 默认安装到非此类文件夹,所以查找不到相关的lib
解决方案
1,cp /usr/local/lib/libpython3.9.so.1.0 /usr/lib64
ldconfig
2,cp /usr/local/lib/libpython3.9.so.1.0 /usr/lib
echo "/usr/lib" > /etc/ld.so.conf.d/python3.9.conf
ldconfig
重点:以上修改原理都一样,根据个人爱好。我 用的第一个。一定要运行ldconfig,这个很重要。
2. terminal 或者终端打不开
问题根因:
使用原生的xterm ,运行命令:gnome-terminal.报错如下:
指定默认python版本
由于系统中有多个python 版本,需要指定使用哪个,网上有各种各样的版本,我建议使用正规方式,使用update-alternatives的方式
1,确认都有系统哪些python版本
可以通过ls /usr/bin/python* 等方式来确认
2,使用update-alternatives来修改
*************************:~$ update-alternatives --help
Usage: update-alternatives [<option> ...] <command>
Commands:
--install <link> <name> <path> <priority>
[--slave <link> <name> <path>] ...
add a group of alternatives to the system.
--remove <name> <path> remove <path> from the <name> group alternative.
--remove-all <name> remove <name> group from the alternatives system.
--auto <name> switch the master link <name> to automatic mode.
--display <name> display information about the <name> group.
--query <name> machine parseable version of --display <name>.
--list <name> display all targets of the <name> group.
--get-selections list master alternative names and their status.
--set-selections read alternative status from standard input.
--config <name> show alternatives for the <name> group and ask the
user to select which one to use.
--set <name> <path> set <path> as alternative for <name>.
--all call --config on all alternatives.
<link> is the symlink pointing to /etc/alternatives/<name>.
(e.g. /usr/bin/pager)
<name> is the master name for this link group.
(e.g. pager)
<path> is the location of one of the alternative target files.
(e.g. /usr/bin/less)
<priority> is an integer; options with higher numbers have higher priority in
automatic mode.
Options:
--altdir <directory> change the alternatives directory
(default is /etc/alternatives).
--admindir <directory> change the administrative directory
(default is /var/lib/dpkg/alternatives).
--instdir <directory> change the installation directory.
--root <directory> change the filesystem root directory.
--log <file> change the log file.
--force allow replacing files with alternative links.
--skip-auto skip prompt for alternatives correctly configured
in automatic mode (relevant for --config only)
--quiet quiet operation, minimal output.
--verbose verbose operation, more output.
--debug debug output, way more output.
--help show this help message.
--version show the version.
显示配置
update-alternatives --display python3
若果没有,就按照规则添加
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.9 2
sudo update-alternatives --install /usr/bin/python3 python3 /usr//bin/python3.10 1
后面的数字越大,优先级越高
选择默认python 配置
sudo update-alternatives --config python3
根据提示,输入需要python版本前面的数字
持续更新中。。。