[英]How do I solve a KeyError when importing a python module?
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.我正在使用 Spyder 并且第一次使用以下导入时,它工作正常。
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')文件“ipython-input-29-f35dfe634c32”,第 1 行,在模块 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)文件“C:\\Users\\nrshakya\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\spyder\\utils\\site\\sitecustomize.py”,第 880 行,运行文件 execfile(文件名,命名空间)
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)文件“C:\\Users\\nrshakya\\AppData\\Local\\Continuum\\Anaconda3\\lib\\site-packages\\spyder\\utils\\site\\sitecustomize.py”,第 102 行,在 execfile exec(compile(f.read(), filename) , 'exec'), 命名空间)
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文件“C:/Users/nrshakya/Documents/CI/src/Comp_IntelPython/ci_search_project_test.py”,第 8 行,来自 source.search.ci_search_project import CI_Search_Project 的模块
File "frozen importlib._bootstrap", line 961, in _find_and_load _find_and_load 中的文件“frozen importlib._bootstrap”,第 961 行
File "frozen importlib._bootstrap", line 946, in _find_and_load_unlocked _find_and_load_unlocked 中的文件“frozen importlib._bootstrap”,第 946 行
File "frozen importlib._bootstrap", line 885, in _find_spec _find_spec 中的文件“frozen importlib._bootstrap”,第 885 行
File "frozen importlib._bootstrap_external", line 1157, in find_spec find_spec 中的文件“frozen importlib._bootstrap_external”,第 1157 行
File "frozen importlib._bootstrap_external", line 1123, in _get_spec _get_spec 中的文件“frozen importlib._bootstrap_external”,第 1123 行
File "frozen importlib._bootstrap_external", line 994, in iter文件“frozen importlib._bootstrap_external”,第 994 行,在迭代器中
File "frozen importlib._bootstrap_external", line 982, in _recalculate _recalculate 中的文件“frozen importlib._bootstrap_external”,第 982 行
File "frozen importlib._bootstrap_external", line 978, in _get_parent_path _get_parent_path 中的文件“frozen importlib._bootstrap_external”,第 978 行
KeyError: 'source'关键错误:'来源'
根据 Vera 对他们问题的回答,在包含模块的文件夹中添加一个空的__init__.py
文件将解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.