搞python环境杂七杂八那些事(更新中)
多版本python环境管理
可能是本篇唯一有点用的一章
纯官方方法
无法做到python版本号第三位(micro version)的不同版本共存(例如3.10.6和3.10.11共存)。不过这似乎也没有实用价值,一般前两位符合需求就没问题。
Windows: py launcher(py.exe)
Windows下,如果同时安装多个版本的python,则添加进PATH的只能有最多一个,添加多个的话,python
命令只会调用PATH中位置靠前的那个。
为了便于在命令行下管理多版本,python官方默认安装了py launcher,并且用管理员权限安装到全局目录;也可以在自定义安装选项中选择:


已经安装过py launcher之后,再安装第二版本的python选项就不可用了:


使用例:py -X.Y
等价于直接使用相应的python.exe
,所以诸如py -X.Y -m pip install foo
或者py -X.Y -m venv venv
这样的命令都是合法的操作。
Linux: 直接共存
Linux下是可以非常方便地直接同时安装多版本的python的。
ubuntu
直接添加ppa源即可:1
2sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update需要注意的是这个ppa源按照debian系拆包的习惯,把每个版本的python都拆分成了多个小包,类似于:
Built packages
idle-python3.11 IDE for Python (v3.11) using Tkinter
libpython3.11 Shared Python runtime library (version 3.11)
libpython3.11-dbg Debug Build of the Python Interpreter (version 3.11)
libpython3.11-dev Header files and a static library for Python (v3.11)
libpython3.11-minimal Minimal subset of the Python language (version 3.11)
libpython3.11-stdlib Interactive high-level object-oriented language (standard library, version 3.11)
libpython3.11-testsuite Testsuite for the Python standard library (v3.11)
python3.11 Interactive high-level object-oriented language (version 3.11)
python3.11-dbg Debug Build of the Python Interpreter (version 3.11)
python3.11-dev Header files and a static library for Python (v3.11)
python3.11-distutils distutils package for Python (version 3.11)
python3.11-examples Examples for the Python language (v3.11)
python3.11-full Python Interpreter with complete class library (version 3.11)
python3.11-gdbm GNU dbm database support for Python (version 3.11)
python3.11-gdbm-dbg GNU dbm database support for Python (version 3.11 debug extension)
python3.11-lib2to3 lib2to3 package for Python (version 3.11)
python3.11-minimal Minimal subset of the Python language (version 3.11)
python3.11-tk Tkinter - Writing Tk applications with Python (version 3.11)
python3.11-tk-dbg Tkinter - Writing Tk applications with Python (version 3.11 debug extension)
python3.11-venv Interactive high-level object-oriented language (pyvenv binary, version 3.11)按需取用,如果图省事就直接安装
python3.*-full
Debian不支持ppa,不过把ppa源的deb包倒腾过来用也不是不行…吧?
Arch系
AUR都有,用喜欢的AUR助手直接装就行;不过AUR的本质也是拉取python源码然后按照预设选项全自动编译,感觉上还没有手动编译好…Linux通用法
根据官方github的方法直接编译就行,调整下--prefix
的位置以便管理,然后最后使用make altinstall
安装即可。
后面会有大量篇幅碎碎念编译(或者说这篇就是为折腾编译cpython这盘醋包的饺子)
共存安装后,直接使用对应的可执行文件调用即可。
例如python3
调用系统自带python,python3.11
调用安装的3.11,pip3.12
调用安装的3.12版本的pip