重新导入许多 python 个库的最简单方法是什么?

What is simplest way to reimport many python libraries?

我正在编写一个服务器,它将 运行 循环遍历在配置文件中找到的其他 python 脚本。

代码将类似于以下内容:

for script in CONFIG.scripts:
    import script as s
    s.some_function()
    s.another_function()

显然每个脚本都必须实现所有需要的功能,但它必须是第 3 方脚本,因为这是用户与服务器交互的方式。

重新导入库的最佳方法是什么? 我在考虑几个选项:

  1. import script as s; importlib.reload(s)
  2. del s; import s
  3. 将所有脚本导入某个列表,然后 运行覆盖它们?

考虑到与用户的界面必须尽可能简单,我宁愿不强迫他们做任何比定义所需功能更多的事情。

服务器是在 python 3.6 上编写的(但如果可以解决这种情况,可以将其迁移到 3.7)

您不需要重新加载! 只需 import script as s 即可。

示例:

a.py:

print("importing a")

def foo():
    print("foo from a")

b.py:

print("importing b")

def foo():
    print("foo from b")

main.py:

import a as c
c.foo()
import b as c
c.foo()

输出:

importing a
foo from a
importing b
foo from b