Ubuntu – Pytest is in PATH but not found

bashcommand linepathspython

I've installed pytest for python testing, but I get No such file or directory error when simply trying to run it from a project folder.

It's in a location that should be accessible through the PATH variable, but (to the best of my ability to describe the problem) isn't being 'found'. For some reason when I type 'pytest' my shell is looking to the wrong location; if I specify the location then pytest will run fine.

Looking in /usr/bin

kirk@kirk:~/develop/foo$ pytest
bash: /usr/bin/pytest: No such file or directory

It's actually in /usr/local/bin, which is part of the path, and works when I explicitly call that location.

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 =========================

What can cause this behavior?

Best Answer

To avoid searching your PATH every time an executable command is called, bash saves previously used commands in a lookup table, or hash.

If you subsequently move the executable or install another version elsewhere on the PATH, it is sometimes necessary to force the shell to 'forget' the old location - running help hash in the bash shell:

     -r                forget all remembered locations

or to forget just a single command

hash -d <command>

In this case, you seem to have had a previous version of pytest at /usr/bin/pytest: running hash -r pytest forced the shell to re-examine your PATH and find its current location /usr/local/bin/pytest.

Related Question