简体   繁体   English

imp.load_source方法的第一个参数是做什么的?

[英]What does the first argument of the imp.load_source method do?

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()

I want to import file from dir which has following structure (it is package): 我想从具有以下结构的dir导入文件(它是包):

__int__.py
model_params.py

I've done this: 我这样做了:

import01 = imp.load_source('module.name', '/home/wakatana/experiments/model_params/model_params.py')

Now I can access variables within model_params.py via import01.VARIABLE_NAME . 现在我可以通过import01.VARIABLE_NAME访问model_params.py变量。 It seems like equivalent to import numpy as np . 这似乎等同于import numpy as np Where model_params.py is like numpy and import01 is like np . 其中model_params.py就像numpyimport01就像np

I would like to ask what does the first argument of load_source method do? 我想问一下load_source方法的第一个参数是做什么的? help(imp) says practically nothing about load_source method, eg following help(imp.load_source) returns load_source(...) help(imp)几乎没有说load_source方法,例如关注help(imp.load_source)返回load_source(...)

Thanks 谢谢

EDIT based on behzad.nouri comment 编辑基于behzad.nouri评论

On documentation page of load_source is said: load_source的文档页面上说:

The name argument is used to create or access a module object. name参数用于创建或访问模块对象。

But when I try to access module.name I get an error about not defined module. 但是当我尝试访问module.name我得到一个关于未定义模块的错误。 Also why there is not documentation that can be accessed by help , can I install it somehow? 另外,为什么没有可以通过help访问的文档,我可以以某种方式安装它吗? 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? 我希望文档是python中代码本身的一部分,或者通常的做法是不内置它而是让它在线?

The official documentation has a bit more info on the topic. 官方文档有关于该主题的更多信息。

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. 即使python路径中的任何位置都没有module.name模块,如果加载某个模块并为其指定名称,那么使用该名称进行常规import其他模块不会引发错误并按预期工作。 Perhaps a small example would illustrate this better: 也许一个小例子会更好地说明这一点:

/tmp/test/foo.py /tmp/test/foo.py

value = 1337

/tmp/test/bar.py /tmp/test/bar.py

from foo.bar import value

def print_val():
    print value

/tmp/test/run.py /tmp/test/run.py

import imp
foo = imp.load_source('foo.bar', '/tmp/test/foo.py')

import bar

bar.print_val()

As expected, you get 1337 printed to the screen. 正如预期的那样,您将1337打印到屏幕上。 If the name was not foo.bar , the import would have failed in bar.py since no such module actually exists. 如果名称不是foo.bar ,则导入将在bar.py失败,因为实际上不存在此类模块。

This method can actually be used for monkey patching as it would override imports inside 3rd party modules. 这种方法实际上可以用于猴子补丁,因为它会覆盖第三方模块内的导入。

From documentation 从文档

imp.load_source(name, pathname[, file]): imp.load_source(name,pathname [,file]):

Load and initialize a module implemented as a Python source file and return its module object. 加载并初始化实现为Python源文件的模块并返回其模块对象。 If the module was already initialized, it will be initialized again. 如果模块已经初始化,它将再次初始化。 The name argument is used to create or access a module object . name参数用于创建或访问模块对象 The pathname argument points to the source file. pathname参数指向源文件。 The file argument is the source file, open for reading as text, from the beginning. file参数是源文件,从头开始打开以作为文本读取。 It must currently be a real file object, not a user-defined class emulating a file. 它当前必须是真实的文件对象,而不是模拟文件的用户定义的类。 Note that if a properly matching byte-compiled file (with suffix .pyc or .pyo) exists, it will be used instead of parsing the given source file 请注意,如果存在正确匹配的字节编译文件(带后缀.pyc或.pyo),则将使用它而不是解析给定的源文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM