使用字符串作为名称导入文件 [英] Import file using string as name

查看:151
本文介绍了使用字符串作为名称导入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

用Python导入动态模块

我打算做一个很快就会出现一些文件,而组织它的最好方法是有一个列表,该列表将位于文件的最顶层,之后将会出现一些荒谬的代码来处理该列表控制的内容它是如何运作的。我只想写一次这样的列表,并且列表是这种格式的文件夹和文件名列表:

I intend to make a suite of files at some point soon, and the best way to organize it is to have a list, that list will be at the very top of a file, and after it will come a ridiculous amount of code to handle what that list controls and how it operates. I'm looking to write said list only once, and said list is a list of folder and file names in this format:

[(folder/filename, bool, bool, int), (folder/filename, bool, bool, int)]

如您所见,文件夹/文件名是相同的(有点)。文件名是文件夹名称,最后是 .py ,但是执行导入XXX时你不需要导入XXX.py,所以我没有看到这导致问题。

As you can see, folder/filename are the same (sort of). File name is folder name with .py on the end, but doing import XXX you don't need to do import XXX.py, so I don't see this causing an issue.

我面临的问题是使用此方法导入...

The problem I'm facing is importing using this method...

for (testName, auto, hardware, bit) in testList:
    print(testName)
    paths = "\\" + testName
    print paths
    addpath(paths)
    sys.modules[testName] = testName # One of a few options I've seen suggested on the net
    print("Path Added")
    test = testName + ".Helloworld()"
    eval(test)

因此,对于我所进行的每项测试,请打印名称,汇编包含路径的字符串(\\ testName),对于此示例,打印测试路径,然后将路径添加到列表中( sys.path.append(path)),然后打印确认它发生,然后汇编一个字符串,该字符串将由 eval 用于测试主要的dule并最终评估它。

So for each test I have, print the name, assemble a string which contains the path ("\\testName"), for this example, print the test path, then add the path to the list (sys.path.append(path)), then print to confirm it happened, then assemble a string which will be executed by eval for the tests main module and eventually eval it.

如你所见,我目前必须在顶部有一个导入列表。我不能简单地导入 testName testName 的内容是我要导入的模块的名称) ,因为它会尝试找到一个名为 testName 的模块,而不是一个名为 testName 的内容的模块。

As you can see, I'm currently having to have a list of imports at the top. I can't simply do import testName (the contents of testName are the name of the module I wish to import), as it will try to find a module called testName, not a module called the contents of testName.

我已经看到了一些这样做的例子,但在我的情况下找不到哪些工作。如果有人可以从字面上抛出一大块代码来完成它那就太棒了。

I've seen a few examples of where this has been done, but can't find any which work in my circumstances. If someone could literally throw a chunk of code which does it that would be wonderful.

我还要求我不要挂起,画画,也不要四处使用对于eval,它在一个非常受控制的环境中使用(它循环的列表在.py文件中,所以没有最终用户应该把它弄乱)。

I'd also request that I'm not hung, drawn, nor quartered for use of eval, it is used in a very controlled environment (the list through which it cycles is within the .py file, so no "end user" should mess with it).

推荐答案

不确定我是否理解了所有内容,但您可以使用 __ import __ 动态导入模块:

Not sure if I understood everything correctly, but you can import a module dynamically using __import__:

mod = __import__(testName)
mod.HelloWorld()

编辑:我不知道python docs不支持使用 __ import __ 用户代码: __import__文档(如Bakuriu所述)

Edit: I wasn't aware that the use of __import__ was discouraged by the python docs for user code: __import__ documentation (as noted by Bakuriu)

这也应该有效,并且会被认为是更好的风格:

This should also work and would be considered better style:

mod = importlib.import_module(testName)
mod.HelloWorld()

这篇关于使用字符串作为名称导入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆