从网络导入模块
有没有办法让 python 从网络读取模块?
我们有很多机器,每次更改模块时手动更新每台机器会花费太多精力,因此我希望 python 从网络上的某个位置获取模块。
有什么想法吗?
Is there a way to get python to read modules from a network?
We have many machines and it would be a too much effort to update each machine manually each time I change a module so I want python to get the modules from a location on the network.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将您的网络位置安装到文件系统中,并将该路径添加到 PYTHONPATH 中。这样,本地计算机上的 Python 将能够看到远程位置中存在的模块。
你不能直接从远程模块导入,就像在html中指定js文件一样。
Mount your network location into your file-system and add that path to your PYTHONPATH. That way, Python on your local machine will be able to see the modules which are present in the remote location.
You cannot directly import from modules remotely, like specifying a js file in html.
我最终是如何做到这一点的:
控制面板\所有控制面板项\系统>>高级>>环境变量>>系统变量>>新>>名称 = PYTHONPATH,值 = \server\scriptFolder
感谢大家的帮助:)
How I ended up doing this:
Control Panel\All Control Panel Items\System >> Advanced >> Environment Variables >> System Variables >> New >> Name = PYTHONPATH, value = \server\scriptFolder
Thanks everyone for all the help :)
值得注意的是,存在一个用于导入通过 HTTP/S 可用的包/模块的模块,它是
httpimport< /代码>
。这适用于 Python2 和 Python3。
因此,从接受的答案来看,事实证明有多种方法可以以编程方式导入远程模块“如 javascript”,如下所示:
编辑 (31/01/2023):
大多数
httpimport
命令的语法在1.0.0
重写后发生了变化。remote_repo
的新参数省略了第一个参数,如下所示:您可能需要查看存储库自述文件中提供的所有使用示例:
https://github.com/operatorequals/httpimport#basic-usage
It might be notable that a module for importing packages/modules available through HTTP/S exists and it is
httpimport
. This is for both Python2 and Python3.So, as of the accepted answer, it turns out that there are ways to programmatically import remote modules "like javascript" as follows:
Edit (31/01/2023):
The syntax of most
httpimport
commands has changed after the1.0.0
re-write. The new parameters forremote_repo
omits the first argument, as below:You might want to look at all usage examples provided in the repository README:
https://github.com/operatorequals/httpimport#basic-usage
我相信您正在寻找一种分布式计算框架,您可以在其中将代码和数据部署到一个节点,并将它们作为任务分布在一组客户端/服务器/对等点之间。检查 Pyro、execnet、并行 Python、Jug 和 RPyC。
I believe you're looking for a distributed computing framework, where you deploy code and data to one node and they are distributed as task among a cluster of clients/servers/peers. Check Pyro, execnet, Parallel Python, Jug and RPyC.
虽然想要通过网络导入模块有点病态,但它实际上是可能的。查看
zipimport
的源代码 了解如何做到这一点。While it's a little pathological to want to import modules over the network, it is actually possible. Take a look at the source for
zipimport
to get an idea of how it can be done.