-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
ModuleNotFoundError when using the tests outside application code layout #2421
Comments
I have the same issue. My project: https://github.com/Projectplace/pytest-setup Running tests using tox and python 2.7 works. Running tests using tox and python 3.6 fails. However, running the tests individually The plugin works as expected when in use on both 2.7 and 3.6. |
from http://python-guide-pt-br.readthedocs.io/en/latest/writing/structure/ You can create a from __future__ import absolute_import
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import mypkg Then in your test file, add |
im closing this issue as it relates to a miss-understanding of the python import system, the errors are indeed intended by python itself |
@yfpeng @RonnyPfannschmidt i followed this exact same guide, but when i execute py.test-3 in test_pytest, i get following error:
I am reluctent to add an __init__.py in my tests directory since this is against what the documentation is offering. Do you know how to execute tests properly in that case? |
As a general recommendation: Always run tests against your installed package. If your package is not installable yet: make it installable. When you develop, install your package with I am speaking from a place of major pain in the past, when I was not doing these things myself and when packaging was in a much worse state than it is today. That all went away now and is replaced by major packaging pains 😀 But joking aside: in most cases it is really much easier to create a simple setup.py to make your project installable and run tests against that, than going through all this hackery to make it work without. |
@obestwalter thanks for the hint. In my case, i very much more appreciate having a code base i can consistently and continuously test (without any additional packaging pain). |
Yes, This is how I used to do it, because I thought it was simpler that way. Turns out it isn't in the long run. Learning about Python packaging is really worth the extra effort in my experience, especially because it works pretty darn good nowadays thanks to the folks at https://github.com/pypa. |
@obestwalter has the correct solution. However, if you need something temporary while you are developing a package, you can simple add the following file to your tests directory, and then pytest will run it before the tests (so no changes are needed per-file when you make a final package, you just delete this one file):
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) (This is one of the top google results for this error, so adding this even though the issue is closed) PS: This is a hack, please use the editable install solution unless you have good reason to use this one. And do not forget to remove this before publishing to PyPI. |
@henryiii i strongly suggest to avoid such hacks and use editable installs instead |
@RonnyPfannschmidt I fully agree; this is just useful if you aren't ready to do an editable install or in special cases; and if so, this is a better solution than one non-editable example shown above because it converts much more easily to the correct solution. In my case, I'm temporarily testing locally against CERN ROOT, and that has to be done against the system python on a Mac, and ROOT does not support pretty much anything nice in Python like virtual environments very well. This is a good way to temporarily test (once it's in Travis, I can test against system Python there). My packages (from cookiecutter) is already installable from moment 0, I just don't want to mess with my user directory by installing the system python one. So I'm sure there is at least one special case where this is useful. :) |
@henryiii that one is a understandable exception, thanks for sharing the context |
@RonnyPfannschmidt I think there is a documentation issue here. I also followed the guide Good Integration Practices, and I would've felt pretty lost if I hadn't stumbled upon this issue. I think some sort of very short guide to how to use pip editable mode would be very welcome in said guide, if that is the recommended way to go no matter how you structure your tests, as long as it's the "Tests outside application code" type 🙂 |
Question 1: Question 2: |
If you read a bit further down it is explained in the tox section: https://docs.pytest.org/en/latest/goodpractices.html#tox |
Ah ok. It's good that it's there, but it should be front and center, I don't see it belonging in the tox section. |
@gandalfsaxe you make a good point. We would appreciate a quick PR to the docs if you got the time, or create a new issue so it doesn't get lost into this one. 👍 |
Should fix complaints in pytest-dev#2421.
I have my project setup the exact same way, had the same issue but fixed it. My problem was having pytest installed system wide (outside of my virtual environment) and a package installed only in my virtualenv. pytest was looking in system site-packages. By installing pytest in virtualenv it solved the issue. Adding this comment in case you end up here from Google (like me). Longer explanation: https://medium.com/@dirk.avery/pytest-modulenotfounderror-no-module-named-requests-a770e6926ac5 |
If you are using pipenv try |
Include a detailed description of the bug or suggestion
I was following the test layout in Good Integration Practices and I couldn't get my tests that were in a similar configuration to run or the simple example I created below.
pip list
of the virtual environment you are usingpytest and operating system versions
Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-77-generic x86_64)
platform linux -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
Minimal example if possible
and the files
Running pytest from the
test_pytest
directory will result in an error similar to this one.(py36env) ubuntu@ubuntu-xenial:/vagrant/test_pytest$ pytest
I can add to the
sys
path manually by adding a__init__.py
totests/
but that will complicate testing with Travis CI
The text was updated successfully, but these errors were encountered: