Pytest and Python 3

PythonPython 3.xPytest

Python Problem Overview


I've installed pytest 2.3.4 under Debian Linux. By default it runs under Python 2.7, but sometimes I'd like to run it under Python 3.x, which is also installed. I can't seem to find any instructions on how to do that.

The PyPI Trove classifiers show Python :: 3 so presumably it must be possible. Aside from py.test somedir/sometest.py, I can use python -m pytest ..., or even python2.7 -m pytest ..., but if I try python3 -m pytest ... I get

/usr/bin/python3: No module named pytest

Python Solutions


Solution 1 - Python

I found a workaround:

  1. Installed python3-pip using aptitude, which created /usr/bin/pip-3.2.
  2. Next pip-3.2 install pytest which re-installed pytest, but under a python3.2 path.
  3. Then I was able to use python3 -m pytest somedir/sometest.py.

Not as convenient as running py.test directly, but workable.

Solution 2 - Python

python3 doesn't have the module py.test installed. If you can, install the python3-pytest package.

If you can't do that try this:

  1. Install virtualenv
  2. Create a virtualenv for python3
    • virtualenv --python=python3 env_name
  3. Activate the virtualenv
    • source ./env_name/bin/activate
  4. Install py.test
    • pip install py.test
  5. Now using this virtualenv try to run your tests

Solution 3 - Python

This worked for me:

python3 $(which py.test) ...

Solution 4 - Python

In addition to the aforementioned python3 -m pytest, this might also work:

env pytest

Solution 5 - Python

Install it with pip3: pip3 install -U pytest

Solution 6 - Python

Debian Linux has the python3-pytest package where if installed you can simply run:

/usr/bin/pytest-3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoe AbbateView Question on Stackoverflow
Solution 1 - PythonJoe AbbateView Answer on Stackoverflow
Solution 2 - PythonJoeView Answer on Stackoverflow
Solution 3 - PythonXPlatformerView Answer on Stackoverflow
Solution 4 - PythonGPHemsleyView Answer on Stackoverflow
Solution 5 - PythonmjakicView Answer on Stackoverflow
Solution 6 - PythonPaul BackView Answer on Stackoverflow