How do I use pytest with virtualenv?

There is a bit of a dance to get this to work:

  1. activate your venv : source venv/bin/activate
  2. install pytest : pip install pytest
  3. re-activate your venv: deactivate && source venv/bin/activate

The reason is that the path to pytest is set by the sourceing the activate file only after pytest is actually installed in the venv. You can’t set the path to something before it is installed.

Re-activateing is required for any console entry points installed within your virtual environment.

Leave a Comment