找不到Py.test命令,但安装了库

E. *_*ero 14 terminal command pip pytest

关于此主题的堆栈溢出已经有两个帖子; 但是,他们都没有解决或解决我的具体情况.

我已经安装了pytest via pip install pytest.我也能用Python导入库.

问题是,当我尝试py.test在终端中使用该命令时,我得到了py.test: command not found.

有没有人知道为什么我无法在终端中使用该命令?

编辑:它甚至显示为已安装的包:

$ pip list
cycler (0.9.0)
matplotlib (1.5.1)
numpy (1.10.1)
pip (8.1.0)
py (1.4.31)
pyparsing (2.0.7)
pytest (2.9.0)
python-dateutil (2.4.2)
pytz (2015.7)
scipy (0.17.0)
setuptools (7.0)
six (1.10.0)
tensorflow (0.5.0)
vboxapi (1.0)
wheel (0.26.0)
Run Code Online (Sandbox Code Playgroud)

Ehs*_*ani 30

使用python -m pytest将适合你.或者,如果您使用虚拟环境并在virtualenv上安装了pytest,那么您应该在虚拟环境中运行py.test.

检查这个网站是有用的:http://pythontesting.net/framework/pytest/pytest-introduction/

  • @YanKingYin 重复问题:/sf/ask/3557491871/ (4认同)
  • “-m”是什么意思? (3认同)
  • @YanKingYin“当 -m 标志与命令行界面上的命令一起使用时,后跟 <module_name>,它允许模块作为可执行文件执行”。更多信息请参见:[Python M 标志的含义是什么?](https://appdividend.com/2021/02/18/what-is-the-meaning-of-python-m-flag/) (3认同)

Hug*_*ugo 8

我已经在 macOS 上安装了最新版本的 pytest,并安装了 Homebrew 安装的 Python 2.7 并修复了它:

pip uninstall pytest
pip install pytest
Run Code Online (Sandbox Code Playgroud)


Ugt*_*tar 5

您是否有机会在Mac上使用自制软件?

我有同样的问题,它基本上归结为权限/与mac os base安装的python冲突。pip install不会安装东西或将东西链接到/ usr / local / bin(virtualenv和pytest都发生了)。

  1. 我使用自制软件(brew uninstall python)完全卸载了python 2.7 。
  2. 接下来,我用homebrew重新安装了python来修复pip(它不是/ usr / local / bin / pip中的符号链接,它应该已经链接到Cellar了) brew install python
  3. 然后我卸载PIP与须藤 - sudo python -m pip uninstall pip删除root拥有画中画
  4. 现在,我再次使用homebrew卸载并重新安装了python,以正确的权限重新安装pip brew uninstall python && brew install python
  5. 接下来,我修复了python符号链接 brew link python
  6. 最后,pip install pytest工作了!(也是如此pip install virtualenv

我发现从这篇文章中选择的答案中的信息非常有帮助:https : //superuser.com/questions/915810/pip-not-working-on-hombrew-python-2-7-install

如果您不在Mac电脑上,请多多包涵...


Ben*_*aan 5

此答案假设您有 python3 和 pip3 - 如果没有,请从此答案中的命令中删除 3 。

安装 pytest 并使用命令确认它在您的 python 环境中可用后pip3 show pytest,如果您仍然收到类似zsh:command not found:pytest. 您有几个选择:

选项 1. 将库作为模块运行python3 -m pytest

选项 2. 通过简单地创建别名来创建永久解决方案。要创建别名,请执行以下步骤:

i) 通过运行查找 shell 配置文件的位置echo $SHELL。以下是 shell 配置文件的可能位置:

  • 重击:~/.bashrc
  • zsh:~/.zshrc
  • Ksh:~/.profile

例如,当我在 Mac 终端上运行时,我得到:/bin/zhs。这意味着在下一步中我需要引用该~/.zshrc文件。

ii) 一旦你弄清楚你的 shell 配置文件的名称。跑步echo "alias pytest='python3 -m pytest'" >> ~/.zshrc。这会将别名定义(在本例中为“pytest”)附加到您的 shell 配置文件中。

iii) 最后,通过编写以下命令重新加载您的配置文件:source ~/.zshrc

测试愉快!