Pytest在PATH中但未找到
我已经为python测试安装了pytest,但是当我只是试图从项目文件夹中运行它时,我得到了No such file or directory
错误。
它位于一个应该可以通过PATH
变量访问的位置,但是(尽我所能描述问题)并没有“找到”。 出于某种原因,当我输入’pytest’时,我的shell正在寻找错误的位置; 如果我指定位置,那么pytest将运行良好。
在/usr/bin
查找
kirk@kirk:~/develop/foo$ pytest bash: /usr/bin/pytest: No such file or directory
它实际上位于/usr/local/bin
,它是路径的一部分,并在我明确调用该位置时起作用。
kirk@kirk:~/develop/foo$ whereis pytest pytest: /usr/local/bin/pytest kirk@kirk:~/develop/foo$ echo $PATH /home/kirk/bin:/home/kirk/.local/bin:/usr/local/sbin:/usr/local/bin: /usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin kirk@kirk:~/develop/foo$ /usr/local/bin/pytest ============================= test session starts ============================== platform linux2 -- Python 2.7.12, pytest-3.0.5, py-1.4.32, pluggy-0.4.0 rootdir: /home/kirk/develop/foo, inifile: pytest.ini collected 0 items ========================= no tests ran in 0.00 seconds =========================
什么可以导致这种行为?
为了避免每次调用可执行命令时都搜索PATH
, bash
会将先前使用的命令保存在查找表或哈希中 。
如果您随后移动可执行文件或在PATH
上的其他位置安装另一个版本,有时需要强制shell“忘记”旧位置 – 在bash shell中运行help hash
:
-r forget all remembered locations
或者忘记一个命令
hash -d
在这种情况下,您似乎在/usr/bin/pytest
上有一个pytest
的先前版本:运行hash -r pytest
强制shell重新检查您的PATH
并找到它的当前位置/usr/local/bin/pytest
。