您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从不同的文件夹导入模块

从不同的文件夹导入模块

当我在Python 2.7中执行此操作时,我使用:

import sys
sys.path.append('C:/python/files/folder1')

import a
import a1

这是我将目录中的所有模块导入字典中的一种技巧:

import os
import sys

dir_of_interest = 'C:/python/files/folder1'
modules = {}

sys.path.append(dir_of_interest)
for module in os.listdir(dir_of_interest):
    if '.py' in module and '.pyc' not in module:
        current = module.replace('.py', '')
        modules[current] = __import__(current)

我刚刚构建了它,但它非常hacky,但它可能更像您想要的东西。因此,访问您想要的模块,而不是说module_name.thing您会说modules["module_name"].thing

其他 2022/1/1 18:33:37 有677人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶