This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Docs: "unittest discover" modifies sys.path
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, ezio.melotti, iritkatriel, michael.foord, rahul-kumi, rbcollins, redixin
Priority: normal Keywords: easy

Created on 2015-05-20 18:11 by redixin, last changed 2022-04-11 14:58 by admin.

Messages (3)
msg243681 - (view) Author: Sergey Skripnick (redixin) Date: 2015-05-20 18:11
"unittest discover" does add some directories to sys.path

later some modules can not work with modified sys.path

Here module `pdb` is trying to import standard module `cmd` but does import `cmd` module from `tests` directory:

"unittest discover" should not modify sys.path in any way.

$ mkdir /tmp/t
$ cd /tmp/t
$ mkdir tests
$ mkdir tests/cmd
$ touch tests/cmd/
$ echo "import pdb" > tests/
$ python -m unittest discover tests
ERROR: test_nothing (unittest.loader.ModuleImportFailure)
ImportError: Failed to import test module: test_nothing
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python2.7/unittest/", line 232, in _get_module_from_name
  File "/tmp/t/tests/", line 1, in <module>
    import pdb
  File "/usr/lib/python2.7/", line 59, in <module>
    class Pdb(bdb.Bdb, cmd.Cmd):
AttributeError: 'module' object has no attribute 'Cmd'

Ran 1 test in 0.000s

FAILED (errors=1)
msg248940 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-08-21 01:41
It did that because you did not specify a top level directory. Without that, the cwd is not on the path and that breaks many environments.

We should probably document it better. The workaround for your needs is to either just run 'unittest discover', or run 'unittest discover tests -t .', not 'unittest discover tests'.

The behaviour is however something I believe to be correct and essential to most user experiences.
msg392260 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-04-28 21:40
This is alluded to in the "caution" under

But the workaround is not explained.
Date User Action Args
2022-04-11 14:58:17adminsetgithub: 68435
2021-04-30 06:07:07rahul-kumisetnosy: + rahul-kumi
2021-04-28 21:40:45iritkatrielsettype: behavior
title: "unittest discover" modifies sys.path -> Docs: "unittest discover" modifies sys.path

keywords: + easy
nosy: + iritkatriel
versions: + Python 3.8, Python 3.9, Python 3.10, Python 3.11, - Python 2.7
messages: + msg392260
2015-08-21 01:41:34rbcollinssetassignee: docs@python
components: + Documentation
title: "unittest discover" does modify sys.path -> "unittest discover" modifies sys.path
nosy: + docs@python

messages: + msg248940
stage: needs patch
2015-05-20 22:54:43ned.deilysetnosy: + rbcollins, ezio.melotti, michael.foord
2015-05-20 18:11:44redixincreate