{"id":"e9234a1b6d17d5c3","repo":"pytest-dev/pytest","slug":"package-argument-cannot-contain-selection-parts","errorCode":null,"errorMessage":"package argument cannot contain :: selection parts: {arg}","messagePattern":"package argument cannot contain :: selection parts: (.+?)","errorType":"exception","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/main.py","lineNumber":1180,"sourceCode":"        if pyarg_strpath is not None:\n            module_name = strpath\n            strpath = pyarg_strpath\n    fspath = invocation_path / strpath\n    fspath = absolutepath(fspath)\n    if not safe_exists(fspath):\n        msg = (\n            \"module or package not found: {arg} (missing __init__.py?)\"\n            if as_pypath\n            else \"file or directory not found: {arg}\"\n        )\n        raise UsageError(msg.format(arg=arg))\n    if parts and fspath.is_dir():\n        msg = (\n            \"package argument cannot contain :: selection parts: {arg}\"\n            if as_pypath\n            else \"directory argument cannot contain :: selection parts: {arg}\"\n        )\n        raise UsageError(msg.format(arg=arg))\n    return CollectionArgument(\n        path=fspath,\n        parts=parts,\n        parametrization=parametrization,\n        module_name=module_name,\n        original_index=arg_index,\n    )\n\n\ndef is_collection_argument_subsumed_by(\n    arg: CollectionArgument, by: CollectionArgument\n) -> bool:\n    \"\"\"Check if `arg` is subsumed (contained) by `by`.\"\"\"\n    # First check path subsumption.\n    if by.path != arg.path:\n        # `by` subsumes `arg` if `by` is a parent directory of `arg` and has no\n        # parts (collects everything in that directory).\n        if not by.parts:","sourceCodeStart":1162,"sourceCodeEnd":1198,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/main.py#L1162-L1198","documentation":"Raised by `resolve_collection_argument` (with `as_pypath=True`) when a `--pyargs` package argument also contains `::` selection parts. pytest does not support selecting a class/function inside a package via dotted-path arguments; selection parts require a concrete module file, not a package directory.","triggerScenarios":"Running `pytest --pyargs mypkg.tests::TestClass` or `--pyargs mypkg::test_foo`. The path resolves to a directory (package) but `parts` is non-empty.","commonSituations":"Mixing the `--pyargs` dotted syntax with the `::` node-selection syntax. Trying to select within a package as if it were a module.","solutions":["Point `--pyargs` at the module containing the test: `pytest --pyargs mypkg.tests.test_foo::TestClass`.","Drop `--pyargs` and use a filesystem path with `::` selection.","Select by keyword (`-k`) or marker (`-m`) instead of node id."],"exampleFix":"// before\n$ pytest --pyargs mypkg.tests::TestClass\n// after\n$ pytest --pyargs mypkg.tests.test_foo::TestClass","handlingStrategy":"validation","validationCode":"def validate_pyargs(arg: str):\n    if \"::\" in arg:\n        mod_part = arg.split(\"::\", 1)[0]\n        import importlib.util\n        spec = importlib.util.find_spec(mod_part)\n        if spec is not None and spec.submodule_search_locations is not None:\n            raise ValueError(f\"package argument cannot contain '::': {arg}\")","typeGuard":"def pyargs_arg_is_module(arg: str) -> bool:\n    import importlib.util\n    mod = arg.split(\"::\", 1)[0]\n    spec = importlib.util.find_spec(mod)\n    return spec is not None and spec.submodule_search_locations is None","tryCatchPattern":null,"preventionTips":["With --pyargs, target a module file before '::', not a package.","Use filesystem paths for directory-level selection.","Filter with -k/-m instead of node ids on packages."],"tags":["pytest","collection","pyargs","cli","usageerror"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}