Tox not finding python3.6 even with the shim present. What is wrong with my pyenv setup?
Asked Answered
E

1

5

Trying to breathe some life back into a django package that has fallen into a state of disrepair. They use tox for testing so I've setup pyenv on my MacBook. I've installed 3 versions of python as you can see below, and everything looks like it should work, but if it was I wouldn't be asking why it is not.

I've replaced my home directory with ~ to make it a bit easier to read.

pyenv was installed with brew install pyenv and the various versions of python were installed with pyenv install #.#.#

The shims exist:

$ echo $PATH
~/.pyenv/shims:~/.platformsh/bin:/usr/local/sbin:...
$ which python3.6
~/.pyenv/shims/python3.6
$ which python3.4
~/.pyenv/shims/python3.4
$ which python3.5
~/.pyenv/shims/python3.5

But executing them does not work as expected:

$ pyenv local 3.4.9 3.5.6 3.6.8
$ python3.4
Python 3.4.9 (default, Feb 12 2019, 10:33:47)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python3.5
pyenv: python3.5: command not found

The `python3.5' command exists in these Python versions:
  3.5.6

$ python3.6
pyenv: python3.6: command not found

The `python3.6' command exists in these Python versions:
  3.6.8

And tox fails like this:

  py34-1.11: commands succeeded
ERROR:   py36-1.11: Error creating virtualenv. Note that some special characters (e.g. ':' and unicode symbols) in paths are not supported by virtualenv. Error details: InvocationError("Failed to get version_info for python3.6: pyenv: python3.6: command not found\n\nThe `python3.6' command exists in these Python versions:\n  3.6.8\n\n", None)
ERROR:   py36-2.0: Error creating virtualenv. Note that some special characters (e.g. ':' and unicode symbols) in paths are not supported by virtualenv. Error details: InvocationError("Failed to get version_info for python3.6: pyenv: python3.6: command not found\n\nThe `python3.6' command exists in these Python versions:\n  3.6.8\n\n", None)
  py36-latest: commands succeeded
  docs: commands succeeded

But in the .tox folder you will find these VirtualEnvs that can be activated manually.

$ ls .tox
dist        docs        flake8      log     py34-1.11   py36-1.11   py36-2.0    py36-latest

Because at some point it was working....

I do understand the mechanics of why it isn't working, what I don't understand is why pyenv is not setting up the environment correctly (Or maybe this is exactly how it is supposed to behave). Everything I read seems to indicate that python3.6 should launch a python3.6.8 interpreter

$ bash -x python3.6
+ set -e
+ '[' -n '' ']'
+ program=python3.6
+ [[ python3.6 = \p\y\t\h\o\n* ]]
+ export PYENV_ROOT=~/.pyenv
+ PYENV_ROOT=~/.pyenv
+ exec /usr/local/Cellar/pyenv/1.2.9/libexec/pyenv exec python3.6
pyenv: python3.6: command not found

The `python3.6' command exists in these Python versions:
  3.6.8
Eulaheulalee answered 13/2, 2019 at 15:0 Comment(0)
G
8

pyenv by default picks the python "locally", that is it looks for the PYTHON_VERSION environment variable or a .python-version file.

Personally I find this setup a little cumbersome (needing to have these files littered around all projects, especially in projects which need multiple versions). Fortunately, you can make these "shims" function anywhere with a default version of python by using pyenv global #.#.#

In your case, to make the python3.6 shim execute 3.6.8 without needing to set up the .python-version files, you'd run pyenv global 3.6.8 -- you can run this multiple times for different python versions as well: pyenv global 3.6.8 3.5.6 ...

The reason that you're likely not having these resolve inside tox is tox clears the environment when executing, so the PYTHON_VERSION environment variable will not carry through. You can turn that off by setting passenv= in your tox.ini. For example:

[testenv]
passenv = PYTHON_VERSION
Garv answered 3/3, 2019 at 23:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.