我有一个python项目,我试图遵循Hypermodern python项目指南来测试Poetry
、nox
和pyenv
。
这是在WLS2上运行的Debian 10上。
> lsb_release -a
Distributor ID: Debian
Description: Debian GNU/Linux 10 (buster)
Release: 10
Codename: buster
我安装了以下版本的Python和pyenv
:
> pyenv versions
system
3.10.2
* 3.7.12
* 3.8.12
* 3.9.10
并且版本3.7.12、3.8.12和3.9.10与pyenv local
一起为该项目启用。
在我的noxfile.py
中,tests
会话如下:
@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)
我希望当我调用nox
来使用poetry run nox -s tests
运行测试时,它将在这三个版本的Python中运行,如Hypermodern示例所示。
相反,我看到的是:
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
我试过pyenv global
和pyenv local
的各种组合。在用pyenv
设置了多个版本之后,我已经运行了poetry upgrade
。同上,重新运行poetry install
。
我一直在网上寻找答案,但没有成功,我希望这里有人能为我指明正确的道路。
我运行了pyenv shell 3.8.12
等,正如相关StackOverflow中所暗示的那样,但这似乎并没有起到作用。同一篇文章建议更新~/.profile
和~/.bashrc
,但这也无济于事。
我卸载并重新安装了poetry
:
curl -sSL https://install.python-poetry.org | python3 - --uninstall
curl -sSL https://install.python-poetry.org | python3 -
没有变化。
我还从这个StackOverflow中寻找了一些想法,但没有成功。
解决方案:根据Espoir Murhabazi关于StackOverflow的提示,我将重点放在了三角形的pyenv
分支上。在阅读了更多关于让我的bashPATH
适合pyenv
的内容并进行了实验之后,当我在pyenv local 3.7.12 3.8.12 3.9.10
之后运行poetry run nox -s tests
时,我最终能够获得预期的结果。即:
nox > Ran multiple sessions:
nox > * tests-3.7: success
nox > * tests-3.9: success
nox > * tests-3.8: success
甜蜜的成功!
最终做了什么:
我删除了任何潜入我的.bash_profile
和.bashrc
的对Poetry
和pyenv
的引用。在我的.profile
结束时,我得到了以下命令:
...
# 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)"
这在我的路径早期设置了pyenv
和poetry
。我想在我开始捣乱之前,他们可能已经走到了后面。我的PATH
现在看起来是这样的,并且在pyenv
、Poetry
和nox
之间一切都很好。谢谢你@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
仔细阅读pyenv自述文件有助于找到这个解决方案。