Summary
A variable imported with from cityMap import * appeared in autocomplete but failed at runtime because the module executed before the Pygame environment was ready, and because wildcard imports hide ordering problems. The variable gray was never actually available in the namespace you expected, even though the editor suggested it.
Root Cause
The failure comes from a combination of Python import semantics and Pygame side effects:
- *Wildcard imports (`import `) copy names, not live references**
- Modules execute immediately on import, including any top‑level Pygame calls
city.pyreferencesscreenduring import, butscreenonly exists inmain.py- Because
city.pycrashes or partially loads, its variables never finish defining - Autocomplete shows symbols from static analysis, not from runtime execution
Why This Happens in Real Systems
Real Python systems often hit this because:
- Import order matters, and circular imports silently break initialization
- Pygame objects (like
screen) must be created afterpygame.init() - Top‑level code in modules is dangerous when it depends on external state
- Wildcard imports hide where variables come from, making debugging harder
Real-World Impact
This pattern causes:
- Partially imported modules, where some variables never get defined
- Silent failures when top‑level code runs too early
- Circular import loops that prevent modules from finishing execution
- Confusing autocomplete suggestions that don’t reflect runtime behavior
Example or Code (if necessary and relevant)
A corrected structure looks like this:
# city.py
gray = (100, 100, 100)
darkgray = (50, 50, 50)
def draw_city(screen):
import pygame
pygame.draw.rect(screen, gray, (40, 500))
# main.py
import pygame
from city import draw_city, gray
pygame.init()
screen = pygame.display.set_mode((700, 500))
clock = pygame.time.Clock()
print(gray)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
draw_city(screen)
pygame.display.flip()
clock.tick(30)
pygame.quit()
How Senior Engineers Fix It
Experienced engineers avoid this class of bug by:
- *Never using `import `**
- Never putting Pygame drawing calls at module top level
- Ensuring modules do not depend on each other’s runtime state
- Passing dependencies explicitly (e.g.,
screenas a function argument) - Keeping modules pure—no side effects during import
Why Juniors Miss It
This issue is subtle because:
- Autocomplete suggests variables even when they won’t exist at runtime
- Wildcard imports hide the true source of names
- Python’s import system silently executes code, which feels “invisible”
- Pygame tutorials often show everything in one file, masking import‑order issues
- Errors occur at runtime, not at import time, making debugging harder
The key takeaway: avoid wildcard imports, avoid top‑level Pygame calls, and structure modules so they don’t depend on each other’s initialization order.