I am writing a script to search data provided by users may contain Python code which may be calling modules. I am able to retrieve the code as a string, but I need a reliable way to determine what modules/packages are used within it so I can compare to what is installed on my system. This is proving to be a seemingly insurmountable task. Especially since I am essentially restricted to default packages for a Python 3.x install.
I'm sure there has to be a way without being relegated to heuristics or regex. I am hoping for a method that will parse the string and be able to return it using pkgutil or the like. I tried here, but it apparently REALLY doesn't like having a string passed to it instead of a file path. I was contemplating trying to convert the string to a filestream in memory and passing that, but I'm not sure that would be any better.