You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行pytest时提示命令未被识别的问题求助

运行pytest时提示命令未被识别的问题求助

问题详情

我在VS Code的终端里运行pytest命令时,系统提示命令未被识别。我已经检查过Python版本、确认了pytest的安装位置,甚至用管理员权限打开终端尝试,测试文件命名也是符合规范的test_sample.py,但还是出现了以下错误:

pytest : The term 'pytest' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:1 char:1

  • pytest
  • + CategoryInfo          : ObjectNotFound: (pytest:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    

我的解决经验分享

我之前也碰到过一模一样的问题,给你几个亲测有效的解决思路:

  • 优先试试用Python模块方式调用pytest:直接在终端输入 python -m pytest,这个命令会跳过系统PATH的限制,直接调用当前Python环境中安装的pytest,大部分情况下都能直接解决“命令未识别”的问题。
  • 确认终端是否激活了正确的虚拟环境:如果你用了虚拟环境(比如venv或者conda),VS Code的终端有时候不会自动激活对应的环境。Windows下如果是venv的话,你可以输入.\venv\Scripts\activate激活环境,激活成功后终端前缀会出现虚拟环境名称,这时候再运行pytest试试。
  • 检查pytest是否真的在当前环境:输入pip show pytest,如果能输出pytest的版本、安装路径等信息,说明已经安装;如果提示“pip不是内部或外部命令”,那说明当前终端的Python环境和你安装pytest的环境不一致,得切换到正确的环境。
  • 把pytest路径加到系统PATH:用pip show pytest找到Location字段,比如显示的路径是C:\Users\你的用户名\AppData\Local\Programs\Python\Python310\Lib\site-packages,那pytest的可执行文件一般在同级的Scripts文件夹里(比如C:\Users\你的用户名\AppData\Local\Programs\Python\Python310\Scripts),把这个Scripts路径添加到系统环境变量的PATH里,然后重启VS Code和终端再试。
  • 重新安装pytest兜底:如果上面的方法都不管用,先执行pip uninstall pytest卸载干净,再用pip install pytest重新安装,确保安装命令是在你当前使用的Python环境下执行的。

备注:内容来源于stack exchange,提问作者I need your help

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 18:23:00