Lex Podgorny
Lex Podgorny

Reputation: 2930

pytest import fails with test directory – "no module found named ..."

I am a newbie with pytests, read through docs, but that did not help. So asking here.

My project structure:

├─ lexor
  ├─ __init__.py
  ├─ lexor
  │  ├─ __init__.py
  │  └─ MyClass.py
  ├─ lib
  └─ tests
     ├─ __init__.py
     └─ test_MyTest.py

Code in test_MyTest.py

from lexor.MyClass import MyClass

class test_MyTest:
   def test1(self):
      assert True

Running pytest from outer lexor directory yields an error: ModuleNotFoundError: No module named 'lexor.MyClass'

I know there is something simple I am missing.. Please help.

Upvotes: 0

Views: 887

Answers (1)

Peter K
Peter K

Reputation: 2464

The problem is the __init__.py in the root directory - it confuses python which tries to look for MyClass in the root directory instead of the subdirectory.

Just remove this file:

├─ lexor
  ├─ __init__.py   <-- delete me
  ├─ lexor
  │  ├─ __init__.py
  │  └─ MyClass.py
  ├─ lib
  └─ tests
     ├─ __init__.py
     └─ test_MyTest.py

Upvotes: 2

Related Questions