问题
I was trying to import a module from a different directory level so I used:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
But now I get this error. I'm using Spyder and the first time I use the following import, it works fine.
from source.search.ci_search_project import CI_Search_Project
From the second time I get this error.
File "ipython-input-29-f35dfe634c32", line 1, in module runfile('C:/Users/nrshakya/Documents/CI/src/Comp_IntelPython/ci_search_project_test.py', wdir='C:/Users/nrshakya/Documents/CI/src/Comp_IntelPython')
File "C:\Users\nrshakya\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile execfile(filename, namespace)
File "C:\Users\nrshakya\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/nrshakya/Documents/CI/src/Comp_IntelPython/ci_search_project_test.py", line 8, in module from source.search.ci_search_project import CI_Search_Project
File "frozen importlib._bootstrap", line 961, in _find_and_load
File "frozen importlib._bootstrap", line 946, in _find_and_load_unlocked
File "frozen importlib._bootstrap", line 885, in _find_spec
File "frozen importlib._bootstrap_external", line 1157, in find_spec
File "frozen importlib._bootstrap_external", line 1123, in _get_spec
File "frozen importlib._bootstrap_external", line 994, in iter
File "frozen importlib._bootstrap_external", line 982, in _recalculate
File "frozen importlib._bootstrap_external", line 978, in _get_parent_path
KeyError: 'source'
来源:https://stackoverflow.com/questions/46439836/how-do-i-solve-a-keyerror-when-importing-a-python-module