Importing Python modules from the web

Last week I came across this piece of code (sorry, I lost the original source, if you know it please let me know!) which uses GitHub raw mode and Python’s import hooks to import modules directly from GitHub.

I modified it slightly, to make it a bit more generic and allow you to import Python modules from arbitrary web URLs. Why would you want to do this? Is it safe? Well, you shouldn’t use this unless you are running your script in some restricted environment where you can’t import arbitrary modules. Security could be an issue here, so be careful!

Here is the code:

[gist]https://gist.github.com/1037345[/gist]

It contains 3 test cases that show how it could be used. I’ll post a ‘real world’ usage later today.

:wq

3 thoughts on “Importing Python modules from the web

  1. This could be very useful for the new Raspberry Pi [1]… managing python libraries is one of the biggest problems that new python programmers encounter. The challenge is to set up and manage a “safe” repository of Python Libraries.[1] http://www.raspberrypi.org

Leave a Reply

Your email address will not be published. Required fields are marked *