-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
conftest.py in root directory should not add root directory to sys.path #2269
Comments
this is expected behaviour with conftests, and indeed tricky to gauge in the root layout you have your python packages in the root folder directly, thus easily causing trouble with sys.path behaviour in the src layout you have your python packages inside a src folder separating them from common importability |
FWIW I would not be against trying to fix this (and I wrote that SO answer...), it is rather bad that pytest modifies sys.path so much just because of loading conftest files. This would probably cause major breakage though so may be trick to introduce and need a major version etc. |
a pytest.ini option wrt early conftest processing as well as expected sys.path changes can be done as a non-intrusive opt-in if we give people a way to specify the expectation, it safes us and them a lot of pain and the need for a major release |
From a glance at the code it seems pytest adds the conftest's directory to |
It would break the world in various cases, like deferred imports |
Hmm thanks. But adding an option like you propose help in this case? |
I mean, adding an option that removes from sys.path |
@nicoddemus on bit we need to control is leading of conftests only in pythonpath (so disallow adding them to sys.path) |
I'm actually interested in the opposite - I would like to include the top-level directory (current directory when running pytest) in the Python path. ("src", I know - not gonna happen) This seems like something a couple of simple command line options could fix:
I get there is a "right" way that people want to force everyone to use, but there are a lot of projects out there that don't use |
Hmmm adding an option indeed might be a good idea here in this case (although I don't think An option to never modify path is possible given we have to do that to import It might be better even as a What do others think? |
I've ran into this today:
So this is very confusing behavior here:
the root will be added to the A manifestation of this is that coverage report breaks, as imports no longer use the site package, but the local files. |
Hi @gaborbernat! That's strange, I can't reproduce your problem:
As can be seen, only |
#5352 was a step in this direction, but was closed without explanation (?). |
Because you did not want to take it as a hidden option only mostly - at least that was my impression. |
@nicoddemus I think |
Indeed, good catch, thanks!
(This will be released in |
I started putting
conftest.py
in the root directory of my packages, instead of thetests/
directory, because I wanted to modify my doctest namespace.However, it turns out that putting
conftest.py
in the root directory causespy.test
to use the local rather than the installed version of my package to do the tests, which breaks some of my tests which depend on compiled modules.Is this the intended behaviour? Some people seem to use this as a feature (e.g. see this stackoverflow answer). However, in my case this was a bug that was relatively difficult to track down.
I fixed this by putting the following lines in the root
conftest.py
file:Anaconda python running on CentOS 6
The text was updated successfully, but these errors were encountered: