How to find out list of all available imports in python 3 via program? I tried this at first, but couldn't understand what it returned
import sys
sys.modules
I think this isn't the way, although this struck my mind first. I searched the web and found this http://effbot.org/librarybook/core-modules-index.htm
Can someone tell me whether this is correct or not?
Worked as with the Python 2 and the Python 3 (tested with the next version of the 2.7, 3.4, 3.5)
Sample output for the Python 2.7
Testing environment
it's possible that you merely needed a list of things imported from a module. after importing a module, say "foo.py", you can type the following within python interpreter to get a sorted list of names available in module "foo".
try
dir(sys)
after importingsys
.you can also type
dir()
at the prompt to get a sorted list of names currently in use by the interpreter. the names represent variables, functions, modules, classes, etc.for more thorough information on python modules you can search within the official documentation. specially look within the tutorial section.
here is official info on modules in python 3: http://docs.python.org/py3k/tutorial/modules.html
From http://docs.python.org/library/sys.html (a good place to look for documentation on python stdlib)
So modules is a dictionary (a mapping of module names to the actual module objects). To get just the names type
sys.modules.keys()
although it probably isn't that usefull.Why would you want to do that?
In any case, I doubt your link is relevant for what you are asking. The list of available modules that ships with python is here:
http://docs.python.org/py3k/library/index.html
The list of modules that are built in is here:
To get a list of everything you can import, including installed modules, you would have to go through the sys.path and look for modules "manually", which is not an entirely trivial task, considering these can be both python files, compiled files like .so and .dll, directories and even zip files, and you would have to handle pth files too.
However, I don't see any need for ever doing that.
Here's my answer for recent Python 2.x. It's certainly not perfect, and I haven't even tested it on Python 3, but I think it has a reasonable chance of being much more useful than any of the other answers and comments currently here - certainly it was good enough for my particular use case. As LennartRegebro is the expert in porting to Python 3, I'm sure he can shed more light on this approach.