Interview Prep/Python for DevOps/Environments, imports & packaging errors

`AttributeError: partially initialized module 'yaml' has no attribute 'safe_load' (most likely due to a circular import)`

BeginnerTroubleshootingEnvironments, imports & packaging errors
Answer

Likely causes → Your own file is named yaml.py (or there is a yaml/ folder) in the script directory, shadowing PyYAML. Less often, a genuine circular import between your modules.

Diagnose →

ls *.py | grep -E '^(yaml|json|requests|boto3|logging)\.py$'
python -c "import yaml; print(yaml.__file__)"

Fix → Rename the file (yaml_utils.py) and delete __pycache__/. For true circular imports, move shared code to a third module or import inside the function.

Prevent → Never name files after stdlib or third-party packages; ruff rule A005 flags stdlib-shadowing module names.

All Python for DevOps questions Roadmap: Python for automation

More on Environments, imports & packaging errors