简体   繁体   English

导入python模块时如何解决KeyError?

[英]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.

相关问题 如何解决此 Python KeyError? - How do I solve this Python KeyError? 如何解决导入python包“ No module named”的问题? - How to solve importing python packages “ No module named ”? 导入模块时,如何指定解释器来编译模块? - When importing a module, how do I specify an interpreter to compile the module? 展平 JSON 文件时出现 KeyError。 我该如何解决这个问题? - I am getting a KeyError when flattening my JSON file. How do I solve this? python-如何解决KeyError:2? - python-How to solve KeyError: 2? 在Python中导入RTree时,如何解决“ ImportError:没有名为索引的模块”? - How do I fix “ImportError: No module named index” when importing RTree in Python? 如何解决导入 Seaborn 时出现的错误? - How do i solve error in importing Seaborn? 导入模块时如何解决 Jupyter 中的“IndentationError: expected an indented block”? - How to solve “IndentationError: expected an indented block” in Jupyter when importing a module? 为什么在 python 中导入模块时出现“ModuleNotFoundError: No module named 'YEETER'”? - Why do I get “ModuleNotFoundError: No module named 'YEETER'” when importing a module in python? 如何解决/ Python / Pandas中的KeyError是什么? - How to solve/what is a KeyError in Python/Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM