mpowcode
mpowcode

Reputation: 83

Pytest - ModuleNotFoundError: No module named 'x'

****solved: added __init__.py to Test/ and renamed testcode.py to test_code.py. To run tests cd -> Zee and type pytest****


Structure:

|--Zee/
|   |--Test/
|   |   |--__init__.py
|   |   |--test_code.py
|   |--Codetotest/
|   |   |--code.py

in code.py

class Foo():
    some code...

in testcode.py

from Codetotest.code import Foo

def test_foo():
   assert ...

When I move to the Zee directory in my command line and run pytest Test/testcode.py I get ModuleNotFoundError: No module named Zee. How can I fix this?

I tried making Test a module by adding Test/__init__.py as suggested here. Ran from multiple directories, no dice.

Pytest version 5.3.4, imported from python 3.6

What I don't understand is, when I add __init__.py to Zee/, it gives me the same error

Upvotes: 5

Views: 3050

Answers (1)

Jérôme
Jérôme

Reputation: 14674

You need a __init__.py in the module directory.

Here's a typical project structure:

|--zee-project-directory/
|   |--tests/
|   |   |--test_zee.py
|   |--zee/
|   |   |--__init__.py
|   |   |--code.py

code.py

class Foo():
    some code...

test_zee.py

from zee.code import Foo

def test_foo():
   assert ...

Upvotes: 1

Related Questions