Software Development

How can I programmatically list or print all modules currently imported in a Python script?

KI Asked by Kimberly Adams · 12-11-2024
0 upvotes 14,869 views 0 comments
The question

I am debugging a complex project with many nested dependencies and I need a way to see every single module that has been loaded into the current session. Is there a built-in function or a specific library that allows me to print a full list of imported modules at runtime, including sub-modules and third-party packages? I'm trying to identify potential version conflicts and redundant imports.

3 answers

0
PA
Answered on 14-11-2024

The most direct way to achieve this is by using the built-in sys module. Specifically, sys.modules is a dictionary that maps module names to modules which have already been loaded. You can simply iterate through the keys of this dictionary to see everything currently in the namespace. To make it readable, you can use print(list(sys.modules.keys())). Keep in mind that this will show a lot of built-in and internal modules that Python loads by default. If you only want to see top-level packages or specifically third-party ones, you might need to filter the list by checking the __file__ attribute of each module to see where it is located on your disk.

0
RI
Answered on 15-11-2024

Are you trying to do this for a simple script, or are you working within a virtual environment where you might also need to see the specific versions of these loaded modules?

JA 16-11-2024

I am working within a Virtualenv. Seeing the versions would actually be a huge help because I suspect that a sub-dependency is pulling in an older version of a library than what I have specified in my requirements file. If I can print the version alongside the module name, it would save me hours of manual checking across different site-packages folders.

0
SU
Answered on 17-11-2024

You can use the pkg_resources or importlib.metadata libraries to get more detailed info, but for a quick list, pip freeze in the terminal is often easier.

KI 18-11-2024

I agree with Susan, but sys.modules is definitely better if you need the info from inside the execution flow. It gives you the "truth" of what the interpreter is actually seeing right at that moment.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

World globe icon Country: India

Book Free Session