coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

sys.path is wrong when using coverage as a module and python version >= 3.7

Open d-fence opened this issue 5 years ago • 5 comments

Describe the bug

When the tested program is run from the coverage module (-m coverage run), the sys.path is wrong.

To Reproduce

Create the following small python script:

#!/usr/bin/env python3

import sys
print(sys.path)
  • Assuming that the script is in "foo/foo-bin.py" file.
  • use coverage version >= 5.0
  • python version >= 3.7

The first entry in sys.path is the absolute path of the current working directory from where you typed the command.

Alternatively repro.zip contains a tox test case (using python 3.5 to 3.8) .

Expected behavior

The first entry should be the absolute path of the directory where the script is

Additional context

Works as expected with python version < 3.7 or coverage < 5.0

d-fence avatar Jan 13 '20 14:01 d-fence

Thanks. Considering how much code I have to test that these things are the same, I'm chagrined to see that you are right. When running a file not in the current directory, "python" and "python -m coverage run" set sys.path[0] differently.

nedbat avatar Jan 14 '20 11:01 nedbat

After investigating a little further, I saw that coverage versions 5.0.0, 5.0.1, 5.0.2 also fails with python 3.5, 3.6, 3.7 and 3.8.

d-fence avatar Jan 15 '20 08:01 d-fence

@d-fence Can you help me understand how the sys.path change affects your program? It looks harder and harder to guarantee that the path will be identical both with and without coverage.

nedbat avatar Mar 10 '20 20:03 nedbat

I'm trying to decide if this is worth pursuing. Do you have any more information?

nedbat avatar Nov 02 '21 11:11 nedbat

Anecdotally, having the wrong sys.path entry leads to one program having a module not found exception. This would be very good to have fixed for me at least.

ishaangandhi avatar Dec 21 '21 18:12 ishaangandhi

If someone can provide more information, feel free to re-open this.

nedbat avatar Mar 18 '23 16:03 nedbat