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, e.g. 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?