mirror of https://github.com/m-labs/artiq.git
tools/file_import: support import of modules importing from their directory
This commit is contained in:
parent
656099a331
commit
170a626c76
|
@ -3,6 +3,7 @@ import sys
|
||||||
import importlib.machinery
|
import importlib.machinery
|
||||||
import linecache
|
import linecache
|
||||||
import logging
|
import logging
|
||||||
|
import os.path
|
||||||
|
|
||||||
|
|
||||||
def format_run_arguments(arguments):
|
def format_run_arguments(arguments):
|
||||||
|
@ -27,9 +28,15 @@ def file_import(filename):
|
||||||
modname = modname[:i]
|
modname = modname[:i]
|
||||||
modname = "file_import_" + modname
|
modname = "file_import_" + modname
|
||||||
|
|
||||||
|
path = os.path.dirname(os.path.realpath(filename))
|
||||||
|
sys.path.insert(0, path)
|
||||||
|
|
||||||
loader = importlib.machinery.SourceFileLoader(modname, filename)
|
loader = importlib.machinery.SourceFileLoader(modname, filename)
|
||||||
module = type(sys)(modname)
|
module = type(sys)(modname)
|
||||||
loader.exec_module(module)
|
loader.exec_module(module)
|
||||||
|
|
||||||
|
sys.path.remove(path)
|
||||||
|
|
||||||
return module
|
return module
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue