{"id":124517,"date":"2022-05-04T17:56:04","date_gmt":"2022-05-04T17:56:04","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=341830"},"modified":"2022-05-04T17:56:04","modified_gmt":"2022-05-04T17:56:04","slug":"5-easy-ways-to-list-imported-modules-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/05\/04\/5-easy-ways-to-list-imported-modules-in-python\/","title":{"rendered":"5 Easy Ways to List Imported Modules in Python"},"content":{"rendered":"<div class=\"kk-star-ratings kksr-valign-top kksr-align-left \" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;341830&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Problem Formulation and Solution Overview<\/h2>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">In this article, you&#8217;ll learn how to display the imported modules in Python. <\/p>\n<p>As a Python Coder, you will encounter times when you need to view a list of all imported modules possessing a <strong>global <\/strong>or <strong>local <\/strong>scope. This article answers the question below.<\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><em><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How would we write Python code to display the imported modules?<\/em><\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">We can accomplish this task by one of the following options:<\/p>\n<ul type=\"video\" class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<li><strong>Method 1<\/strong>: Use <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-guide-of-all-pip-commands\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-guide-of-all-pip-commands\/\" target=\"_blank\">pip<\/a> freeze<\/code><\/li>\n<li><strong>Method 2<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a><\/li>\n<li><strong>Method 3<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dir-a-simple-guide-with-video\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dir-a-simple-guide-with-video\/\" target=\"_blank\"><code>dir()<\/code><\/a><\/li>\n<li><strong>Method 4<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/inspect.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/inspect.html\" target=\"_blank\"><code>inspect.getmember()<\/code><\/a> and a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-simple-introduction-of-the-lambda-function-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-simple-introduction-of-the-lambda-function-in-python\/\" target=\"_blank\">Lambda<\/a><\/li>\n<li><strong>Bonus<\/strong>: Count Number of Imported Modules<\/li>\n<\/ul>\n<hr class=\"wp-block-separator\" \/>\n<h2>Method 1: Use pip freeze<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method displays a list of all imported <strong>global<\/strong> module names and versions <strong>sorted<\/strong>, by default, in alphabetical order.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip freeze<\/pre>\n<p>Navigate to the terminal window from an IDE and enter the above command. Then, hit the &lt;<code>Enter<\/code>&gt; key to execute. The output is sent to the terminal.<\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>&nbsp;<strong>Note<\/strong>: Your prompt may be different from the example shown above.<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"PIP Commands \u2013 A Simple Guide\" width=\"780\" height=\"585\" src=\"https:\/\/www.youtube.com\/embed\/vtGZNLMi4yU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<p><strong>Output (snippet)<\/strong><\/p>\n<p>Your imported <strong>global <\/strong>module names and versions may differ from that shown below. <\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>absl-py==1.0.0<br \/>anyio==3.5.0<br \/>argon2-cffi==21.3.0<br \/>argon2-cffi-bindings==21.2.0<br \/>arrow==1.2.2<br \/>asttokens==2.0.5<br \/>astunparse==1.6.3<br \/>attrs==18.2.0<br \/>Babel==2.10.1<br \/>backcall==0.2.0<br \/>beautifulsoup4==4.10.0<br \/><strong>...<\/strong><br \/>zope.interface==5.4.0]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator\" \/>\n<h2>Method 2: Use List Comprehension<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses the <code>sys<\/code> library with <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprenehsion<\/a> to return all imported <strong>local<\/strong> module names, by default, in an unsorted list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2-3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import sys\nresults = [m.__name__ for m in sys.modules.values() if m]\nresults = sorted(results)\nprint(results)<\/pre>\n<p>This code loops through <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/sys.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/sys.html\" target=\"_blank\"><code>sys.modules.values()<\/code><\/a> using <code>__name__<\/code> (aka a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dunder-methods-cheat-sheet\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dunder-methods-cheat-sheet\/\" target=\"_blank\">dunder<\/a>) and determines if the item is a <strong>locally <\/strong>scoped module. If so, the module name saves to <code>results<\/code>. <\/p>\n<p>This code sorts the <code>results<\/code> variable and saves it back to itself for readability. These <code>results<\/code> are output to the terminal in <a href=\"https:\/\/blog.finxter.com\/python-lists\/\">list<\/a> format.<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Single and Double Underscore in Python [&quot;_&quot; vs &quot;__&quot;]\" width=\"780\" height=\"439\" src=\"https:\/\/www.youtube.com\/embed\/azb6JSp7RJI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<p><strong>Output (snippet)<\/strong><\/p>\n<p>Your imported <strong>local<\/strong> module names may differ from that shown below.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['<strong>main<\/strong>', '_abc', '_codecs', '_collections', '_distutils_hack', '_functools', '_imp', '_operator', '_signal', '_sitebuiltins', '_stat', '_thread', '_warnings', '_weakref', 'abc',...'zope']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator\" \/>\n<h2>Method 3: Use dir()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dir-a-simple-guide-with-video\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dir-a-simple-guide-with-video\/\" target=\"_blank\"><code>dir()<\/code><\/a> function to return all <strong>local <\/strong>module names in a <strong>sorted<\/strong> <a href=\"https:\/\/blog.finxter.com\/python-lists\/\">list<\/a> format. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">modules = dir()\nprint(modules)<\/pre>\n<p>The output below confirms this script displays only the names that apply to our <strong>local <\/strong>scope.<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Python dir() \u2014 A Simple Guide with Video\" width=\"780\" height=\"439\" src=\"https:\/\/www.youtube.com\/embed\/DZyICkSIPRI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<p><strong>Output (snippet)<\/strong><\/p>\n<p>Your imported <strong>local<\/strong> module names may differ from that shown below.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['annotations', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator\" \/>\n<h2>Method 4: Use <code>inspect.getmember()<\/code> and a Lambda<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/inspect.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/inspect.html\" target=\"_blank\"><code>inspect.getmember()<\/code><\/a> and a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/a-simple-introduction-of-the-lambda-function-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/a-simple-introduction-of-the-lambda-function-in-python\/\" target=\"_blank\">Lambda<\/a> to return the imported <strong>local <\/strong>modules in a <strong>sorted <\/strong>format.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3-4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import inspect\nimport os\nm = inspect.getmembers(os)\nres = filter(lambda x: inspect.ismodule(x[1]), m) for r in res: print(r)<\/pre>\n<p>This code returns the names of the imported <strong>local <\/strong>modules and their location on the system as an iterable object. A <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\"><code>for<\/code><\/a> the loop is used to iterate through this and output one\/line.<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Let&#039;s Play Finxter - The Lambda Function in Python\" width=\"780\" height=\"439\" src=\"https:\/\/www.youtube.com\/embed\/kBg4n52XoUQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<p><strong>Output<\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>('abc', &lt;module 'abc' from 'C:\\\\mypythoninstall\\\\lib\\\\abc.py'&gt;)<br \/>('path', &lt;module 'ntpath' from 'C:\\\\mypythoninstall\\\\lib\\\\ntpath.py'&gt;)<br \/>('st', &lt;module 'stat' from 'C:\\\\mypythoninstall\\\\lib\\\\stat.py'&gt;)<br \/>('sys', &lt;module 'sys' from 'C:\\\\mypythoninstall\\\\lib\\\\sys.py'&gt;)<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator\" \/>\n<h2>Bonus: Count Modules<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">If you want to determine the total number of imported modules, use the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dir-a-simple-guide-with-video\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dir-a-simple-guide-with-video\/\" target=\"_blank\"><code>dir()<\/code><\/a> and <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-len\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-len\/\" target=\"_blank\">len()<\/a><\/code> functions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">count = dir()\nprint(len(count))<\/pre>\n<p>This code references the imported <strong>local <\/strong>modules and uses <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-len\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-len\/\" target=\"_blank\">len()<\/a><\/code> to determine how many are imported. The output is sent to the terminal.<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>Your <code>count<\/code> may differ from the output below.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td>11<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator\" \/>\n<p> <span style=\"color: var(--contrast);font-size: 44px;font-weight: bold\">Summary<\/span> <\/p>\n<p>These four (4) methods to list imported modules should give you enough information to select the best one for your coding requirements.<\/p>\n<p>Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator\" \/>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation and Solution Overview In this article, you&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-124517","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/124517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=124517"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/124517\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=124517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=124517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=124517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}