即使存在垫片,Tox 也找不到 python3.6。我的 pyenv 设置有什么问题?

boa*_*der 4 python tox pyenv

试图为陷入失修状态的 django 包注入活力。他们使用 tox 进行测试,所以我在我的 MacBook 上设置了 pyenv。我已经安装了 3 个版本的 python,如下所示,一切看起来都应该可以工作,但如果是这样,我就不会问为什么不行。

我用 ~ 替换了我的主目录,使其更易于阅读。

pyenv 已安装,brew install pyenv并安装了各种版本的 pythonpyenv install #.#.#

垫片存在:

$ 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
Run Code Online (Sandbox Code Playgroud)

但是执行它们并没有按预期工作:

$ 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
Run Code Online (Sandbox Code Playgroud)

和 tox 失败是这样的:

  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
Run Code Online (Sandbox Code Playgroud)

但是在 .tox 文件夹中,您会找到这些可以手动激活的 VirtualEnv。

$ ls .tox
dist        docs        flake8      log     py34-1.11   py36-1.11   py36-2.0    py36-latest
Run Code Online (Sandbox Code Playgroud)

因为在某些时候它正在工作......

我确实理解为什么它不工作的机制,我不明白的是为什么 pyenv 没有正确设置环境(或者这正是它应该表现的方式)。我读到的一切似乎都表明 python3.6 应该启动一个 python3.6.8 解释器

$ 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
Run Code Online (Sandbox Code Playgroud)

Ant*_*ile 6

pyenv默认情况下 “本地”选择 python,即它查找PYTHON_VERSION环境变量或.python-version文件。

我个人觉得这个设置有点麻烦(需要在所有项目中散布这些文件,特别是在需要多个版本的项目中)。幸运的是,您可以使用默认版本的 python 在任何地方使用这些“垫片”功能pyenv global #.#.#

在您的情况下,要使python3.6shim 执行3.6.8而无需设置.python-version文件,您需要运行pyenv global 3.6.8- 您也可以针对不同的 python 版本多次运行:pyenv global 3.6.8 3.5.6 ...

您可能没有在内部解决这些问题的原因tox是 tox 在执行时清除了环境,因此PYTHON_VERSION环境变量不会继续执行。您可以通过passenv=在您的tox.ini. 例如:

[testenv]
passenv = PYTHON_VERSION
Run Code Online (Sandbox Code Playgroud)