导入python模块时如何解决KeyError? [英] How do I solve a KeyError when importing a python module?

查看:620
本文介绍了导入python模块时如何解决KeyError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从其他目录级别导入模块,所以我使用了:

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__), '..')))

但是现在我得到了这个错误.我正在使用Spyder,并且第一次使用以下导入,效果很好.

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

从第二次出现此错误.

文件"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 "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')

第880行,运行文件中的文件"C:\ Users \ nrshakya \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py" execfile(文件名,命名空间)

File "C:\Users\nrshakya\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile execfile(filename, namespace)

exec文件中第102行的文件"C:\ Users \ nrshakya \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py" exec(compile(f.read(),文件名,'exec'),命名空间)

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/Documents/CI/src/Comp_IntelPython/ci_search_project_test.py",模块中的第8行 从source.search.ci_search_project导入CI_Search_Project

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

文件_find_and_load中的冻结importlib._bootstrap"行961

File "frozen importlib._bootstrap", line 961, in _find_and_load

文件_find_and_load_unlocked中的文件"frozen importlib._bootstrap"(行946)

File "frozen importlib._bootstrap", line 946, in _find_and_load_unlocked

文件_find_spec中的行"frozen importlib._bootstrap"(行885)

File "frozen importlib._bootstrap", line 885, in _find_spec

find_spec中的文件冻结importlib._bootstrap_external",第1157行

File "frozen importlib._bootstrap_external", line 1157, in find_spec

文件_get_spec中的冻结importlib._bootstrap_external"行1123

File "frozen importlib._bootstrap_external", line 1123, in _get_spec

文件冻结的importlib._bootstrap_external",在 it

File "frozen importlib._bootstrap_external", line 994, in iter

文件_recalculate中的文件冻结importlib._bootstrap_external",第982行

File "frozen importlib._bootstrap_external", line 982, in _recalculate

文件冻结importlib._bootstrap_external",第978行,位于_get_parent_path中

File "frozen importlib._bootstrap_external", line 978, in _get_parent_path

KeyError:源"

KeyError: 'source'

推荐答案

According to Vera's answer to their question, adding an empty __init__.py file in the folder containing the module will fix this problem.

这篇关于导入python模块时如何解决KeyError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆