python - How to import a module given the full path? -


how can load python module given full path? note file can anywhere in filesystem, configuration option.

for python 3.5+ use:

import importlib.util spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) foo.myclass() 

for python 3.3 , 3.4 use:

from importlib.machinery import sourcefileloader  foo = sourcefileloader("module.name", "/path/to/file.py").load_module() foo.myclass() 

(although has been deprecated in python 3.4.)

python 2 use:

import imp  foo = imp.load_source('module.name', '/path/to/file.py') foo.myclass() 

there equivalent convenience functions compiled python files , dlls.

see also. http://bugs.python.org/issue21436.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -