pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Pytest suddenly cannot find module

Open adamhrosenberg opened this issue 8 years ago • 29 comments

Hello. I use pytest on a CI server to test our software. We have been using pytest for years but with the latest update we have an error importing a module during pytest. My logic behind isolating this issue to pytest is: on the CI server, nothing has changed (same python, same code, same OS image) the updated version of pytest is the only thing that has changed.

original versions of what pytest passed with platform linux2 -- Python 2.7.12, pytest-3.6.3, py-1.5.4, pluggy-0.6.0

current versions where I get failures: platform linux2 -- Python 2.7.12, pytest-3.7.1, py-1.5.4, pluggy-0.7.1

Let's say the module that cannot import is called X. When within the directory of X, (where the _UnitTest directory is) running pytest allows all of the tests to pass. Changing directory back one layer (or anywhere outside of the X directory), now those tests from module X fail because the import statement failed.

Also on macOS I had all of the tests passing with pytest 3.6.3. Upgraded pytest, and now the tests related to module X fail. Many other modules import and work however module X does not. I have debugged a lot of it, and all of the paths look good, but it still fails to import. I'm not exactly sure what sets module X apart from the other dozens of module we use successfully.

I hope this makes sense and I can elaborate more if needed. I am not working in a virtualenv but I can put full pip lists in the comments if more is needed other then the versions I gave above.

Thank you

Adam

adamhrosenberg avatar Aug 07 '18 19:08 adamhrosenberg

Same issue for me, if I downgrade to version <7.1 it's working.

Agi-dev avatar Aug 09 '18 08:08 Agi-dev

@Agi-dev thank you for the feedback! glad to know i'm not alone

adamhrosenberg avatar Aug 09 '18 15:08 adamhrosenberg

Hi guys, sorry for the delay.

Does 3.7.0 work for you? It might help pin point the problem. Also if any of you can produce a reproducible use case it would be great.

nicoddemus avatar Aug 09 '18 23:08 nicoddemus

No worries. 3.7.0 does NOT work either. However, version 3.6.4 DOES work. I'm honestly very unsure how to reproduce this outside of my example. I can think about this at work tomorrow though. I'm not sure why it only happens to one of my modules out dozens.

I do know that it stems from my init file in that module. That is where the import statement is, which is where the error occurs. (all of my other modules that pass have an init like this as well)

@Agi-dev do you have any method to reproduce?

adamhrosenberg avatar Aug 10 '18 00:08 adamhrosenberg

Sorry, I've tried to reproduce it with simple example with no success despite the fact I've found a way to make my tests work again. I've fixed the pb (for me) by deleting a __init__.py in the first sub directory of my tests directory. I'm not sure it's the way to go, I'm relatively new in Python programming.

Agi-dev avatar Aug 10 '18 08:08 Agi-dev

I have tried doing that to no luck :(

adamhrosenberg avatar Aug 10 '18 14:08 adamhrosenberg

We should release 3.7.2 soon (probably next week) with a few fixes in place regarding package collection (#3768, #3526), let's see if they end up solving this issue as well.

nicoddemus avatar Aug 10 '18 15:08 nicoddemus

Sounds great

adamhrosenberg avatar Aug 10 '18 17:08 adamhrosenberg

Here is a way to reproduce the problem:

Make a directory structure such as

/tmp/dir1/
/tmp/dir1/__init__.py
/tmp/dir1/dir1_1/
/tmp/dir1/dir1_1/__init__.py
/tmp/dir1/dir1_1/test_1_1.py
/tmp/dir1/dir1_1/test_1_1.py::test_1_1_case
/tmp/dir1/dir1_2/
/tmp/dir1/dir1_2/test_1_2.py
/tmp/dir1/dir1_2/test_1_2.py::test_1_2_case

then run pytest /tmp --collect-only

the output is

================================================ test session starts =================================================
platform linux -- Python 3.5.2, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
rootdir: /, inifile:
collected 2 items                                                                                                    
<Package '/tmp/dir1'>
  <Package '/tmp/dir1/dir1_1'>
    <Module 'test_1_1.py'>
      <Function 'test_1_1_case'>
  <Module 'test_1_2.py'>
    <Function 'test_1_2_case'>

It seems there is a problem when mixing directories of tests with packages of tests. Note that there is no mention of dir1_2 in the output. I saw this starting with 3.7.0. I do not see the issue with 3.6.4.

Eric

highvelcty avatar Aug 10 '18 17:08 highvelcty

Thank you! That's great

adamhrosenberg avatar Aug 10 '18 17:08 adamhrosenberg

@nicoddemus I still haven't found a way to reproduce it outside of my software package. We are open source so if you really want to test, you can get our code from here:

source Here is some documentation on compiling: documents

I can also test 3.7.2 when it comes out! Thanks for the feedback

adamhrosenberg avatar Aug 10 '18 17:08 adamhrosenberg

Seems like a duplicate of #3768 . Can you try my pull request and see if that fixes your issue?

jonozzz avatar Aug 10 '18 20:08 jonozzz

yea i can do that today

adamhrosenberg avatar Aug 13 '18 15:08 adamhrosenberg

@jonozzz assuming i've followed your PR trail correctly, I should be cloning your fork of pytest-dev with this branch? https://github.com/jonozzz/pytest/tree/fix-3768

Just installed...trying now.

Update: I still get the error

adamhrosenberg avatar Aug 13 '18 20:08 adamhrosenberg

I would install it in a virtualenv like this: pip install git+git://github.com/jonozzz/pytest.git@fix-3768

jonozzz avatar Aug 13 '18 20:08 jonozzz

yea that's pretty much what I did. no luck though, same error

adamhrosenberg avatar Aug 14 '18 15:08 adamhrosenberg

@adamhrosenberg might be a different issue then. Can you work on a minimal set of repro steps that you could share? Or at least paste the command you're running and the complete traceback and maybe I can figure out what's going on.

jonozzz avatar Aug 14 '18 21:08 jonozzz

Not sure why this was closed, the PR still doesn't fix my problem. Sorry I wasn't able to get back to you right away it was a busy week. I'll post the traceback below.

I run a normal pytest, and I'm told that the module radiation_pressure cannot be found. It gets to the init file which has from radiation_pressure import *. And it bugs out there. I posted the link to our software above.

adamhrosenberg avatar Aug 18 '18 17:08 adamhrosenberg

Hi @adamhrosenberg, if this is still is an issue we can re-open this, no problem! Did you try out with the recently released 3.7.2?

nicoddemus avatar Aug 18 '18 17:08 nicoddemus

I think this has more to do with PR #3797 because __init__.py is hardcoded in python_files. It will error out when there's a bad __init__.py somewhere, whereas in 3.6.x it'd just ignore it.

jonozzz avatar Aug 18 '18 18:08 jonozzz