Unable to import 'pytest'

68 Views Asked by At

I want to test for raise, but getting Unable to import 'pytest'PylintE0401:import-error when importing pytest into test_main.py. My environment and editor are correctly configured and pytest installed.

Steps to reproduce:

  • Created environment with pipenv:
❯ pipenv --version
pipenv, version 2023.10.24

❯ pipenv shell

❯ pipenv --venv
/user/path/.local/share/virtualenvs/environment-name

❯ which python3
/user/path/.local/share/virtualenvs/environment-name/bin/python3
  • Correctly configured my editor to use the created virtual environment
  • Installed pipenv install --dev pytest
❯ pipenv graph
...
pytest==7.4.4
├── iniconfig [required: Any, installed: 2.0.0]
├── packaging [required: Any, installed: 23.2]
└── pluggy [required: >=0.12,<2.0, installed: 1.3.0]
  • Pipfile contains pytest
[dev-packages]
pytest = "*"
  • Contents of test_main.py
import pytest


def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        1 / 0

Am I missing something?

Any help is very much appreciated.

1

There are 1 best solutions below

1
Liap On

Try to run your test_main.py with this command

pipenv run python test_main.py

Doc: https://pipenv.pypa.io/en/latest/installation.html#using-installed-packages