{"id":341,"date":"2021-04-01T19:59:23","date_gmt":"2021-04-01T19:59:23","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=341"},"modified":"2022-04-11T20:22:59","modified_gmt":"2022-04-11T20:22:59","slug":"100-days-of-learning-day-24-wrapping-up-on-the-python-importing-process","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/100-days-challenge\/100-days-of-learning-day-24-wrapping-up-on-the-python-importing-process\/","title":{"rendered":"100 Days of Learning: Day 24 \u2013 Wrapping up on the Python importing process"},"content":{"rendered":"\n

Photo by Boitumelo Phetla<\/a> on Unsplash<\/a><\/p>\n\n\n\n

Here is my Log book<\/a>.<\/p>\n

Code for today<\/a>.<\/p>\n

A handy function<\/h2>\n

The Definitive Guide to Python import Statements<\/a> has a neat little bit of code that will show you all the modules that you can import. For my exploration I will wrap this into a function.<\/p>\n

Edit example.py and add the following function<\/p>\n

def all_available_modules(search_path = ['.']):\n    import pkgutil\n    all_modules = [x[1] for x in pkgutil.iter_modules(path=search_path)]\n    print('All available modules:')\n    print(all_modules)\n...\nif __name__ == '__main__':\n\t...\n\tall_available_modules()\n<\/code><\/pre>\n
# The file tree at the moment\nproject\n  \u251c\u2500\u2500 example.py\n  \u251c\u2500\u2500 module1.py\n  \u251c\u2500\u2500 package1\n  \u2502\u00a0\u00a0 \u251c\u2500\u2500 __init__.py\n  \u2502\u00a0\u00a0 \u2514\u2500\u2500 hello.py\n  \u2514\u2500\u2500 time.py\n\n# Run example.py\n$ python example.py\n...\nAll available modules:\n['example', 'module1', 'package1', 'time']\n\n# Modify the call to be this and then run it again:\nall_available_modules(None)\n\n...\nAll available modules:\n['example', 'module1', 'package1', 'time', '__future__', '_aix_support',\n '_bootlocale', '_bootsubprocess', '_collections_abc', '_compat_pickle',\n '_compression', '_markupbase', '_osx_support', '_py_abc', '_pydecimal',\n '_pyio', '_sitebuiltins', '_strptime', '_sysconfigdata__darwin_darwin',\n '_threading_local', '_weakrefset', 'abc', 'aifc', 'antigravity', 'argparse',\n 'ast', 'asynchat', 'asyncio', 'asyncore', 'base64', 'bdb', 'binhex',\n... # and the list goes on and on\n<\/code><\/pre>\n

What is the difference between absolute and relative imports?<\/h2>\n