开发者

How to import a python module accessible via an url

My company uses some kind of version-control archival tool (Microsoft Windows), and I would like to make direct imports from it. But I can't find a way to do that.

Here's wha开发者_开发知识库t I tried:

>>> import sys
>>> sys.path.append(r"http://some_path_copied_and_pasted/05.Autres%20Outils/Regen_Engine")
>>> sys.path
['C:\\Python26\\Lib\\idlelib', 'C:\\Python26\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'http://some_path_copied_and_pasted/05.Autres%20Outils/Regen_Engine']
>>> import regen_engine as r
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    import regen_engine as r
ImportError: No module named regen_engine

I tried by replacing the "%20" of the URL with " " (spaces), but the result is the same. I also tried by using the imp module, but no better.

So here are my questions:

  1. is it possible to make an import from an URL?
  2. if yes, how can I make it happen?

Maybe a solution could be to access this file with some other, hidden path of Sharepoint, so a tag for it would be appropriate.


import urllib
def urlimport(x):
    exec urllib.urlopen(x) in globals()

This is the wrong way to do this, you should never use this in production code, its just a proof of concept.


Try:

def dynamic_import_by_uri(file)
    file = os.path.abspath(file)
    exec open(file, 'rb') in globals()

It works but it is unsafe for production version...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜