Py.test command not found, but library is installed
Asked Answered
B

17

76

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)
Bukhara answered 14/3, 2016 at 22:5 Comment(1)
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.Murrey
P
147

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/

Parted answered 15/3, 2016 at 9:14 Comment(4)
What is the meaning of -m?Phineas
@YanKingYin Duplicate question: https://mcmap.net/q/22857/-what-is-the-effect-of-using-python-m-pip-instead-of-just-pip-duplicate/…Cohen
@YanKingYin "When the -m flag is used with a command on the command-line interface, followed by a <module_name>, it allows the module to be executed as an executable file". More here: What Is The Meaning Of Python M Flag?Uncanonical
The link is dead. Can you update it?Polygraph
P
17

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
Pectoralis answered 2/1, 2017 at 12:44 Comment(0)
U
12

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

Unregenerate answered 3/8, 2016 at 19:43 Comment(2)
This worked for me on a Mac. Thanks for the answer.Dinesh
Worked for me - thanks. Lots of Mac issues with recent disto upgradesSaxen
B
5

This answer assumes that you have python3 and pip3 - if not remove 3 from the commands in this answer.

After installing pytest and confirming that it is available in your python environment with pip3 show pytest command, if you are still getting the error like zsh:command not found:pytest. You have a few options:

Option 1. Run the library as a module with python3 -m pytest.

Option 2. Create a permanent solution by simply creating an alias. To create an alias, follow these steps:

i) Find the location of your shell profile file by running echo $SHELL. The following are the potential locations of your shell profiles:

  • Bash: ~/.bashrc
  • Zsh: ~/.zshrc
  • Ksh: ~/.profile

For instance, when I run on my Mac terminal I get: /bin/zhs. This means that in the next step I need to refer to the ~/.zshrc file.

ii) Once you figure out the name of your shell profile file. Run echo "alias pytest='python3 -m pytest'" >> ~/.zshrc. This will append the alias definition, which in this case is 'pytest', to your shell profile file.

iii) Finally, reload your profile file by writing this command: source ~/.zshrc.

Happy Testing!!!

Bankrupt answered 28/3, 2023 at 21:52 Comment(0)
H
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

Homiletic answered 14/11, 2019 at 7:54 Comment(0)
P
1

This worked for me on Ubuntu 22.04, following this GitHub issue comment:

I checked that pytest was in ~/.local/bin, and noticed this path wasn't in the $PATH variable. I edited ~/.bashrc and added the following line at the end:

export PATH="$PATH:~/.local/bin"

I then ran the command source ~/.bashrc to reload the ~/.bashrc profile in the current bash terminal, ran the command pytest, and then it worked.

Pasho answered 19/12, 2023 at 19:35 Comment(0)
O
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.

Opal answered 4/10, 2016 at 4:44 Comment(0)
E
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

Espino answered 13/7, 2020 at 18:56 Comment(0)
K
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.

Krug answered 21/1, 2021 at 6:50 Comment(0)
O
0

For MAC users:

Download python universal installer for mac:
https://www.python.org/ftp/python/3.10.5/python-3.10.5-macos11.pkg

Then try to install pytest module in terminal using this command:
pip install pytest

Hope this will fix the issue. Thanks!

Olgaolguin answered 19/7, 2022 at 14:17 Comment(1)
the question is not about how to install pytest, OP already has done that, please read the problem carefully.Aude
B
0

use the command, pip install -U pytest and install it in your cmd prompt, it will solve the issueenter image description here

Bandsman answered 3/9, 2022 at 5:45 Comment(0)
B
0

I used macbook air m2, and the way I deal with this problem is: Command in terminal in macbook:

which pytest

/opt/anaconda3/bin/pytest -> my terminal shows this

Then you got the path of pytest, in the "Command", before "pytest", add its path and following with the path of python file you wanna test.

/opt/anaconda3/bin/pytest /Users/cindyng/Desktop/Testing.py

Done, and if you cannot find the path of python in macbook, "which python" also helps, and you can put it in "Home" and "Custom Python Builder".

Hope that helps, good luck!

Bushcraft answered 29/9, 2022 at 16:29 Comment(0)
E
0
  1. Install pytest: If you haven't already installed pytest, you can install it using a package manager like pip. Run the following command in your terminal:
#!/bin/bash
 pip install pytest

This command will install pytest and its dependencies.

Check your PATH: Make sure that the directory containing the pytest executable is included in your PATH environment variable. You can check your current PATH by running the following command:

 echo $PATH

If the directory containing pytest is not included in your PATH, you can add it by editing your shell configuration file (e.g. ~/.bashrc or ~/.zshrc) and adding the following line:

#!/bin/bash
 export PATH=$PATH:/path/to/pytest

Replace "/path/to/pytest" with the actual path to the directory containing pytest.

Check your shell: Make sure that you are running the correct shell and that it is configured correctly. If you are using zsh, make sure that your shell configuration file (e.g. ~/.zshrc) contains the necessary configuration for pytest. For example, you may need to add the following line to your ~/.zshrc file:

#!/bin/bash
 autoload -Uz compinit && compinit

This line ensures that zsh's or bash's tab completion system is set up correctly for pytest.

I hope this helps! Let me know if you have any other questions.

Eiten answered 12/5, 2023 at 7:39 Comment(0)
H
0

Most likely you installed it without Administrator credentials, like when using,

pip install pytest --user

This will install it in a local folder instead of the main folder defined on the windows Path Environment Variables.

Open a windows terminal as Administrator, and then uninstall the installed pytest and install it again, like this:

pip uninstall pytest
pip install pytest

So, always install packages with Administrator credentials in order to be in the right path!

Hedrick answered 24/8, 2023 at 14:32 Comment(0)
S
0

I faced the same issue in terminal: py.test

zsh: command not found: py.test

The below steps solved the problem in Mac:

  1. cd to directory where pytest file is located.
  2. python3 -m pytest: this will run the tests present in the directory.
  3. pytest -v: to get more details of test run.
  4. pytest -v -s: to get further more details of test run.
Sevigny answered 12/4, 2024 at 11:9 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Comyns
N
-1

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

Niko answered 17/3, 2019 at 11:29 Comment(0)
I
-2

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

Infusive answered 15/9, 2021 at 7:5 Comment(1)
This is the same solution as in the accepted answer. 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.Duala

© 2022 - 2025 — McMap. All rights reserved.