question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pipenv wrongly chooses pre-release pythons if available

See original GitHub issue

Setup:

Using pyenv, install a python and a release candidate or beta of the same version. pyenv removes rc/beta pythons from pyenv when the final build is available, so you might have to create the build definition yourself. Put the following in $PYENV_ROOT/plugins/python-build/share/python-build/2.7.14rc1:

install_package "openssl-1.0.2k" "https://www.openssl.org/source/openssl-1.0.2k.tar.gz#6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0" mac_openssl --if has_broken_mac_openssl
install_package "readline-6.3" "https://ftpmirror.gnu.org/readline/readline-6.3.tar.gz#56ba6071b9462f980c5a72ab0023893b65ba6debb4eeb475d7a563dc65cafd43" standard --if has_broken_mac_readline
install_package "Python-2.7.14rc1" "https://www.python.org/ftp/python/2.7.14/Python-2.7.14rc1.tgz#4d1ca11c5451e1b7cafcabc2003cf80fb3745bac45ba51c39b855b0011a27408" ldflags_dirs standard verify_py27 ensurepip

(I had managed to have both the RC and final build installed because I had installed 2.7.14rc1 before the final version was out, then forgot to remove it).

Next, use pyenv shell 2.7.14 to set the version of python for the current shell

The Problem

When I run:

$ pipenv install --python 2.7.14
Creating a virtualenv for this project…
Using /Users/josh/.pyenv/versions/2.7.14rc1/bin/python2.7 to create virtualenv…

Pipenv is prefering the prerelease python over the one I specifically asked for

Explanation

  1. Pipenv detects pyenv is installed
  2. Pipenv scans $PYENV_ROOT/versions for installed pythons
  3. Pipenv prepends each python to $PATH as it finds them in the PATH
  4. Pipenv runs which python2.7 and finds ['/Users/josh/.pyenv/versions/2.7.14rc1/bin/python2.7', '/Users/josh/.pyenv/versions/2.7.14/bin/python2.7', '/usr/bin/python2.7'] (shortened a bit, i have a lot of pythons installed)
  5. Pipenv runs python --version on each and parses the output discarding the rc/beta status
  6. Since 2.7.14rc1 came first in the path, Pyenv picks it since without the rc1 suffix, it is the version I had asked for.

I dug through cli.py for a while trying to fix it and found some issues:

  • RC/Beta pythons sort last in the glob results when pipenv lists the contents of $PYENV_ROOT/versions, but end up first in $PATH
  • Even if the list of pythons is reversed when added to $PATH to restore the sort order, if 2.7.14 is the active python, it is already in the path, so Pipenv skips prepending that one, and only prepends 2.7.14rc1, causing it to take priority.

I have not yet been able to determine a way to fix this that doesn’t break pipenv --python 2.7 installing the latest 2.7.* version, for example.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
techalchemycommented, Oct 31, 2017

OK I have a fix!

Old Version

 /t/test  pipenv install --python 2.7.14 tablib
Creating a virtualenv for this project…
Using /home/hawk/.pyenv/versions/2.7.14rc1/bin/python2.7 to create virtualenv…
⠋Running virtualenv with interpreter /home/hawk/.pyenv/versions/2.7.14rc1/bin/python2.7
New python executable in /home/hawk/.local/share/virtualenvs/test-hxkKlP5o/bin/python2.7
Also creating executable in /home/hawk/.local/share/virtualenvs/test-hxkKlP5o/bin/python
Installing setuptools, pip, wheel...done.

Fix!

 /t/test  pipenv --rm
Removing virtualenv (/home/hawk/.local/share/virtualenvs/test-hxkKlP5o)…
 /t/test  pipenv install --python 2.7.14 tablib
Creating a virtualenv for this project…
Using /home/hawk/.pyenv/versions/2.7.14/bin/python2.7 to create virtualenv…
⠋Running virtualenv with interpreter /home/hawk/.pyenv/versions/2.7.14/bin/python2.7
New python executable in /home/hawk/.local/share/virtualenvs/test-hxkKlP5o/bin/python2.7
Also creating executable in /home/hawk/.local/share/virtualenvs/test-hxkKlP5o/bin/python
Installing setuptools, pip, wheel...done.
1reaction
joshfriendcommented, Oct 30, 2017

As an amusing side note, 2.7 is a valid python version (it’s extremely old though, everyone reading: please please please do not use it 😝 ), and if I tried to create a virtualenv with that version specifier, I would get 2.7.14 currently instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common Pipenv Errors - Towards Data Science
A collection of Pipenv errors and their solutions. ... locking fails is if a package in the Pipfile only exists as a pre-release...
Read more >
pipenv Documentation - Read the Docs
Automatically install required Python version when pyenv is available. • Automatically finds your project home, recursively, by looking for a Pipfile.
Read more >
Frequently Encountered Pipenv Problems - Read the Docs
Uninstall all existing Pipenv installations, and see ☤ Installing Pipenv to choose one of the recommended way to install Pipenv instead. ☤ My...
Read more >
Why is pipenv telling me the wrong version of python?
If there is no Pipfile yet, pipenv will create one and set python_version correctly.
Read more >
pipenv Documentation
or refer to the Installing Python section of The Hitchhiker's Guide to ... Additionally, you'll need to make sure you have pip available....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found