Kir*_*rst 7 python command-line bash paths
我已经安装了 pytest 进行 python 测试,但是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
Run Code Online (Sandbox Code Playgroud)
它实际上在 中/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 =========================
Run Code Online (Sandbox Code Playgroud)
什么会导致这种行为?
ste*_*ver 13
为避免PATH每次调用可执行命令时都进行搜索,请将bash以前使用的命令保存在查找表中,或hash 中。
如果您随后移动可执行文件或在 上的其他位置安装另一个版本PATH,有时需要强制外壳“忘记”旧位置 -help hash在 bash 外壳中运行:
-r forget all remembered locations
Run Code Online (Sandbox Code Playgroud)
或者忘记一个命令
hash -d <command>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您似乎有一个以前版本的pytestat /usr/bin/pytest: runninghash -r pytest迫使 shell 重新检查您的PATH并找到它的当前位置/usr/local/bin/pytest。