{"id":6053,"library":"pygount","title":"pygount","description":"Pygount is a command line tool designed to scan folders for source code files and accurately count the number of source lines of code (SLOC). It leverages the robust `pygments` package to parse source code, allowing it to analyze hundreds of programming languages. Pygount, currently at version 3.2.0, is an actively maintained library with regular updates.","status":"active","version":"3.2.0","language":"en","source_language":"en","source_url":"https://github.com/roskakori/pygount.git","tags":["code analysis","SLOC","static analysis","code metrics","pygments"],"install":[{"cmd":"pip install pygount","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Core dependency for source code parsing and language detection.","package":"pygments","optional":false},{"reason":"Optional dependency for automatic character encoding detection when using `--encoding=chardet`.","package":"chardet","optional":true}],"imports":[{"symbol":"SourceAnalysis","correct":"from pygount import SourceAnalysis"},{"note":"While `pygount.source_analysis` is still available, the object-oriented API (`SourceAnalysis` and `ProjectSummary`) is the recommended approach for programmatic use, though still considered 'work in progress' and subject to change.","wrong":"import pygount; pygount.source_analysis(...)","symbol":"ProjectSummary","correct":"from pygount import ProjectSummary"}],"quickstart":{"code":"import os\nfrom pathlib import Path\nfrom pygount import SourceAnalysis, ProjectSummary\n\n# Create dummy files for demonstration\ndummy_dir = Path('./my_project')\ndummy_dir.mkdir(exist_ok=True)\n(dummy_dir / 'main.py').write_text(\n    \"\"\"# This is a Python file\nimport sys\n\ndef hello(): # Code line\n    print('Hello, world!') # Code line\n    # Another comment\n\"\"\"\n)\n(dummy_dir / 'README.md').write_text(\n    \"\"\"# My Project\n\nThis is a test project.\n\"\"\"\n)\n(dummy_dir / '.hidden_file.txt').write_text(\"should not be counted\")\n\nproject_summary = ProjectSummary()\n\n# Analyze files in the dummy project directory\nfor source_path in dummy_dir.rglob('*'):\n    if source_path.is_file():\n        try:\n            source_analysis = SourceAnalysis.from_file(source_path, 'my_project')\n            project_summary.add(source_analysis)\n        except UnicodeDecodeError as e:\n            print(f\"Warning: Could not decode {source_path}: {e}\")\n\nprint(\"\\n--- Analysis Results ---\")\nfor language_summary in project_summary.language_to_language_summary_map.values():\n    print(language_summary)\n\n# Clean up dummy files\nimport shutil\nshutil.rmtree(dummy_dir)","lang":"python","description":"This quickstart demonstrates how to use `pygount`'s API to programmatically analyze source code files within a directory and summarize the results. It creates dummy files, analyzes them using `SourceAnalysis` and aggregates the results with `ProjectSummary`, then prints the language-specific counts."},"warnings":[{"fix":"Upgrade your Python interpreter to version 3.10 or higher.","message":"Pygount versions 3.x have removed support for older Python versions. Specifically, Python 3.7 and 3.8 are no longer supported. Ensure your environment uses Python 3.10 or newer.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Refer to the official documentation and 'Changes' log for each new release before upgrading when using the API programmatically. Pin `pygount` to a specific version if API stability is critical.","message":"The programmatic API (e.g., `SourceAnalysis`, `ProjectSummary`) is explicitly stated as 'work in progress and subject to change'. Breaking changes to the API might occur in future versions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use the `--folders-to-skip` and `--names-to-skip` command-line options to override or extend the default exclusion patterns if you need to include such files/folders in your analysis.","message":"Pygount automatically excludes certain files and folders from analysis by default. This includes files starting with a dot (`.`), files ending with a tilde (`~`), and folders named `.svn` or starting with a dot (`.`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider `pygount` for accuracy and language coverage rather than raw speed. For very large codebases where performance is critical, profile `pygount`'s execution or consider alternative tools if its speed is insufficient.","message":"While robust, pygount can be notably slower than other command-line SLOC counting tools due to its detailed `pygments`-based parsing.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}