How to run tests?

Hello, I want to run test files in tests/, for example, test_flash_attn.py, with command as:

cd vllm/tests/kernels/attention/
python -m pytest test_flash_attn.py

But it will report an error as:

ImportError while loading conftest '/data/framework_vllm/sxh/vllm/tests/conftest.py'.
../conftest.py:20: in <module>
    from tests.models.utils import (TokensTextLogprobs,
../models/utils.py:11: in <module>
    from vllm.config import ModelConfig, TaskOption
../../vllm/config.py:35: in <module>
    from vllm.model_executor.layers.quantization import QuantizationMethods
../../vllm/model_executor/__init__.py:4: in <module>
    from vllm.model_executor.parameter import (BasevLLMParameter,
../../vllm/model_executor/parameter.py:10: in <module>
    from vllm.distributed import get_tensor_model_parallel_rank
../../vllm/distributed/__init__.py:4: in <module>
    from .communication_op import *
../../vllm/distributed/communication_op.py:9: in <module>
    from .parallel_state import get_tp_group
../../vllm/distributed/parallel_state.py:150: in <module>
    from vllm.platforms import current_platform
../../vllm/platforms/__init__.py:285: in __getattr__
    _current_platform = resolve_obj_by_qualname(
../../vllm/utils/__init__.py:2510: in resolve_obj_by_qualname
    module = importlib.import_module(module_name)
../../vllm/platforms/cuda.py:18: in <module>
    import vllm._C  # noqa
E   ModuleNotFoundError: No module named 'vllm._C'

I am confused about this error, because I install vllm successfully. run the command below get no error.

python -c "import vllm; import vllm._C"

So, How can I run test_flash_attn.py file ?

This error occurs because when you run pytest from within the vllm source directory (e.g., cd vllm/tests/kernels/attention/), Python may import the local source files instead of the installed vllm package, causing it to miss the compiled extension module vllm._C. The recommended solution is to run your tests from a directory outside the vllm source tree, so Python imports the installed package (which includes the compiled extensions) rather than the local source files. For example, from your home directory or any directory that does not contain a vllm/ folder, run:

pytest /data/framework_vllm/sxh/vllm/tests/kernels/attention/test_flash_attn.py

This issue and solution are discussed in detail in this GitHub issue and the troubleshooting guide. Would you like more detail on why this happens or how Python’s import system works in this context?

Sources:

No, it does not work. No matter from where I run my tests.

If you still get ModuleNotFoundError: No module named 'vllm._C' even when running tests from outside the source directory, it likely means the compiled extension was not built or is not being found by Python. This can happen if vLLM was not installed with compilation, or if the Python environment used for testing is not the same as the one where vLLM was installed.

Make sure you have installed vLLM with compilation using the correct Python environment. The recommended way is:

cd /data/framework_vllm/sxh/vllm
pip install -e .

If you are using a conda or virtualenv, ensure it is activated before running the above. After installation, run:

python -c "import vllm; import vllm._C"

If this works, then run your test from outside the source directory as:

python -m pytest /data/framework_vllm/sxh/vllm/tests/kernels/attention/test_flash_attn.py

If you still see the error, check that there is no other vllm folder in your current directory or PYTHONPATH that could shadow the installed package. Also, ensure your Python version matches the one used for installation. For more, see this issue and the troubleshooting guide.

Would you like step-by-step debugging instructions?

Sources: