Skip to main content
 首页 » 编程设计

Python:根据用户想要的文件导入文件

2025年05月04日7傻小

<分区>

我有以下目录结构

+ code 
| 
--+ plugins 
  | 
  -- __init__.py 
  -- test_plugin.py (has a class TestPlugin) 
  -- another_test_plugin.py (has a class AnotherTestPlugin) 
--+ load.py 
--+ __init__.py 

在 load.py 中,我希望能够仅初始化用户指定的那些类。例如,假设我做了类似的事情

$ python load.py -c test_plugin # Should only import test_plugin.py and initialize an object of the TestPlugin class 

我在尝试使用“imp”模块时遇到问题。它一直说“没有这样的文件或目录”。我的理解是它在某种程度上没有正确理解路径。有人可以帮我解决这个问题吗?