Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] 5 Easy Ways to List Imported Modules in Python

#1
5 Easy Ways to List Imported Modules in Python

5/5 – (1 vote)

Problem Formulation and Solution Overview


In this article, you’ll learn how to display the imported modules in Python.

As a Python Coder, you will encounter times when you need to view a list of all imported modules possessing a global or local scope. This article answers the question below.

? Question: How would we write Python code to display the imported modules?

We can accomplish this task by one of the following options:


Method 1: Use pip freeze


This method displays a list of all imported global module names and versions sorted, by default, in alphabetical order.

pip freeze

Navigate to the terminal window from an IDE and enter the above command. Then, hit the <Enter> key to execute. The output is sent to the terminal.

? Note: Your prompt may be different from the example shown above.




Output (snippet)

Your imported global module names and versions may differ from that shown below.


absl-py==1.0.0
anyio==3.5.0
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
arrow==1.2.2
asttokens==2.0.5
astunparse==1.6.3
attrs==18.2.0
Babel==2.10.1
backcall==0.2.0
beautifulsoup4==4.10.0
...
zope.interface==5.4.0]


Method 2: Use List Comprehension


This example uses the sys library with List Comprenehsion to return all imported local module names, by default, in an unsorted list.

import sys
results = [m.__name__ for m in sys.modules.values() if m]
results = sorted(results)
print(results)

This code loops through sys.modules.values() using __name__ (aka a dunder) and determines if the item is a locally scoped module. If so, the module name saves to results.

This code sorts the results variable and saves it back to itself for readability. These results are output to the terminal in list format.




Output (snippet)

Your imported local module names may differ from that shown below.


['main', '_abc', '_codecs', '_collections', '_distutils_hack', '_functools', '_imp', '_operator', '_signal', '_sitebuiltins', '_stat', '_thread', '_warnings', '_weakref', 'abc',...'zope']


Method 3: Use dir()


This example uses the dir() function to return all local module names in a sorted list format.

modules = dir()
print(modules)

The output below confirms this script displays only the names that apply to our local scope.




Output (snippet)

Your imported local module names may differ from that shown below.


['annotations', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec']


Method 4: Use inspect.getmember() and a Lambda


This example uses inspect.getmember() and a Lambda to return the imported local modules in a sorted format.

import inspect
import os
m = inspect.getmembers(os)
res = filter(lambda x: inspect.ismodule(x[1]), m) for r in res: print®

This code returns the names of the imported local modules and their location on the system as an iterable object. A for the loop is used to iterate through this and output one/line.




Output


('abc', <module 'abc' from 'C:\\mypythoninstall\\lib\\abc.py'>)
('path', <module 'ntpath' from 'C:\\mypythoninstall\\lib\\ntpath.py'>)
('st', <module 'stat' from 'C:\\mypythoninstall\\lib\\stat.py'>)
('sys', <module 'sys' from 'C:\\mypythoninstall\\lib\\sys.py'>)


Bonus: Count Modules


If you want to determine the total number of imported modules, use the dir() and len() functions.

count = dir()
print(len(count))

This code references the imported local modules and uses len() to determine how many are imported. The output is sent to the terminal.

Output

Your count may differ from the output below.


11


Summary

These four (4) methods to list imported modules should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!




https://www.sickgaming.net/blog/2022/05/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] 5 Expert-Approved Ways to Remove Unicode Characters from a Python Dict xSicKxBot 0 2 2 hours ago
Last Post: xSicKxBot
  Python Modules for GUI interfaces SickProdigy 0 5,176 02-18-2024, 10:49 AM
Last Post: SickProdigy
  [Tut] List Comprehension in Python xSicKxBot 0 2,101 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Python IndexError: Tuple Index Out of Range [Easy Fix] xSicKxBot 0 1,944 08-22-2023, 09:07 AM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 1,949 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,552 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,692 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 1,602 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python Snake Made Easy xSicKxBot 0 1,211 04-25-2023, 05:36 PM
Last Post: xSicKxBot
  [Tut] Python ? Put Legend Outside Plot ? – Easy Guide xSicKxBot 0 1,460 04-22-2023, 11:08 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016