Be sure you're doing:
$ poetry run SOME_COMMAND
# such as `poetry run pytest`
Instead of just:
$ SOME_COMMAND
# such as `pytest`
My problem (in detail) was...
# Starting with a _contrasting_ example that _unexpectedly worked_..
# A long time ago I did..
$ pip install pytest
# And now, I was doing everything below..
$ poetry add requests
# ..Then I wrote code that has `import requests` in it
# ..Then I wrote some unit test code (to use with pytest) to test the use of `requests`
# ..And NOT knowing I'm supposed to do `poetry run pytest`, I was just doing
$ pytest
# ..And it (oddly) worked, perhaps because maybe `requests` had been installed globally somewhere for me.
# ..But then I did
$ poetry add python-dotenv
# ..Then I wrote code that had `from dotenv import load_dotenv` in it
# ..Then I wrote some unit test code (to use with pytest) to test the use of `python-dotenv`
# And I got the error I should have gotten..
$ pytest
# ..a bunch of error output, including..
ModuleNotFoundError: No module named 'dotenv'
Thus, the fix was:
# Get rid of the global pytest. Don't want to use that.
# (I think this step is optional, b/c I think `poetry run pytest` below will use the pytest installed via poetry in your virtual env (i.e. I _think_ it will (on it's own) NOT use the globally installed pytest.))
$ pip uninstall pytest
$ poetry add python-dotenv
$ poetry add --dev pytest
$ poetry run pytest
Credit for the fix goes to:
https://github.com/joeyespo/pytest-watch/issues/112
dotenv
? – Subwaypip install python-dotenv
– Rodrigopython3.8 -m pip install python-dotenv
– Tutti