Question: Why does this import work by default, but not with --import-mode=importlib #9835
Replies: 4 comments
-
Hi @LuminairPrime,
Just a note: I don't think that will be the case, because unfortunately it has its own issues as well.
pytest needs to import
The |
Beta Was this translation helpful? Give feedback.
-
Hmmm. In this case, the path that import-mode=importlib needs to find is equal to 1) the directory where pytest is run, and 2) the test file directory, and 3) the tested file directory. Shouldn't every import mode succeed at this? Where do I need to place y.py for import-mode=importlib to find it? |
Beta Was this translation helpful? Give feedback.
-
Indeed import modes and test discovery is a somewhat complicated topic (because Python's import is complicated). We have a dedicated section in the docs dedicated to this topic: https://docs.pytest.org/en/7.1.x/explanation/pythonpath.html
Usually the recommendation is using a virtual environment and installing your application there. For quick one-off scripts or very simple projects, you can run |
Beta Was this translation helpful? Give feedback.
-
For my personal use the default behavior of pytest works, and I can apply sys.path.append for extra distant directories. What I'm trying to get to the bottom of here is whether import-mode=importlib is broken. To ME it seems like it should work in this case. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I was testing --import-mode=importlib because I read that it will eventually be the default mode.
I have test_x.py with:
And y.py with:
The output with a default pytest is a pass:
And the output with a pytest --import-mode=importlib is a failed import:
What's up? Is that a bug, or do I need to do something else with --import-mode=importlib ?
Beta Was this translation helpful? Give feedback.
All reactions