多次导入同一文件名

发布于 2024-10-08 22:43:32 字数 871 浏览 0 评论 0原文

我正在尝试创建一个插件系统,并且有一个将所有模块导入到数组中的函数。

插件布局:

插件目录/

插件目录/聊天

pluginsDir/chat/main.py

这是查找并导入插件的函数:

if os.path.exists(pluginsDir):
    for path, dirArray, fileArray in os.walk(pluginsDir):
        for fileName in fileArray:
            if fileName == "main.py":
            sys.path.append(path)
            try:
                plugins.append(__import__("main"))
            except:
                print 'Could not import plugin, "'+path+'": plugin contains errors or is not a real plugin.'

如果我只有一个插件,这很好,但当我有多个插件时,它会继续导入它检测到的第一个插件。

插件布局:

插件目录/

插件目录/聊天

pluginsDir/chat/main.py

插件目录/构建

pluginsDir/build/main.py

我尝试在try语句之后添加sys.path.remove(path),但是它在我导入模块后不会删除路径。

我该怎么做才能正确导入我的插件?

I am trying to create a plugin system and I have a function that imports all the modules into an array.

Layout of Plugins:

pluginsDir/

pluginsDir/chat

pluginsDir/chat/main.py

And this is the function that finds and imports the plugins:

if os.path.exists(pluginsDir):
    for path, dirArray, fileArray in os.walk(pluginsDir):
        for fileName in fileArray:
            if fileName == "main.py":
            sys.path.append(path)
            try:
                plugins.append(__import__("main"))
            except:
                print 'Could not import plugin, "'+path+'": plugin contains errors or is not a real plugin.'

This is fine if I only have one plugin but when I have multiple plugins it keeps importing the first plugin it detects.

Layout of Plugins:

pluginsDir/

pluginsDir/chat

pluginsDir/chat/main.py

pluginsDir/build

pluginsDir/build/main.py

I've tried to addsys.path.remove(path)after my try statement, but it doesn't remove the path after I have already imported the module.

What can I do to import my plugins correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

咆哮 2024-10-15 22:43:32

sys.path.append(path) 会将插件文件夹附加到 sys.path末尾。由于 Python 从前往后搜索 sys.path 中的文件夹,因此不会找到在列表末尾附加其他路径,因为之前在 sys.path 中指定的文件夹中的任何 main.py 模块 本质上将隐藏列表末尾文件夹中的模块。相反,您可以使用 sys.path.insert(0, path) 来将新路径添加到列表的前面。

您应该查看 Python 包,以找到更好地构建插件的方法。

plugindir/
    __init__.py
    plugin1/
        __init__.py
    plugin2/
        __init__.py

使用 Python 包,可以通过以下方式轻松实现脚本中的循环:

sys.path.insert(0, path_to_plugindir)
for folder in dirArray:
    __import__(folder)

sys.path.append(path) will attach the plugin folder to the end of sys.path. Since Python searches folders in sys.path from front to back, appending additional paths to the end of the list will not be found because any main.py modules in folder specified earlier in sys.path will essentially hide modules in folders at the end of the list. Instead, you could use sys.path.insert(0, path) to add new paths to the front of the list.

You should look at Python packages for a way to better structure the plugins.

plugindir/
    __init__.py
    plugin1/
        __init__.py
    plugin2/
        __init__.py

With Python packages, the loop in your script can be easily implemented with:

sys.path.insert(0, path_to_plugindir)
for folder in dirArray:
    __import__(folder)
丶情人眼里出诗心の 2024-10-15 22:43:32

Python 模块系统是处理命名空间的一种非常酷的方式。将多个同名模块导入到当前命名空间会使它变得混乱。

无需遍历pluginsDir 并导入每个文件,Python 会为您执行此操作(from pluginsDir import *)。如果 main.py 仅执行初始化操作,您可以将代码移至 pluginsDir/chat/__init__.py

导入pluginsDir引用您的插件(例如“pluginsDir.chat”)被认为是更好的做法。

The Python module system is just a very cool way to handle namespaces. Importing several modules with the same name to your current namespace will clutter it.

No need to traverse the pluginsDir and import each file, Python will do that for you (from pluginsDir import *). If main.py does only initialization stuf you can move the code to pluginsDir/chat/__init__.py.

Import pluginsDir refering to your plugins like 'pluginsDir.chat' is considered better pratice.

番薯 2024-10-15 22:43:32

你的内部 for 循环没有缩进,我根本不明白你的代码为什么运行。修复缩进可能会解决问题。

Your inner for loop is not indented, I don't see why your code runs at all. Fixing the indentation might fix the problem.

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