Bench does not work on upgrade to ubuntu 21.04

I recently upgraded ubuntu to 21.04. Now bench does not work. It has to do with the python version. My version is now 3.9.5

Is it possible to get bench working without scrapping everything? This is the error message we get.

Traceback (most recent call last):
File “/usr/lib/python3.8/runpy.py”, line 185, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File “/usr/lib/python3.8/runpy.py”, line 111, in get_module_details
import(pkg_name)
File “/srv/bench/frappe-bench/apps/frappe/frappe/init.py”, line 1658, in
def log_error(message=None, title=
(“Error”)):
File “/srv/bench/frappe-bench/apps/frappe/frappe/init.py”, line 67, in _
from frappe.translate import get_full_dict
File “/srv/bench/frappe-bench/apps/frappe/frappe/translate.py”, line 19, in
from frappe.model.utils import InvalidIncludePath, render_include
File “/srv/bench/frappe-bench/apps/frappe/frappe/model/utils/init.py”, line 6, in
from frappe.build import html_to_js_template
File “/srv/bench/frappe-bench/apps/frappe/frappe/build.py”, line 10, in
from distutils.spawn import find_executable
File “/srv/bench/frappe-bench/env/lib/python3.8/distutils/init.py”, line 44, in
from distutils import dist, sysconfig # isort:skip
ImportError: cannot import name ‘dist’ from partially initialized module ‘distutils’ (most likely due to a circular import) (/srv/bench/frappe-bench/env/lib/python3.8/distutils/init.py)

Traceback (most recent call last):
File “/home/erp/.local/bin/bench”, line 8, in
sys.exit(cli())
File “/home/erp/.local/lib/python3.9/site-packages/bench/cli.py”, line 48, in cli
if sys.argv[1] in get_frappe_commands() + [“–site”, “–verbose”, “–force”, “–profile”]:
TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘list’

To be clear - we should never do this on a production machine. I ran the upgrade on my local machine, to see what would happen. Clearly a self-inflicted wound. I was running on the Dev branch, Ubuntu 20.04, Python 3.8.5

Upgraded to Ubuntu 21.04 (which automagically upgrades python to 3.9.5. This is something to look out for if you’re ever upgrading a Frappe/ERPnext server.

change the python version. Sometimes this does the trick.
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.9 10

go to the folder above frappe-bench. (in my case /srv/bench)
sudo -H pip3 install --upgrade --force-reinstall ./bench-repo

setup the virtual environment
cd frappe-bench (find path to virutalenv using ‘which virtualenv’)
/home/cspl/.local/bin/virtualenv -q env -p python3

install frappe
cd apps
git clone GitHub - frappe/frappe: Low code web framework for real world applications, in Python and Javascript --branch develop

cd /srv/bench/ (go back up)
frappe-bench/env/bin/python -m pip install -q -U -e frappe-bench/apps/frappe
cd frappe-bench
yarn install
cd apps/frappe
yarn install

install any other apps you have -
bench get-app …

bench build

now you can run bench (sudo supervisorctl restart all or bench start)

follow these instructions thereonin

Bench update --reset
bench update --patch
bench update
bench build

EDIT
For my production sandbox, the above approach did not work. This was a bit more challenging.

I renamed my original frappe-bench folder.
ran the bench init command
bench init frappe-bench --frappe-path GitHub - frappe/frappe: Low code web framework for real world applications, in Python and Javascript --frappe-branch develop --python python3

copied over the frappe-bench-archive/site/TENANTs folders to frappe-bench/site/*

Next: setup nginx
bench setup nginx
sudo rm -f /etc/nginx/sites-enabled/*
sudo ln -s /srv/bench/frappe-bench/config/nginx.conf /etc/nginx/sites-enabled/frappe.conf

Setup the firewalls (probably as a result of the upgrade, firewalls were not working)

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw enable
sudo ufw app list
sudo ufw allow ‘Nginx HTTPS’
sudo ufw allow 22,25,143,80,443,3306,8000/tcp