A Linux administrator is reviewing a colleague's new Python backup script and notices the import section shown below, placed immediately above the main() function.
import sys, os
import requests, json
from backup.utils import logger
According to PEP 8 best practices, which revision would make this import block compliant with the style guide while preserving its functionality?
Move all import statements below the main() function and append # noqa to silence style warnings.
import json, logger, os, requests, sys
import json
import os
import sys
import requests
from backup.utils import logger
from backup.utils import *
from requests import *
import sys, os
PEP 8 states that each import should usually be on its own line, imports must appear at the top of the file, and they should be grouped in the order standard-library, third-party, local with a blank line separating each group. The corrected block therefore places the standard-library modules (json, os, sys) first, follows with the third-party requests import, and finishes with the project-specific import. Combining multiple modules on one line, moving imports below executable code, or using wildcard imports all violate PEP 8 recommendations.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is PEP 8 and why is it important for Python developers?
Open an interactive chat with Bash
Why should imports be grouped into standard-library, third-party, and local?
Open an interactive chat with Bash
What are wildcard imports and why are they discouraged in PEP 8?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access