How can a Python module be imported from a URL?

Basically there is a module exactly for this purpose called httpimport. Currently it supports importing from a URL that contains the package/module and also from archives (.tar.*, .zip) that can be found in URLs (this is a way to handle remote dependencies).

It is fully integrated with Python’s import system so you don’t need to exec anything in globals(). You just:

>>> with httpimport.remote_repo(['package1'], 'http://my-codes.example.com/python_packages'):
...     import package1
...

and then package1 is usable for the rest of the script like it was a local resource.


Disclaimer: I’m the author of this module.


Edit (31/01/2023):
The syntax of most httpimport commands has changed after the 1.0.0 re-write. The new parameters for remote_repo omits the first argument, as below:

>>> with httpimport.remote_repo('http://my-codes.example.com/python_packages'):
...     import package1
...

You might want to look at all usage examples provided in the repository README:
https://github.com/operatorequals/httpimport#basic-usage

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)