I develop ROMs and TWRP for Android phones, and Android was quite late in the transition to Python 3, when developing a ROM based on Android 9 or earlier, or TWRP for a device that shipped with Android 9 or earlier, you'll still need Python 2. This has recently become an issue for me, as I switched from Ubuntu 22.04 to Debian 12, and Debian 12 doesn't have Python 2. Is there any way to install Python 2 on Debian 12?
You can still install Python 2.7 from Debian 11. Mixing releases is typically decried as a bad idea, but that is mostly in the context of adding a newer release; adding an older (still-supported) release is much less error-prone, as long as you keep an eye on any packages apt wants to remove.
Add a file, /etc/apt/sources.list.d/bullseye.list, containing
deb http://deb.debian.org/debian bullseye main
deb http://deb.debian.org/debian bullseye-updates main
deb http://security.debian.org bullseye-security main
Run sudo apt update, and you will be able to install python2.7-dev.
As you are probably aware, maintaining Python 2-based projects is becoming increasingly difficult, and the above won’t be a viable approach once Debian 11 is no longer supported (some would argue it already isn’t viable).
The PPA Approach
While Stephen's answer is certainly viable until Bullseye is no longer supported. I believe this approach will survive until the various python versions are removed from the PPA. Adding this PPA or Personal Package Archive, or any for that matter is relatively easy.
The PPA Specifically for Python
The Dead Snakes Personal Package Archive contains packaged versions of Python all the way back to 2.3. Add it with: sudo add-apt-repository ppa:deadsnakes/ppa
Note that these are made to work with Ubuntu, and since that's a Debian derivative:
The packages may also work on other versions of Ubuntu or Debian, but that is not tested or supported.
See also: Install newer & older versions of python on debian?. Read Gilles' answer from 8 years ago that's still quite valid.
I wanted to install python 2 on debian testing to use inkscape extensions and I found this thread. Apparently, as many of you reply, it is dropped on debian, the only solution I have found has been to use conda.
Copyconda create -n "Inkscape" python=2.7
Solution:
remove python3-virtualenv
sudo apt-get purge python3-virtualenv
sudo apt-get install python2
Install virtualenv version <= 20.21.1.
sudo pip install virtualenv==20.21.1 --break-system-packages
Instead --break-system-packages, can be used:
removing file /usr/lib/python3.x/EXTERNALLY-MANAGED)
adding following lines to ~/.config/pip/pip.conf:
[global] break-system-packages = true see: How do I solve "error: externally-managed-environment" every time I use pip 3?
After virtualenv -p /usr/bin/python2 py2venv
should work.
Addition: As @matt noted debian 12 droped python2 support at all, so python distribution should be installed from debian 11 distro or conda is used.
Get the Python 2.7.1 sources and compile it manually:
configure --prefix=/path/to/python-2.7
make; make install
Python 2.7 is available for wheezy (testing), so you should be able to install it by adding the testing repository and doing some APT pinning.
1) add the repository in /etc/apt/sources.list
deb http://ftp.us.debian.org/debian testing main contrib non-free
2) do the actual pinning in /etc/apt/preferences
Package: *
Pin: release n=testing
Pin-Priority: 100
A Pin-Priority of under 500 basically means that no packages from testing are installed automatically, so you won't have problems with other packages.
3) install python2.7 from testing:
aptitude -t testing install python2.7
(or apt-get if you don't have aptitude)