How to list imported modules?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To "list imported modules" in Python, you first need to decide what you mean by imported. You might want the modules currently loaded in a running interpreter, or you might want the import statements that appear in a source file without executing that file.
Listing Modules Already Loaded at Runtime
Python keeps a registry of loaded modules in sys.modules. If your code has already run and you want to inspect what the interpreter has imported so far, this is the simplest option.
This shows everything loaded in the current process, including modules imported indirectly by Python itself or by libraries you imported earlier.
If you only care about top-level package names:
That makes the list easier to scan, but it is still a runtime view, not a static analysis of your file.
Listing Import Statements from a Python File
If you want to know what a file imports without executing it, parse the file with ast. This is safer than using exec or importing the file just to inspect its dependencies.
This works for statements such as:
The output tells you which modules the source references, even if the file would fail at runtime.
Runtime Listing and Static Listing Answer Different Questions
This distinction matters a lot.
sys.modules answers, "What has this interpreter loaded already?"
ast answers, "What import statements appear in this source code?"
Those answers are not always the same. A file may contain conditional imports, imports inside functions, or imports that fail at runtime. A running session may also contain many modules that your file never imports directly.
For example:
Static analysis sees numpy in the file. Runtime inspection does not, because the branch never executes.
Listing Third-Party Packages Is a Different Task
Sometimes people ask this question when what they really want is the environment's installed packages. That is not the same as imported modules.
Installed packages can be listed with tools such as:
or:
Those commands show what is installed in the environment, not what a given script imports.
A Practical Utility Script
If you want a reusable command-line tool for static inspection, this is a solid starting point:
Run it like this:
That gives you a quick, repeatable way to inspect source dependencies without running application code.
Common Pitfalls
The most common mistake is mixing up loaded modules with installed packages. sys.modules is about the current interpreter session, while pip list is about the environment.
Another issue is assuming static analysis captures dynamic imports. Calls such as importlib.import_module(...) may not appear as plain import statements, so an ast walk will not necessarily catch them.
Relative imports can also be confusing. A statement like from .helpers import x is valid in a package, but the raw output may appear as a relative module path rather than a fully resolved absolute name.
Finally, avoid importing unknown files just to discover their imports. That can execute arbitrary code and produce side effects.
Summary
- Use
sys.modulesto list what the running interpreter has already loaded. - Use
astto list import statements from a source file without executing it. - Do not confuse imported modules with installed packages.
- Static analysis will miss some dynamic import patterns.
- Choose the method that matches the question you are actually trying to answer.
Related reading
- How to list Kafka consumer group using python
- How to list only top level directories in Python?
- How to load a model from an HDF5 file in Keras?
- How to load a pickle file from S3 to use in AWS Lambda?
- How to load a tflite model in script?
- How to load a tsv file into a Pandas DataFrame?
- How to load all modules in a folder?
- How to load an image and show the image using keras?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.