1

I have two python scripts, x.py and test_x.py, I want to import x from test_x.

  • x.py is located at ./foo/bar/baz/x.py
  • test_x.py is located at ./tests/test_x.py

Contents of x.py

def f(x):
    return x + 1

Contents of test_x.py

import foo.bar.baz.x as x

def test_f():
    assert x.f(1) == 2, "Error"

I then try running the above as:

pytest -m tests/test_x.py

Which gives me the error:

tests/test_x.py:1: in <module>
    import foo.bar.baz.x as x
E   ModuleNotFoundError: No module named 'foo'

So - how should I go about running the above test?

baxx
  • 3,956
  • 6
  • 37
  • 75
  • 1
    Do you have an `__init__.py` in each directory (`foo, bar, and baz`)? You need that for Python to recognize that directory as an importable "module". – puppydog May 06 '20 at 15:35
  • 1
    @baxx I recommend taking a look at [relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Jay Mody May 06 '20 at 15:36
  • @puppydog i'm trying to import scripts though, not directories – baxx May 06 '20 at 15:45
  • yes, scripts that you've nested in directories. check out @Jay Mody's post – puppydog May 06 '20 at 18:06
  • That’s not how `-m` is supposed to be used: it expects a **module** name. – Davis Herring May 06 '20 at 19:36

0 Answers0