How to do custom python imports?
Question: Is there a way to have custom behaviour for import statements in Python? How?
E.g.:

    
    
    import "https://github.com/kennethreitz/requests"
    import requests@2.11.1
    import requests@7322a09379565bbeba9bb40000b41eab8856352e
    

Alternatively, in case this isn't possible... Can this be achieved in a
standard way with function calls? How? E.g.:

    
    
    import gitloader
    repo = gitloader.repo("https://github.com/kennethreitz/requests")
    requests = repo.from_commit("7322a09379565bbeba9bb40000b41eab8856352e")
    

There are two reasons for why I would like to do this. The first reason is
convenience (Golang style imports). The second reason is that I need
cryptographic verification of plugin modules for a project. I'm using Python
3.x


Answer: What you're essentially asking is customization of the import steps. This was
made possible with [PEP 302](https://www.python.org/dev/peps/pep-0302/) which
brought about certain hooks for for customization.

That PEP is currently not the source from which you should learn how importing
works; rather, look at the [documentation for the `import`
statement](https://docs.python.org/3/reference/import.html) for reference.
There [it states](https://docs.python.org/3/reference/import.html#finders-and-
loaders):

> Python includes a number of default finders and importers. The first one
> knows how to locate built-in modules, and the second knows how to locate
> frozen modules. A third default finder searches an import path for modules.
> The import path is a list of locations that may name file system paths or
> zip files. _It can also be extended to search for any locatable resource,
> such as those identified by URLs._
>
> _The import machinery is extensible, so new finders can be added to extend
> the range and scope of module searching_.

In short, you'll need to define the appropriate finder (to find the modules
you're looking for) and an appropriate loader for loading these.

