I’m currently writing an autocompletion for python. Therefore I need to find the modules, that are imported.
This seems pretty easy with imp.find_module, but it doesn’t work if I insert a path, which is important for an autocompletion. Inserting a path means, that it won’t find the system modules.
The solution would be to get the PYTHONPATH and then, insert it. But that is really not a nice way to do it, since I just want the default path. Additionally PYTHONPATH is not even in my os.environ[].
Is there really no alternative to this complicated procedure? And if there is not, how can I get the default paths to the libraries?
Is there maybe even a better solution, which crawls automatically through the directories?
Note: Since I’m writing an autocompletion, I don’t want to actually import/execute any code.
To get the path of the standard libraries use:
Using these paths you can search through them by using
os.listdir, such as:You will have check if you check if the path is a directory.
You might have to check this recursively and check for extensions.
This will get you the idea where to start.
You will also have to check your current directory (
os.getcwd()) for files that can be a part of a package (python package structure).