0

I use pybind11 to add a module _cxx to existing python library liba. The liba._cxx.func doesn't exist until I compile the c++ extension and install the whole library by setup.py.

When I run tests in liba/tests/test__cxx.py, it complains that it can't import liba._cxx

How to solve this?

R zu
  • 2,034
  • 12
  • 30
  • seems to solve it by removing `__init__` in the `liba/tests` folder, and then move all tests out of the `liba` folder. But I still have to test the tests a bit more. https://stackoverflow.com/questions/41748464/pytest-cannot-import-module-while-python-can – R zu Jan 24 '18 at 19:32

2 Answers2

1

From pytest cannot import module while python can

Remove __init__.py in the test folder. That way, the test will use the liba installed in the system instead of liba within the source code.

R zu
  • 2,034
  • 12
  • 30
0

I had to specify the path in conftest.py

import os
import sys
directory = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, directory)
sys.path.insert(0, directory + os.sep + "../pybewego")
sys.path.insert(0, directory + os.sep + "..")

pytest was also not able to handle the specific case where the c++ module has the same name as the python module. I had to follow the convention advised there:

https://github.com/pybind/pybind11/issues/1004#issuecomment-322941844

Now I can define a module that comprises both pure python and c++ extensins using pybind11 and test with pytest.

Jim M.
  • 96
  • 3