nox does not find my pyenv versions in my Python Poetry project

1.3k views Asked by At

I have a python project and I have tried to follow the Hypermodern Python Project guidelines for testing with Poetry and nox and pyenv.

This is on Debian 10 running on WLS2.

> lsb_release -a
Distributor ID: Debian
Description:    Debian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

I have these versions of Python installed with pyenv:

> pyenv versions
  system
  3.10.2
* 3.7.12 
* 3.8.12 
* 3.9.10 

and versions 3.7.12, 3.8.12 and 3.9.10 are enabled for this project with pyenv local.

In my noxfile.py my tests session is like this:

@session(python=["3.7", "3.9", "3.8"])
def tests(session: Session) -> None:
    """Run the test suite."""
    session.install(".")
    session.install("coverage[toml]", "pytest")
    session.run("pytest", *args)

I would expect that when I invoke nox to run my tests with poetry run nox -s tests, it would run in all three of these versions of Python, as shown in the Hypermodern example.

Instead, I see this:

nox > Running session tests-3.7
nox > Creating virtual environment (virtualenv) using python3.7 in .nox/tests-3-7
nox > poetry build --format=wheel
...
nox > Session tests-3.7 was successful.
nox > Running session tests-3.9
nox > Session tests-3.9 skipped: Python interpreter 3.9 not found.
nox > Running session tests-3.8
nox > Session tests-3.8 skipped: Python interpreter 3.8 not found.
nox > Ran multiple sessions:
nox > * tests-3.7: success
nox > * tests-3.9: skipped
nox > * tests-3.8: skipped

I have tried various combinations of pyenv global and pyenv local. I have run poetry upgrade after setting the multiple version with pyenv. Ditto re-running poetry install.

I have been unsuccessful searching for an answer on the interwebs and I'm hoping someone here can point me on the right path.

I ran pyenv shell 3.8.12 etc., as hinted in a related StackOverflow, but that did not seem to do the trick. That same article suggested updating ~/.profile and ~/.bashrc, but that did not help either.

I uninstalled and reinstalled poetry:

curl -sSL https://install.python-poetry.org | python3 - --uninstall
curl -sSL https://install.python-poetry.org | python3 -

and no change.

I also chased the ideas from this StackOverflow, to no avail.

1

There are 1 answers

0
Tom Willis On BEST ANSWER

Solution: Based on the tip from Espoir Murhabazi to focus on this StackOverflow, I focused on the pyenv leg of the triangle. After reading and experimenting more with getting my bash PATH right for pyenv, I was eventually able to get the expected results when I run poetry run nox -s tests after pyenv local 3.7.12 3.8.12 3.9.10. That is:

nox > Ran multiple sessions:
nox > * tests-3.7: success
nox > * tests-3.9: success
nox > * tests-3.8: success

Sweet success!

What ended up doing:

I removed any reference to Poetry and pyenv that had crept into my .bash_profile and my .bashrc. At the end of my .profile I ended up with these commands which did the trick:

...
# set PATH so it includes Poetry bin so I can manage my Python projects
export PATH="$HOME/.poetry/bin:$PATH"

# set up pyenv so I can use al the versions of Python I want
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv init --path)"

This sets up pyenv then poetry early in my path. I think they may have been later in the path before I started messing around. My PATH now looks like this, and all is well between pyenv, Poetry and nox. Thank you @Espoir Murhabazi.

PATH=~/.local/bin:/home/me/.pyenv/plugins/pyenv-virtualenv/shims:/home/me/.pyenv/shims:/home/me/.pyenv/bin:/home/me/.poetry/bin:/home/me/.local/bin:/home/me/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

Poring over the pyenv README helped arrive at this solution.