简体   繁体   中英

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

__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 . It seems like equivalent to import numpy as np . Where model_params.py is like numpy and import01 is like np .

I would like to ask what does the first argument of load_source method do? help(imp) says practically nothing about load_source method, eg following help(imp.load_source) returns load_source(...)

Thanks

EDIT based on behzad.nouri comment

On documentation page of load_source is said:

The name argument is used to create or access a module object.

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.

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:

/tmp/test/foo.py

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

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.

From documentation

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

Load and initialize a module implemented as a Python source file and return its module object. If the module was already initialized, it will be initialized again. The name argument is used to create or access a module object . The pathname argument points to the source file. The file argument is the source file, open for reading as text, from the beginning. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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