当我在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