Skip to content
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

why adding conftest.py "adds" to the sys.path ? #4699

Closed
david-fliguer-ws opened this issue Jan 31, 2019 · 8 comments
Closed

why adding conftest.py "adds" to the sys.path ? #4699

david-fliguer-ws opened this issue Jan 31, 2019 · 8 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@david-fliguer-ws
Copy link

Hi,

I was having some issues importing a module that is on a folder in an "upper" level than the .py of my test (In general I know that a lot of importing issues in python are related to the sys.path) so I printed the sys.path and effectively I don't see the folder that contains the module that I wanted to import (Despite inside pycharm it gives me autocomplete), however once I create an empty conftest.py in the upper level of my project then I don't have an import error and I can see that having the conftest.py "added" values to the sys.path.

Can someone explain how conftest.py affects the sys.path values ?

Thanks,

David

@nicoddemus
Copy link
Member

Hi @david-fliguer-ws,

In short, pytest needs to import the conftest.py, and in order to do so it needs to append the directory that contains the conftest.py to sys.path.

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Jan 31, 2019
@david-fliguer-ws
Copy link
Author

david-fliguer-ws commented Jan 31, 2019

Thanks for the fast answer !!!

That means than whenever I have a conftest.py file in the uppest level of my project then I can freely make imports in any .py file that I have there and in lower levels ? (No need to "manipulate" the sys.path or stuff like:

from ....\xxx import)

Relying on the conftest.py (Usually it won't be empty and I will use it for fixture reasons) is it a good "approach" to solve import issues ?

@nicoddemus
Copy link
Member

Exactly, but is not considered a good approach to that problem because it relies on a (necessary) side-effect of how pytest imports conftest.py files.

It is suggested instead to use virtual environments and "editable" installs.

@wimglenn
Copy link
Member

Avoid conftest.py in the project root, if you can. I usually put a tests/conftest.py.

Related: src-based project structures https://hynek.me/articles/testing-packaging/

@blueyed
Copy link
Contributor

blueyed commented Feb 1, 2019

@nicoddemus

In short, pytest needs to import the conftest.py, and in order to do so it needs to append the directory that contains the conftest.py to sys.path.

But that could be undone also after importing, couldn't it?

@RonnyPfannschmidt
Copy link
Member

@blueyed currently, that would break quite some interesting usages (deferred imports in hooks for example)

@blueyed
Copy link
Contributor

blueyed commented Feb 1, 2019

@RonnyPfannschmidt
I see - just wasn't sure if I misses something else.

It is clear that it would break backward-compatibility (but that's what this issue is about basically).

As for deferred imports in hooks, those could also get wrapped by changing sys.path then temporarily I guess.

@nicoddemus
Copy link
Member

@blueyed my feeling is that could generate much more problems than it is worth.

@Zac-HD Zac-HD closed this as completed Feb 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

6 participants