Is there a way to modify python/pip to, whenever an import fails at runtime, it would try to install the module (by the same name) from pip and then import the module?
I'd say it would be a nicer default than to just throw an error. If after loading the module from pip any problems happen, then it would also throw an error, similar to when I notice I cannot import something, try pip install and then come to the same exact error message.
I know we can use requirements.txt for bundling a package, but I'm talking from a "client" (person running the script) rather than "provider" (person providing the script) perspective; that is, I, as a client, would like to be able to import any script and have dependencies be solved automatically.
I understand that this could potentially cause trouble, but whenever I see an ImportError I'd just try to pip install the module anyway. Only if the module wouldn't work after pip installation "would I ask further questions".
I thought of something like this snippet that would be "built in" to the python process:
def getDirectoryOfInterpreter(p):
return "someway to return the directory"
try:
import somemodule
except ImportError:
os.system(getDirectoryOfInterpreter('THIS_INTERPRETER_BINARY') + ' pip install ' + "MODULE_NAME")
import somemodule