Error in relative import in python

1

I'm trying to test a simple python code using pytest. My hierarchy is as follows:

PythonExamples
    files
        mymath.py
    tests
        test_mymath.py

In test_mymath.py, I import mymath as

from ..files import mymath as mm
def test_mysum():
    assert mm.mysum(2,3) == 5

In the PythonExamples folder, run the py.test tests command, however I get the following message:

  

Parent module '' not loaded, can not perform relative import

What can it be?

    
asked by anonymous 16.03.2017 / 20:24

1 answer

1

Try: from ..files.mymath import mysum

In the case of mysum would be def that is created inside mymath.py

for example in a structure:

mypackage/
    __init__.py
    mymodule.py
    myothermodule.py

o mymodule.py

#!/usr/bin/env python3

# Exported function
def as_int(a):
    return int(a)

# Test function for module  
def _test():
    assert as_int('1') == 1

if __name__ == '__main__':
    _test()

o myothermodule.py

#!/usr/bin/env python3

from .mymodule import as_int

# Exported function
def add(a, b):
    return as_int(a) + as_int(b)

# Test function for module  
def _test():
    assert add('1', '1') == 2

if __name__ == '__main__':
    _test()
    
16.03.2017 / 20:35