使用CMD运行Pytest遇两类错误:命令未识别与权限拒绝
解决CMD中运行Pytest的两类错误
一、'py.test' is not recognized as an internal or external command... 错误解决
- 确认pytest已安装:执行
pip list查看已安装包,未安装则运行pip install pytest。 - 替换命令:新版本pytest不再使用
py.test,直接用pytest命令启动测试。 - 配置环境变量:找到Python安装目录下的
Scripts文件夹(如C:\Python39\Scripts),将其路径添加到系统环境变量PATH中,重启CMD后重试。
二、ERROR: PermissionError: [WinError 5] Access is denied. 错误解决
- 管理员身份运行CMD:右键CMD图标,选择「以管理员身份运行」后执行
pytest。 - 检查文件/目录权限:右键测试文件所在文件夹,进入「属性」-「安全」,确保当前用户拥有读写权限。
- 转移测试文件:不要在系统保护目录(如
C:\Windows、Program Files)下运行测试,移到个人用户目录(如C:\Users\你的用户名\Documents)再执行。 - 使用虚拟环境:隔离依赖环境,避免权限冲突,执行以下命令:
python -m venv test_env test_env\Scripts\activate pip install pytest pytest
内容的提问来源于stack exchange,提问作者Bhakti Lad
相关产品推荐
相关产品推荐

