Search code examples
pytestmayapackagingsetup.pytox

ModuleNotFoundError when running pytest and tox


I am trying to build template packages for 2 slightly different purposes, they can be found here :

The first one is a generic template project using tox/pytest/setup.py and works quite fine. The second one however is for the same purpose but when testing with the interpreter provided with Autodesk Maya.

My problem comes with the second one. When running the tests using the tox command, I get this error :

______________________________________________________________________________________________________________ ERROR collecting tests/test_maya_test_setup.py ______________________________________________________________________________________________________________ 
ImportError while importing test module 'D:\python\maya_template_project\tests\test_maya_test_setup.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Program Files\Autodesk\Maya2022\Python37\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests\test_maya_test_setup.py:4: in <module>
    from one.module_one import create_sphere
E   ModuleNotFoundError: No module named 'one'
========================================================================================================================= short test summary info ========================================================================================================================== 
ERROR tests/test_maya_test_setup.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
============================================================================================================================= 1 error in 0.13s ============================================================================================================================= 
ERROR: InvocationError for command 'C:\Program' 'Files\Autodesk\Maya2022\bin\mayapy' -m pytest (exited with code 2)
_________________________________________________________________________________________________________________________________ summary __________________________________________________________________________________________________________________________________
ERROR:   maya2022: commands failed

Here is the content of my tox.ini :

[tox]
envlist = maya2022
skipdist = True

[testenv:maya2022]
passenv = PYTHONPATH

deps = -rrequirements.txt

commands =
    C:\Program Files\Autodesk\Maya2022\bin\mayapy -m pytest
setenv =
    IN_MAYA = 1

As far as I understand, this seems to come from the location of the conftest.py, which forces tox to add the location of the conftest file to sys.path.

I have tried to remove the skipdist as mentioned in a few threads, but this had no effect whatsoever on the result (as I suspected anyway, because of the work around mentioned below).

This means that if I update this line to from src.one.module_one import create_sphere, this fixes the problem and works properly. However, this is very undesirable as I do not want the src package to be importable, but more importantly, I'd like the one package to be importable directly as this is the most logical thing to do, but also because this is the intended behavior as explained in this article.

So my question is : how do I fix this and get my project working without needing to add the src package to my imports?


Solution

  • tox does not care about a conftest.py, as this is configuration/collection file for pytest fixtures.

    Adding or removing skipdist from or to a tox.ini file does not make a difference, as this is no valid tox setting. I think you meant skipsdist, see https://tox.wiki/en/latest/config.html

    You probably want to delete the __init__.py in your src directory - this is meant to be a folder with only your project source in it, ie the one folder. src should not be importable.

    The specific problem with your setup is, that you install the project with tox, but then use a Python interpreter (C:\Program Files\Autodesk\Maya2022\bin\mayapy) which has no access to the installed package - that is why you get the ModuleNotFoundError.

    The reason why from src.one.module_one import create_sphere works - at least a bit - is that you invoke pytest as a module.

    python -m pytest makes pytest add the current dir to sys path, see https://jugmac00.github.io/til/what-is-the-difference-between-invoking-pytest-and-python-m-pytest/

    So, how to fix this?

    In general and as far as I know tox is not meant to work with custom interpreters.

    Maybe you can work around that by manipulating sys.path by adding the one directory to the path, which is then available to your custom interpreter.

    This means that your project needs to work without being installed, and the tests need to be discoverable from the "external" pytest.

    I cannot determine whether these workarounds are useful and work for you, as I am not familiar with Maya, but I am one of the tox developers and what you do is definitely not "maintstream" :-)

    P.S.: You could also use the install_command to install your package not with pip from your tox env, but with pip from Maya, see https://tox.wiki/en/latest/config.html#conf-install_command and https://knowledge.autodesk.com/support/maya/downloads/caas/CloudHelp/cloudhelp/2022/ENU/Maya-Scripting/files/GUID-72A245EC-CDB4-46AB-BEE0-4BBBF9791627-htm.html