Skip to content

Running with pytest-xdist changes sys.path #376

@nedbat

Description

@nedbat

A simple test to show sys.path (and then fail, to always see the output):

$ cat test_sys_path.py
import pprint, sys

def test_sys_path():
    pprint.pprint(sys.path)
    1/0
$ pip freeze
apipkg==1.5
atomicwrites==1.2.1
attrs==18.2.0
execnet==1.5.0
more-itertools==4.3.0
pluggy==0.8.0
py==1.7.0
pytest==3.10.1
pytest-forked==0.2
pytest-xdist==1.24.1
six==1.11.0
$ pytest -n 0 -q
F                                                                                                                                                        [100%]
=========================================================================== FAILURES ===========================================================================
________________________________________________________________________ test_sys_path _________________________________________________________________________

    def test_sys_path():
        pprint.pprint(sys.path)
>       1/0
E       ZeroDivisionError: division by zero

test_sys_path.py:5: ZeroDivisionError
--------------------------------------------------------------------- Captured stdout call ---------------------------------------------------------------------
['/private/var/folders/j2/gr3cj3jn63s5q8g3bjvw57hm0000gp/T/pytest_sys_path',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/bin',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/lib/python36.zip',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/lib/python3.6',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/lib/python3.6/lib-dynload',
 '/usr/local/py/cpython-v3.6.7/lib/python3.6',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/lib/python3.6/site-packages']
1 failed in 0.05 seconds
$ pytest -n 1 -q
gw0 [1]
scheduling tests via LoadScheduling
F                                                                                                                                                        [100%]
=========================================================================== FAILURES ===========================================================================
________________________________________________________________________ test_sys_path _________________________________________________________________________
[gw0] darwin -- Python 3.6.7 /usr/local/virtualenvs/tmp-9cf2e53a94c913c/bin/python3.6

    def test_sys_path():
        pprint.pprint(sys.path)
>       1/0
E       ZeroDivisionError: division by zero

test_sys_path.py:5: ZeroDivisionError
--------------------------------------------------------------------- Captured stdout call ---------------------------------------------------------------------
['/private/var/folders/j2/gr3cj3jn63s5q8g3bjvw57hm0000gp/T/pytest_sys_path',
 '',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/lib/python36.zip',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/lib/python3.6',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/lib/python3.6/lib-dynload',
 '/usr/local/py/cpython-v3.6.7/lib/python3.6',
 '/usr/local/virtualenvs/tmp-9cf2e53a94c913c/lib/python3.6/site-packages']
1 failed in 0.54 seconds

The second entry in sys.path is different: without xdist, it's the bin directory of the virtualenv. With xdist, it's an empty string (current directory).

xdist shouldn't change this, it means my tests behave differently under the two scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions