pythonpytesttox

python3.6 pytest failing with "future feature annotations is not defined"


Running pytests using the python3.6 tox. it started failing with following error.

python3.6 tox failing with following error:

    Error processing line 1 of /home/test/sample/.tox/py36/lib/python3.6/site-packages/_virtualenv.pth:
    
      Traceback (most recent call last):
        File "/usr/lib/python3.6/site.py", line 174, in addpackage
          exec(line)
        File "<string>", line 1, in <module>
        File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/_virtualenv.py", line 3
          from __future__ import annotations
                                           ^
      SyntaxError: future feature annotations is not defined
    
    Remainder of file ignored
    Error processing line 1 of /home/test/sample/.tox/py36/lib/python3.6/site-packages/_virtualenv.pth:
    
      Traceback (most recent call last):
        File "/usr/lib/python3.6/site.py", line 174, in addpackage
          exec(line)
        File "<string>", line 1, in <module>
        File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/_virtualenv.py", line 3
          from __future__ import annotations
                                           ^
      SyntaxError: future feature annotations is not defined
    
    Remainder of file ignored
    Traceback (most recent call last):
      File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
        "__main__", mod_spec)
      File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/pip/__main__.py", line 29, in <module>
        from pip._internal.cli.main import main as _main
      File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/pip/_internal/cli/main.py", line 9, in <module>
        from pip._internal.cli.autocompletion import autocomplete
      File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/pip/_internal/cli/autocompletion.py", line 10, in <module>
        from pip._internal.cli.main_parser import create_main_parser
      File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/pip/_internal/cli/main_parser.py", line 9, in <module>
        from pip._internal.build_env import get_runnable_pip
      File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/pip/_internal/build_env.py", line 19, in <module>
        from pip._internal.cli.spinners import open_spinner
      File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/pip/_internal/cli/spinners.py", line 9, in <module>
        from pip._internal.utils.logging import get_indentation
      File "/home/test/sample/.tox/py36/lib/python3.6/site-packages/pip/_internal/utils/logging.py", line 8, in <module>
        from dataclasses import dataclass
    ModuleNotFoundError: No module named 'dataclasses'

Solution

  • Both data classes and the future import are Python 3.7 only features. So you either need to use at least Python 3.7 or change your code.

    See https://github.com/jugmac00/python-version-cheat-sheet#python-37