从网络导入模块

发布于 2024-10-12 08:12:13 字数 112 浏览 4 评论 0原文

有没有办法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情绪 2024-10-19 08:12:13

将您的网络位置安装到文件系统中,并将该路径添加到 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.

风向决定发型 2024-10-19 08:12:13

我最终是如何做到这一点的:

控制面板\所有控制面板项\系统>>高级>>环境变量>>系统变量>>新>>名称 = 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 :)

久随 2024-10-19 08:12:13
sys.path.append(r'\\network\path')
import module
sys.path.append(r'\\network\path')
import module
指尖上的星空 2024-10-19 08:12:13

值得注意的是,存在一个用于导入通过 HTTP/S 可用的包/模块的模块,它是 httpimport< /代码>。这适用于 Python2 和 Python3。

因此,从接受的答案来看,事实证明有多种方法可以以编程方式导入远程模块“如 javascript”,如下所示:

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

编辑 (31/01/2023):
大多数httpimport命令的语法在1.0.0重写后发生了变化。 remote_repo 的新参数省略了第一个参数,如下所示:

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

您可能需要查看存储库自述文件中提供的所有使用示例:
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:

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

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

陈年往事 2024-10-19 08:12:13

我相信您正在寻找一种分布式计算框架,您可以在其中将代码和数据部署到一个节点,并将它们作为任务分布在一组客户端/服务器/对等点之间。检查 Pyroexecnet并行 PythonJugRPyC

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.

漆黑的白昼 2024-10-19 08:12:13

虽然想要通过网络导入模块有点病态,但它实际上是可能的。查看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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文