Plugin imports might use cwd sys.path entry · Issue #2104 · pytest-dev/pytest (original) (raw)
Here's an interesting one where I could use some advice.
In pypa/setuptools#852, I discovered that a plugin (pytest-flake8 by way of pyflakes and flake8) will import setuptools
during plugin setup. For the setuptools project in particular, this poses a problem because that causes setuptools to be imported from the current directory and with a relative __file__
attribute. Later, when the cwd has changed, that __file__
attribute no longer resolves to the actual file path of the setuptools module, and a test fails.
My first question is - how is it that deferring the import of setuptools until later bypasses the issue? Is pytest doing something to the import mechanism to ensure that __file__
attributes are absolute, but only after plugins have been setup? Or is pytest manipulating sys.path such that the relative path is replaced by an absolute path?
Then, the question becomes, what's the proper solution here? Should all plugins defer their third-party imports as late as possible? Should pytest protect the __file__
attribute during plugin setup the same way it appears to be doing for later imports? What other options are there?