3 ms·
PSA: You don't need to use Pyenv (or asdf, or anything else...) on Linux. It just creates a mess. If you need to install multiple versions of Python that are n
by senko 4y ago
PSA: You don't need to use Pyenv (or asdf, or anything else...) on Linux. It just creates a mess.
If you need to install multiple versions of Python that are not available from your distribution packages, simply download the code, install it to a place like /opt and then point your virtualenv to that version when you need it.
Here's what's needed:
> ./configure --prefix=/opt/python-<VERSION>; make; make install
This is not more complicated than what pyenv asks you to do (read the article).
Once you have all the versions installed, no need to use a tool to switch between them. When you use virtualenv (which you should, and no need for pipenv, poetry and the rest), just do this to create a new virtualenv:
> /opt/python-<VERSION>/bin/python -m venv /path/to/where/you/want/to/store/your/virtualenv
This will pick up the correct version of Python. Once you activate the virtual environment, that's it.
There's no need to use anything besides what default Python installation already gives you.
PS. What about the dependencies? Glad you asked: `apt build-dep python3` on Debian/Ubuntu or `dnf builddep python` on Fedora. No need to go through them manually or copy/paste some list off someone's blog and hope it's still valid.
PPS. All this applies to Python 2 with minor tweaks, but hopefully you're not starting new Python2 projects in 2022.
- uranusjr 4y agoThere’s a command in pyenv called “python-build” that more or less automates the Python compilation part. Most don’t realise it (it’s hidden from the general UI) but that’s where most pyenv functionalities come from. I agree the rest of the pyenv (shims, shell magic etc.) tends to make a mess.
- rbanffy 4y agoThis, a million times. Extra tools means extra magic and magic is always bad.
- nerdponx 4y agoYou can use Pyenv entirely as a build/version manager if you want. Just don't use `pyenv activate` and point your tools to the absolute path of the Python interpreter that you want to use. In recent versions, the shims are installed to a separate directory, which you can omit from your PATH if you never want to use them.
- rbanffy 4y ago> You can use Pyenv entirely as a build/version manager if you want Usually the distros suffice for that. There are very few cases where it's worth to roll out your own Python. > In recent versions, the shims are installed to a separate directory This is a huge progress!
- krupan 4y agoThis should be the top comment. I might add direnv to automatically activate the virtual environment when you CD into the project directory, and to automatically deactivate it when you CD out of that directory
- tyingq 4y agoUnfortunately, you might have to use tools like a CI/CD system or a runtime environment (AWS Lambdas, for example), where none of this flows down correctly. Edit: As a concrete example, say you need a C based python package to work locally, but also build within Gitlab and be deployable to an AWS Lambda. There's a gap there where Python really wants absolute paths that don't change.