imp.load_source方法的第一个参数是做什么的? [英] What does the first argument of the imp.load_source method do?
问题描述
我正在阅读此 SO问题关于从绝对路径导入模块.一个答案建议使用以下代码:
I'm reading this SO question about importing modules from absolute path. An answer suggest to use following code:
import imp
foo = imp.load_source('module.name', '/path/to/file.py')
foo.MyClass()
我想从具有以下结构的dir导入文件(它是包):
I want to import file from dir which has following structure (it is package):
__int__.py
model_params.py
我已经做到了:
import01 = imp.load_source('module.name', '/home/wakatana/experiments/model_params/model_params.py')
现在,我可以通过import01.VARIABLE_NAME
访问model_params.py
中的变量.似乎等效于import numpy as np
.其中model_params.py
如numpy
,而import01
如np
.
Now I can access variables within model_params.py
via import01.VARIABLE_NAME
. It seems like equivalent to import numpy as np
. Where model_params.py
is like numpy
and import01
is like np
.
我想问一下load_source
方法的第一个参数是做什么的? help(imp)
几乎没有提及load_source
方法,例如以下help(imp.load_source)
返回load_source(...)
I would like to ask what does the first argument of load_source
method do? help(imp)
says practically nothing about load_source
method, e.g. following help(imp.load_source)
returns load_source(...)
谢谢
根据behzad.nouri评论进行编辑
在 load_source 的文档页面上说:>
On documentation page of load_source is said:
name参数用于创建或访问模块对象.
The name argument is used to create or access a module object.
但是当我尝试访问module.name
时,出现关于未定义模块的错误.另外,为什么没有help
可以访问的文档,我可以以某种方式安装它吗?我希望文档是python中代码本身的一部分,还是不将其内置而将其在线化是常见的做法吗?
But when I try to access module.name
I get an error about not defined module. Also why there is not documentation that can be accessed by help
, can I install it somehow? I was hoping that documentation is part of the code itself in python, or is it common practice to not have it built-in but rather have it on-line?
推荐答案
官方文档有关该主题的更多信息.
The official documentation has a bit more info on the topic.
基本上,加载模块所用的名称将在导入该模块的其他文件中使用.即使python路径中的任何地方都不存在module.name
模块,但是如果您加载某个模块并为其命名,那么使用该名称执行常规import
的其他模块将不会引发错误并按预期工作.也许有一个小例子可以更好地说明这一点:
Basically the name that you load the module with will be used in other files that import that module. Even though no module.name
module exists anywhere in the python path, if you load some module and give it that name, other modules that do a regular import
with that name will not raise errors and work as expected. Perhaps a small example would illustrate this better:
value = 1337
/tmp/test/bar.py
from foo.bar import value
def print_val():
print value
/tmp/test/run.py
import imp
foo = imp.load_source('foo.bar', '/tmp/test/foo.py')
import bar
bar.print_val()
如预期的那样,您将1337
打印到屏幕上.如果名称不是foo.bar
,则在bar.py
中导入将失败,因为实际上不存在这样的模块.
As expected, you get 1337
printed to the screen. If the name was not foo.bar
, the import would have failed in bar.py
since no such module actually exists.
此方法实际上可用于猴子修补,因为它将覆盖第三方模块内部的导入.
This method can actually be used for monkey patching as it would override imports inside 3rd party modules.
这篇关于imp.load_source方法的第一个参数是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!