# Buggy code
def main(
    args: Optional[List[str]] = NotProvided,
    stdin: Optional[IO] = NotProvided,
    pwd: Optional[str] = None,
) -> None:
    args = sys.argv[1:] if args is NotProvided else args
    stdin = sys.stdin if stdin is NotProvided else stdin

    subcommands = list_subcommands()
    parser = argparse.ArgumentParser(
        prog=__command__,
        description="ArchiveBox: The self-hosted internet archive",
        add_help=False,
    )
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "--help",
        "-h",
        action="store_true",
        help=subcommands["help"],
    )
    group.add_argument(
        "--version",
        action="store_true",
        help=subcommands["version"],
    )
    group.add_argument(
        "subcommand",
        type=str,
        help="The name of the subcommand to run",
        nargs="?",
        choices=subcommands.keys(),
        default=None,
    )
    parser.add_argument(
        "subcommand_args",
        help="Arguments for the subcommand",
        nargs=argparse.REMAINDER,
    )
    command = parser.parse_args(args or ())

    if command.help or command.subcommand is None:
        command.subcommand = "help"
    elif command.version:
        command.subcommand = "version"

    if command.subcommand not in ("help", "version", "status"):
        from ..logging import log_cli_command

        log_cli_command(
            subcommand=command.subcommand,
            subcommand_args=command.subcommand_args,
            stdin=stdin,
            pwd=pwd or OUTPUT_DIR,
        )

    run_subcommand(
        subcommand=command.subcommand,
        subcommand_args=command.subcommand_args,
        stdin=stdin,
        pwd=pwd or OUTPUT_DIR,
    )


# Traceback (ImportError)
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last):                                                                                                                                                                                                    [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)

Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)

# Fixed code
def main(
    args: Optional[List[str]] = NotProvided,
    stdin: Optional[IO] = NotProvided,
    pwd: Optional[str] = None,
) -> None:
    args = sys.argv[1:] if args is NotProvided else args
    stdin = sys.stdin if stdin is NotProvided else stdin

    subcommands = list_subcommands()
    parser = argparse.ArgumentParser(
        prog=__command__,
        description="ArchiveBox: The self-hosted internet archive",
        add_help=False,
    )
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        "--help",
        "-h",
        action="store_true",
        help=subcommands["help"],
    )
    group.add_argument(
        "--version",
        action="store_true",
        help=subcommands["version"],
    )
    group.add_argument(
        "subcommand",
        type=str,
        help="The name of the subcommand to run",
        nargs="?",
        choices=subcommands.keys(),
        default=None,
    )
    parser.add_argument(
        "subcommand_args",
        help="Arguments for the subcommand",
        nargs=argparse.REMAINDER,
    )
    command = parser.parse_args(args or ())

    if command.help or command.subcommand is None:
        command.subcommand = "help"
    elif command.version:
        command.subcommand = "version"

    if command.subcommand not in ("help", "version", "status"):
        from ..logging_util import log_cli_command

        log_cli_command(
            subcommand=command.subcommand,
            subcommand_args=command.subcommand_args,
            stdin=stdin,
            pwd=pwd or OUTPUT_DIR,
        )

    run_subcommand(
        subcommand=command.subcommand,
        subcommand_args=command.subcommand_args,
        stdin=stdin,
        pwd=pwd or OUTPUT_DIR,
    )