gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:00:27 26 4
gpt4 key购买 nike

我试图从不同的目录级别导入模块,所以我使用了:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
但现在我得到了这个错误。我正在使用 Spyder,并且第一次使用以下导入时,它工作正常。
from source.search.ci_search_project import CI_Search_Project
从第二次我得到这个错误。

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 runfileexecfile(filename, namespace)


File "C:\Users\nrshakya\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfileexec(compile(f.read(), filename, 'exec'), namespace)


File "C:/Users/nrshakya/Documents/CI/src/Comp_IntelPython/ci_search_project_test.py", line 8, in modulefrom 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'

最佳答案

According to Vera's answer to their question , 添加一个空的 __init__.py包含模块的文件夹中的文件将解决此问题。

关于python - 导入 python 模块时如何解决 KeyError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46439836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com