How does Python find modules when you import?

Python modules, packages and environments · Experienced

Model answer

It searches sys.modules (already imported), then each location in sys.path in order: the script's directory, installed site-packages and others. Naming your file like a standard module (for example random.py) can shadow it and cause confusing errors.

Learn this topic

Modules, pip and Virtual Environments in Python in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions