Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:根据用户想要的文件导入文件_Python - Fatal编程技术网

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

Python:根据用户想要的文件导入文件,python,Python,我有以下目录结构 + 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_p

我有以下目录结构

+ 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”模块时遇到问题。它不断地说“没有这样的文件或目录”。我的理解是,它不知何故没有正确理解路径。有人能帮我解决这个问题吗

exec('import ' + sys.argv[2])
obj = test_plugin.TestPlugin()
这里sys.argv[2]是来自命令行参数的“test_plugin”字符串

编辑:避免使用exec的另一种方法:

import importlib
mod = importlib.import_module(sys.argv[2])
这里sys.argv[2]是来自命令行参数的“test_plugin”字符串

编辑:避免使用exec的另一种方法:

import importlib
mod = importlib.import_module(sys.argv[2])

好的,您的问题是一个与路径相关的问题。您希望脚本在load.py所在的目录下运行,但实际情况并非如此

你需要做的是:

import imp, os, plugins

path = os.path.dirname(plugins.__file__)
imp.load_source('TestPlugin', os.path.join(path, 'test_plugin.py')

其中,plugins是包含所有插件的模块(即,只有空的
\uuu init\uuu.py
),它将帮助您获得插件模块文件的完整路径。

好的,您的问题是与路径相关的问题。您希望脚本在load.py所在的目录下运行,但实际情况并非如此

你需要做的是:

import imp, os, plugins

path = os.path.dirname(plugins.__file__)
imp.load_source('TestPlugin', os.path.join(path, 'test_plugin.py')

其中,plugins是包含所有插件的模块(即,只有空的
\uuu init\uu.py
),它将帮助您获得插件模块文件的完整路径。

如果您需要“plugins”发现工具,另一种解决方案:

import imp, os
import glob

def load_plugins(path):
  """
  Assuming `path` is the only directory in which you store your plugins,
  and assuming each name follows the syntax:
  plugin_file.py -> PluginFile
  Please note that we don't import files starting with an underscore.

  """
  plugins = {}
  plugin_files = glob.glob(path + os.sep + r'[!_]*.py')
  for plugin_path in plugin_files:
    module_name, ext = os.path.splitext(plugin_path)
    module_name = os.path.basename(module_name)
    class_name = module_name.title().replace('_', '')
    loaded_module = imp.load_source(class_name, plugin_path) # we import the plugin
    plugins[module_name] = getattr(loaded_module, class_name)
  return plugins

plugins = load_plugins(your_path_here)
plugin_name = sys.argv[3]
plugin = plugins.get(plugin_name)
if not plugin:
    # manage a not existing plugin
else:
    plugin_instance = plugin() # creates an instance of your plugin

这样,您还可以通过更改密钥来指定不同的名称,例如,“test_plugins”=>“tp”。您不必初始化插件,但只要您想在运行时加载插件,就可以运行此函数

另一个解决方案,如果您想要“插件”发现工具:

import imp, os
import glob

def load_plugins(path):
  """
  Assuming `path` is the only directory in which you store your plugins,
  and assuming each name follows the syntax:
  plugin_file.py -> PluginFile
  Please note that we don't import files starting with an underscore.

  """
  plugins = {}
  plugin_files = glob.glob(path + os.sep + r'[!_]*.py')
  for plugin_path in plugin_files:
    module_name, ext = os.path.splitext(plugin_path)
    module_name = os.path.basename(module_name)
    class_name = module_name.title().replace('_', '')
    loaded_module = imp.load_source(class_name, plugin_path) # we import the plugin
    plugins[module_name] = getattr(loaded_module, class_name)
  return plugins

plugins = load_plugins(your_path_here)
plugin_name = sys.argv[3]
plugin = plugins.get(plugin_name)
if not plugin:
    # manage a not existing plugin
else:
    plugin_instance = plugin() # creates an instance of your plugin


这样,您还可以通过更改密钥来指定不同的名称,例如,“test_plugins”=>“tp”。您不必初始化插件,但只要您想在运行时加载插件,就可以运行此函数

您说您在尝试使用“imp”模块时遇到了问题,但到目前为止您尝试了哪些代码?你有什么问题?您不了解教程/文档的哪一部分?stackoverflow不是其他人为您编写代码的地方!你看了吗?我正在尝试以下代码
import imp
import.load\u source('TestPlugin','plugins/test\u plugin.py')
我输入错误
import.load\u source
。它应该是
imp.load\u source
。。。当你:1)没有显示你正在使用的代码,2)没有显示你正在获得的完整回溯时,我们怎么知道你出了什么问题?你说你在尝试使用“imp”模块时遇到了问题,但到目前为止你尝试了什么代码?你有什么问题?您不了解教程/文档的哪一部分?stackoverflow不是其他人为您编写代码的地方!你看了吗?我正在尝试以下代码
import imp
import.load\u source('TestPlugin','plugins/test\u plugin.py')
我输入错误
import.load\u source
。它应该是
imp.load\u source
。。。当你:1)没有显示你正在使用的代码,2)没有显示你正在得到的完整回溯时,我们怎么知道出了什么问题呢?如果
sys.argv[2]
os;操作系统(“邪恶命令”)
exec
eval
类函数是邪恶的,永远不能使用。如果
sys.argv[2]
os;操作系统(“邪恶命令”)
exec
eval
之类的函数是邪恶的,永远不会被使用。谢谢,没有注意到我输入了那个错误:-)谢谢,没有注意到我输入了那个错误:-)如果用户编写了自己的插件并想加载它怎么办?主程序无法硬编码其名称,因此需要动态发现和动态导入插件。是的,这样做并不难。如果用户编写了自己的插件并希望加载它怎么办?主程序无法硬编码其名称,因此需要动态发现和动态导入插件。是的,这并不难做到。