relative import of modules in pytest in a Scafold structure

Viewed 155

using pyscafold in order to build a module I get an structure as follows

scr    
---module    
------__init__.py    
------file.py (containing func inside)    
tests   
---fileTest.py   

How is the right way to import file.py in the test file fileTest.py?

So far many of this variations dont work:

import pytest
from ../src/module import func

including this in init.py does not help:

import os, sys
sys.path.append(os.path.dirname(os.path.realpath(__file__)))

These do not help:

from .src/module/myfile import func
from ..src/module/myfile import func
1 Answers

There should not be forward slashes in your imports.

Also:

  • add a __init__.py file in tests directory
  • in fileTest.py, you should have:
import pytest
from src.module.file import func

Then, make sure the current working directory is the parent directory of src and run pytest .

Related