首页 > 解决方案 > 如何在我的 python 虚拟环境中设置 pytest

问题描述

我按照https://sourabhbajaj.com/mac-setup/Python/virtualenv.html中的步骤在我的目录中设置一个 python 虚拟环境,我python -m venv testEnv做了source venv/bin/activate。我在 MacOSX 和 zsh 上。

但是当我这样做时pytest,我会收到类似的错误

 $ pytest                      
zsh: /Library/Frameworks/Python.framework/Versions/3.8/bin/pytest: bad interpreter: /usr/local/bin/python3: no such file or directory

这些是我的 python 和 pytest

❯ which python                  testEnv
python: aliased to /Users/hap497/.pyenv/shims/python
❯ which pytest                  testEnv
/Library/Frameworks/Python.framework/Versions/3.8/bin/pytest

那么我需要做什么才能在我的虚拟环境中设置 python 和 pytest?

标签: python

解决方案


如果你做了

python -m venv testEnv

那你想要

source testEnv/bin/activate

并不是

source venv/bin/activate

我们可以在which输出中看到 pytest 未正确安装到您的 venv。正确激活 testEnv 并再次安装 pytest。


推荐阅读