{"record":{"id":"ac74850e2c15749e","repo":"microsoft/autogen","slug":"module-name-must-be-a-valid-python-identifier","errorCode":null,"errorMessage":"Module name must be a valid Python identifier","messagePattern":"Module name must be a valid Python identifier","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py","lineNumber":227,"sourceCode":"        # Handle bind_dir\n        self._bind_dir: Optional[Path] = None\n        if bind_dir is not None:\n            self._bind_dir = Path(bind_dir) if isinstance(bind_dir, str) else bind_dir\n        else:\n            self._bind_dir = self._work_dir  # Default to work_dir if not provided\n\n        # Track temporary directory\n        self._temp_dir: Optional[tempfile.TemporaryDirectory[str]] = None\n        self._temp_dir_path: Optional[Path] = None\n\n        self._started = False\n\n        self._auto_remove = auto_remove\n        self._stop_container = stop_container\n        self._image = image\n\n        if not functions_module.isidentifier():\n            raise ValueError(\"Module name must be a valid Python identifier\")\n\n        self._functions_module = functions_module\n        self._functions = functions\n        self._extra_volumes = extra_volumes if extra_volumes is not None else {}\n        self._extra_hosts = extra_hosts if extra_hosts is not None else {}\n        self._init_command = init_command\n        self._delete_tmp_files = delete_tmp_files\n        self._device_requests = device_requests\n\n        # Setup could take some time so we intentionally wait for the first code block to do it.\n        if len(functions) > 0:\n            self._setup_functions_complete = False\n        else:\n            self._setup_functions_complete = True\n\n        self._container: Container | None = None\n        self._running = False\n","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py#L209-L245","documentation":"Constructor validation in DockerCommandLineCodeExecutor: the `functions_module` argument (the module name the executor writes your `functions` to inside the container) must be a valid Python identifier, because generated code imports it via `import <functions_module>`. Names with dashes, dots, leading digits, or spaces are rejected.","triggerScenarios":"Passing functions_module=\"my-functions\", \"funcs.v2\", \"2funcs\", or an empty string. The generated functions file is written as <functions_module>.py and imported, so non-identifier names would break that import; the check fails fast at construction.","commonSituations":"Deriving the module name from a file stem or app name containing dashes/dots; i18n tooling or templating inserting spaces; passing a filename instead of a module name.","solutions":["Use a plain identifier: functions_module=\"functions\" (default) or \"my_functions\"","If the name comes from external config, sanitize it: re.sub(r'\\W|^(?=\\d)', '_', name).strip('_')","Prefer leaving the default unless multiple executors share a container"],"exampleFix":"# before\nexecutor = DockerCommandLineCodeExecutor(functions_module=\"agent-funcs\")\n\n# after\nexecutor = DockerCommandLineCodeExecutor(functions_module=\"agent_funcs\")","handlingStrategy":"validation","validationCode":"import re\n\ndef to_identifier(name: str) -> str:\n    ident = re.sub(r\"\\W\", \"_\", name)\n    if not ident or ident[0].isdigit():\n        ident = f\"f_{ident}\"\n    assert ident.isidentifier()\n    return ident","typeGuard":"def is_valid_module_name(name: str) -> bool:\n    return isinstance(name, str) and name.isidentifier()","tryCatchPattern":"null","preventionTips":["Sanitize external strings into identifiers before passing them as functions_module","Prefer the default 'functions' module name unless you need multiple executors in one container"],"tags":["docker","validation","naming","constructor"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}