{"id":563,"library":"pyflakes","title":"Pyflakes: Python Static Code Analysis","description":"Pyflakes is a fast, lightweight static analysis tool that meticulously checks Python code for common programming errors without executing it. It primarily identifies issues such as unused imports, undefined names, and redefined variables. Focusing solely on error detection, rather than code style, it aims for speed and minimizes false positives. Currently at version 3.4.0, Pyflakes supports Python 3.9 and newer, and is actively maintained by the PyCQA organization.","status":"active","version":"3.4.0","language":"python","source_language":"en","source_url":"https://github.com/PyCQA/pyflakes","tags":["linter","static analysis","code quality","developer tool","error checking"],"install":[{"cmd":"pip install pyflakes","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[],"quickstart":{"code":"import subprocess\nimport os\n\n# Create a dummy Python file with an error\ncode_to_check = \"\"\"\nimport os\n\ndef my_func():\n    x = 10\n    # y is used but not defined, 'os' is imported but unused\n    print(y)\n\"\"\"\n\nfile_path = \"temp_code.py\"\nwith open(file_path, \"w\") as f:\n    f.write(code_to_check)\n\n# Run pyflakes on the file\ntry:\n    result = subprocess.run(\n        [\"pyflakes\", file_path],\n        capture_output=True,\n        text=True,\n        check=False  # pyflakes exits with non-zero on errors\n    )\n    print(\"Pyflakes Output:\\n\" + result.stdout)\n    if result.stderr:\n        print(\"Pyflakes Error (stderr):\\n\" + result.stderr)\nfinally:\n    # Clean up the dummy file\n    if os.path.exists(file_path):\n        os.remove(file_path)\n\n# Expected output might look like:\n# temp_code.py:2: 'os' imported but unused\n# temp_code.py:6: undefined name 'y'","lang":"python","description":"Demonstrates how to run Pyflakes from the command line on a Python file to detect errors. Pyflakes outputs issues to stdout."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or a newer active version.","message":"Pyflakes 3.x and newer officially supports Python 3.9+. Older Python versions (e.g., 2.x or Python 3.8 and below) are no longer supported, requiring users to upgrade their Python environment.","severity":"breaking","affected_versions":"3.0.0+"},{"fix":"For combined style and error checking, use `flake8` (`pip install flake8`) which integrates Pyflakes along with other tools like `pycodestyle` (formerly `pep8`) and `mccabe`.","message":"Pyflakes is a static *error* checker, not a *style* checker. It will not report on PEP 8 violations, line length, or other stylistic concerns. Users expecting comprehensive linting (style + errors) should use tools like Flake8, which bundles Pyflakes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If configuration is required, consider using `flake8` which provides extensive configuration options, or integrate Pyflakes into a larger system that manages its invocation and output filtering.","message":"Pyflakes does not support configuration files for customizing rules, ignoring specific warnings, or defining project-specific settings. Its behavior is largely fixed by its code.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Integrators should rely on Pyflakes's standard command-line output parsing where possible, or pin Pyflakes to a specific minor version to prevent unexpected breakage from internal API changes.","message":"Pyflakes's internal API, particularly the structure of its error messages (e.g., `pyflakes.messages`), is not considered stable. Updates can change or remove message types, which may break third-party tools that integrate deeply with Pyflakes's programmatic output or internal modules.","severity":"breaking","affected_versions":"2.5.0+"},{"fix":"Review the reported issues in your code. Unused imports/variables may indicate dead code or refactoring opportunities. Undefined names are critical runtime errors. Address these directly or, if intentional, consider using appropriate suppression comments (e.g., '# noqa') as per Pyflakes's guidelines.","message":"Pyflakes is designed to identify common Python programming errors, including but not limited to, unused imports, variables assigned but never used, and references to undefined names. These findings indicate actual or potential issues in the codebase rather than stylistic concerns.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T06:10:16.765Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Remove the unused import statement to keep the codebase clean and avoid unnecessary dependencies.","cause":"Pyflakes detects an `import` statement for a module that is not subsequently referenced or used anywhere in the code.","error":"module imported but unused"},{"fix":"Ensure the 'name' is correctly defined before its first use, or that the necessary module/object containing 'name' is properly imported.","cause":"This error occurs when a variable, function, or class name is used in the code without being defined or imported in the current scope.","error":"undefined name 'name'"},{"fix":"Initialize the variable with a default value before its first use, or ensure it is assigned a value on all possible code paths before being referenced. If intending to modify a global variable, use the `global` keyword.","cause":"A local variable is accessed or used within a function before any value has been assigned to it in that function's scope.","error":"local variable 'name' referenced before assignment"},{"fix":"Refactor the code to avoid redundant definitions. If the redefinition is intentional, ensure the first definition is used or remove the unnecessary initial definition.","cause":"A name (e.g., a variable or function) is defined, but then defined again later in the same scope without its initial definition being used.","error":"redefinition of unused name from line N"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}