I use pyenv to manage my python versions as well as their virtual environments. Sometimes I ran into problems, of which the solution requires building python after installing certain dependencies.
So I simply run
pyenv uninstall 3.8.12
and install the dependencies required, then
pyenv install 3.8.12
however, this operation removes all the package installed along with the entire virtual environment under
pyenvrootdir/.pyenv/versions/3.8.12
in the previous version, like numpy or matplotlib.
So is there any way to remove, then re-build python, while those packages remains, so that I don't need to install them again?
Use virtualenv with pyenv: https://github.com/pyenv/pyenv-virtualenv
Then you can just write, uninstall only needed if you like to delete it:
pyenv install 3.11.2
pyenv virtualenv 3.11.2 test1
pyenv activate test1
pyenv uninstall test1
To install a new one, just deactivate the current:
pyenv deactivate
pyenv virtialenv 3.11.2 mynewapp
pyenv activate mynewapp
It is not uncommon having lots of virtualenvs.