Install latest python version with pyenv
Asked Answered
E

5

43

With ruby-install, to install the latest stable ruby version, one needs only ruby-install ruby.

However, with pyenv one seems to need to do something ridiculous like pyenv install "$(pyenv install --list | tr -d ' ' | grep --extended-regexp '^[0-9.]+$' | tail -1)".

Is there a better way to do this? Why do python tools seem to always make installing the latest version such an obtuse process compared to ruby (gem update vs pip list --outdated | awk '!/Could not|ignored/ { print $1 }' | xargs pip install --upgrade)? I hope I’m the one missing something, but I can never find easy solutions for this online.

Eldred answered 16/4, 2015 at 22:34 Comment(2)
I have no experience with pyenv but looking at the source of the install command it appears there really is no such built-in command. You could add a script containing the command line you already have to the path pyenv/libexec though (e.g. pyenv/libexec/pyenv-update) which you could then call as pyenv update.Caseose
I have no issue with using the command, I was just wondering if there’s a default easier way. Python tools always seem incredibly complicated in these regards, compared to ruby.Eldred
E
47

Since pyenv 2.3.6 (November 2022) it will automatically resolve a prefix version to the latest revision:

pyenv install 3.10

Note that, as the link indicates, pyenv uninstall does not do prefix resolution, it needs the full version upfront.

You may need to update your pyenv if it's too old (pyenv -v will tell you the version of pyenv itself)


old outdated answer:

FWIW as of version 1.2.24 (March 2021) this issue is finally fixed:

pyenv install 3.10:latest

pyenv/pyenv#1831 lets you suffix any section of version with :latest (just avoid :latest alone it yields weird results) to get the latest revision for that section e.g. right now 3:latest will install 3.11 alpha, 3.10:latest will install 3.10.0.

It's not quite perfect when dealing with non-mainline, and :latest doesn't work in every context, but it's progress.

Eduardo answered 9/12, 2021 at 13:43 Comment(5)
Sadly you cannot do pyenv global 3.10:latest to select it, aliases do not work here. I mention this because I am trying to write a bootstrap script that installs or updates the latest versions.Millisecond
pyenv update doesn't work for me, but pyenv install 3.10:latest worked fine.Lather
I believe pyenv update is provided by github.com/pyenv/pyenv-updateHolmic
As of pyenv v2.3.3, you can do install 3.x:latest but not local 3.x:latest, annoyingly. The alias seems to only work with the install command. At least it's something.Lannielanning
as of pyenv 2.3.11 for me works to just run pyenv install 3.10 and it will grab the newest 3.10 version. pyenv install 3 also works, but it will install alpha releases as well, not only stable.Amal
P
12

Try https://github.com/momo-lab/pyenv-install-latest

Installation...

git clone https://github.com/momo-lab/pyenv-install-latest.git "$(pyenv root)"/plugins/pyenv-install-latest

Latest 2.7 build of python...

pyenv install-latest 2.7

and for python 3...

pyenv install-latest

Pretext answered 4/4, 2017 at 15:47 Comment(7)
Thank you, but to do that, I could just stick with my command. The question was if there was an official way to do it that was less obtuse.Eldred
@Eldred This comment makes little sense. ruby-install is not more official than pyenv install-latest. It doesn't come with any official ruby installer or distro package management. This is the correct answer I see no reason not to mark it as such.Physicist
For the record, the repository was updated to point at this one instead which ironically covers rbenvPhysicist
@Physicist Official for pyenv, not official for python. This is not the correct answer, because it does not solve the issue, it just requires extra software to be installed (and like I said, to do that I’m better off using what I already had).Eldred
@Eldred I know you've got what you need. Just saying ruby-install is extra software, and so is this, and it is a plugin which is part of the ecosystem and it is made for this specific problem.Physicist
@Physicist ruby-install is extra software on top of ruby. This solution is extra software on top of pyenv, which is extra software on top of python, and it’s useless for my needs. I’m not going to add a dependency on top of another dependency for no gain. I understood your point, it’s you who aren’t understanding mine. This answer doesn’t solve the issue. The comment above stating there is no way is more accurate.Eldred
Well this is all subjective, then. I came here with the exact same problem and found the exact same solution and it works fine for me which apparently means nothing.Physicist
C
11

The following is a little shorter than your suggested "hack" and assumes you don't want versions like 3.130a2 and 3.5.0b1.

pyenv install $(pyenv install --list | grep -v - | grep -v a | grep -v b | tail -1 | tr -d ' ')

Crawler answered 29/10, 2015 at 20:27 Comment(2)
It’s not shorter in any meaningful way, though. Using -v instead of --invert-match isn’t really important. You have one less pipe, yes, but you’re also not removing the leading spaces. Finally, you’re solution won’t catch versions like 3.5.0a1 (though I should update mine to catch b as well.Eldred
(1) Add another grep -v a to avoid alpha versions (2) Add | tr -d ' ' at the end of the pipes so that the output can be passed like pyenv install $(pyenv install --list | grep -v - | grep -v a | grep -v b | tail -1 | tr -d ' ')Gummy
B
5

Combining this with this answer, another option is:

pyenv install --list | grep --extended-regexp "^\s*[0-9][0-9.]*[0-9]\s*$" | tail -1 | tr -d ' '

The regex looks for lines that start with a number ^[0-9], followed by any amount of dots and/or numbers [0-9.]*, and end with a number [0-9]$. Leading ^\s* or trailing \s*$ whitespaces may occur but don't have to.

Edit: to install:

pyenv install $(pyenv install --list | grep --extended-regexp "^\s*[0-9][0-9.]*[0-9]\s*$" | tail -1 | tr -d ' ')
Benjamin answered 28/1, 2021 at 18:49 Comment(2)
That doesn’t work for the stated purposed. Try to use that to install the latest version directly (inside pyenv install "$()" and it will give an error.Eldred
@Eldred Add | tr -d ' ' at the end of the pipes: pyenv install $(pyenv install --list | grep --extended-regexp "^\s*[0-9][0-9.]*[0-9]\s*$" | tail -1 | tr -d ' ')Gummy
G
-7

Because being on the latest "stable" version of everything is rarely a good idea. Different upstream maintainers have a different concept of stable (my little pymumble fork and eglibc have very different notions of release quality). The newest stable version often introduces breaking changes and it is often inadvisable to upgrade blindly without understanding what changes you're bringing into your codebase.

In ruby's case, 1.8's threads were greenthreads and 1.9's threads were kernel threads. While they maintained the same API, completely changing the underlying threading module when your language supports C gems is not acceptable in any universe known to me. Upgrading any multi-threaded code to the latest stable ruby was highly likely to break everything. Arch Linux had a similar fiasco when it upgraded everyone to python 3, ignoring the myriad dependencies it their own repos against python 2.

The usual solution is to depend on your distro's repo for new version of python and use python's virtualenv or python3's venv to create environments based on that specific version of python.

Gelatin answered 16/4, 2015 at 23:27 Comment(4)
I never mentioned upgrading. We’re talking about the first command you run to first setup the environment, and at that time, I want the latest version.Eldred
You spoke about upgrading gems/eggs. In any case, the usual approach is to just use the version from your distro's repo or make a venv from it.Gelatin
I’m using OS X, not a Linux distro. I mentioned upgrading solely as an example of another python task that is unnecessarily obtuse when compared to ruby.Eldred
OK, but surely you agree that both the "problems" you run into come from the same difference in philosophy.Gelatin

© 2022 - 2024 — McMap. All rights reserved.