48

There are already two posts on stack overflow on this topic; however, none of them have resolved or addressed my specific situation.

I have installed pytest via pip install pytest. I am able to import the library in Python as well.

The problem is that when I try to use the py.test command in Terminal, I get py.test: command not found.

Does anyone have any insight as to why I am not able to use the command in the terminal?

EDIT: It even shows up as an installed package:

$ pip list
cycler (0.9.0)
matplotlib (1.5.1)
numpy (1.10.1)
pip (8.1.0)
py (1.4.31)
pyparsing (2.0.7)
pytest (2.9.0)
python-dateutil (2.4.2)
pytz (2015.7)
scipy (0.17.0)
setuptools (7.0)
six (1.10.0)
tensorflow (0.5.0)
vboxapi (1.0)
wheel (0.26.0)
E. Otero
  • 671
  • 1
  • 7
  • 13
  • 3
    Are you using a virtualenv? If so, `pip` (at least older versions) will not install the `py.test` binary if it's already installed systemwide. You can use `python -m pytest` instead. – The Compiler Mar 15 '16 at 05:31

9 Answers9

99

using python -m pytest will work for you.

Or if you using virtual environment and installed pytest on virtualenv you should then run py.test alongside your virtual environment.

Check this website can be useful:http://pythontesting.net/framework/pytest/pytest-introduction/

Jason
  • 7,978
  • 4
  • 33
  • 34
Ehsan Maiqani
  • 1,314
  • 12
  • 16
  • 4
    What is the meaning of `-m`? – Yan King Yin Feb 20 '19 at 08:12
  • 3
    @YanKingYin Duplicate question: https://stackoverflow.com/questions/50821312/meaning-of-python-m-flag/50821426 – Victory Omole Feb 20 '19 at 20:30
  • @YanKingYin "When the -m flag is used with a command on the command-line interface, followed by a , it allows the module to be executed as an executable file". More here: [What Is The Meaning Of Python M Flag?](https://appdividend.com/2021/02/18/what-is-the-meaning-of-python-m-flag/) – ellockie Apr 05 '22 at 11:02
11

Are you on a mac with homebrew by any chance?

I had the same issue and it basically came down to permissions/conflict with the mac os base installed python. pip install would not install or link stuff into /usr/local/bin (it happened with both virtualenv and pytest).

  1. I uninstalled python 2.7 completely with homebrew (brew uninstall python).
  2. Next, I reinstalled python with homebrew to fix pip (it was not a symlink in /usr/local/bin/pip where it should have been linked to Cellar) -- brew install python
  3. Then I uninstalled pip with sudo -- sudo python -m pip uninstall pip to remove the pip owned by root
  4. Now I uninstalled and reinstalled python with homebrew again to reinstall pip with the correct permissions brew uninstall python && brew install python
  5. Next I fixed the python symlinks brew link python
  6. Finally, pip install pytest worked! (and so did pip install virtualenv)

I found the information in the chosen answer from this post very helpful: https://superuser.com/questions/915810/pip-not-working-on-hombrew-python-2-7-install.

If you're not on a mac, sorry for the noise...

Community
  • 1
  • 1
Ugtar
  • 300
  • 2
  • 7
9

I already had the latest version of pytest on macOS with Homebrew-installed Python 2.7 and this fixed it:

pip uninstall pytest
pip install pytest
Hugo
  • 25,519
  • 6
  • 77
  • 92
1

I may be late, but while exploring this I noticed that this can be because the Scripts folder for python is not present in the PATH.

For me this is my scripts folder:

C:\Python38\Scripts\

If the path is a problem then running pip install pytest should actually you give you the warning with the path it was added to.

This should be present in the path. If on windows, edit the environment variables and this location to the PATH.

For me the path was incorrect because of an improper installation of python

Winston Jude
  • 539
  • 2
  • 11
0

I had the same issue. I had pytest v2.8.3 installed and the binary was on my path but under the name py.test. Upgrading to v3.0.3 added the regular pytest executable to the path.

toes
  • 563
  • 5
  • 13
0

I Fixed this issue via below steps. 1.First uninstall existing pytest. 2.Check python version. 3.then verify pytest version is supported with python version or not via github issue tracker. 4. via sudo install pytest sudo pip install pytest 5. verify pytest version and insatlled correctly or not. pip list pytest --version

6.run any test using pytest test_abc.py

jayesh
  • 2,449
  • 1
  • 16
  • 7
0

I had the same problem. I have changed the Python installed folder permission to full access. And then uninstalled the pytest and installed again.

pip uninstall pytest

Sujani
  • 1
  • 1
0

In my case, I had a similar issue in ubuntu 20.04. The below solution worked for me. Cause: Shell remembers the previous version or previously used Path, hence we need to force the shell to 'forget' the old location - with -r

hash -r pytest 

Then execute the tests it should work fine.

Mahadev Gouda
  • 707
  • 9
  • 12
-1

I encounter the same problem, python -m pytest works for me.

Avinash Yadav
  • 691
  • 5
  • 13
ryan x
  • 1
  • This is the same solution as in [the accepted answer](https://stackoverflow.com/a/36006909/2227743). *When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.* – Eric Aya Sep 15 '21 at 10:17