简体   繁体   English

如何导入可通过 url 访问的 python 模块

[英]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.我的公司使用某种版本控制存档工具(Microsoft Windows),我想从中直接导入。 But I can't find a way to do that.但我找不到办法做到这一点。

Here's what 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.我尝试用“”(空格)替换 URL 的“%20”,但结果是一样的。 I also tried by using the imp module, but no better.我也尝试过使用imp模块,但没有更好。

So here are my questions:所以这是我的问题:

  1. is it possible to make an import from an URL?是否可以从 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.也许一个解决方案可能是使用 Sharepoint 的其他隐藏路径访问此文件,因此它的标签是合适的。

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...它可以工作,但对于生产版本来说是不安全的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM