From b48e782dd67282127a6bab96de5166a143e57f29 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 1 May 2018 22:15:19 +0800 Subject: [PATCH] tools/file_import: restore sys.modules. Closes #976 --- artiq/tools.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/artiq/tools.py b/artiq/tools.py index f7a399db7..679293143 100644 --- a/artiq/tools.py +++ b/artiq/tools.py @@ -6,6 +6,7 @@ import collections import atexit import string import os +from copy import copy import numpy as np @@ -84,8 +85,12 @@ def file_import(filename, prefix="file_import_"): path = os.path.dirname(os.path.realpath(filename)) sys.path.insert(0, path) try: - loader = importlib.machinery.SourceFileLoader(modname, filename) - module = loader.load_module() + previous_modules = copy(sys.modules) + try: + loader = importlib.machinery.SourceFileLoader(modname, filename) + module = loader.load_module() + finally: + sys.modules = previous_modules finally: sys.path.remove(path)