在 spyder 中导入第二个库时出现 KeyError/frozen importlib._bootstrap 错误 [英] KeyError / frozen importlib._bootstrap error on second library import in spyder
问题描述
我收到一个
File " <frozen importlib._bootstrap_external> ", line 978, in _get_parent_path
KeyError: 'python_library'
当我在 spyder 中第二次从子文件夹导入库时出错,但第一次(重新启动 spyder 后)或在 spyder 之外它工作正常.
error when I import a library from a subfolder the second time in spyder, but the first time (after restarting spyder) or outside of spyder it works fine.
代码是:
from python_library.tools.test_lib import test_func
test_func()
其中 test_lib.py 很简单
where test_lib.py is simply
def test_func():
print('Hello!')
输出为:
runfile('/home/user/Desktop/test.py', wdir='/home/user/Desktop')
Hello!
runfile('/home/user/Desktop/test.py', wdir='/home/user/Desktop')
Reloaded modules: python_library, python_library.tools.test_lib
Traceback (most recent call last):
File "< ipython-input-2-e750fd08988c >", line 1, in <module>
runfile('/home/user/Desktop/test.py', wdir='/home/user/Desktop')
File "/home/user/anaconda3/envs/qutip/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 678, in runfile
execfile(filename, namespace)
File "/home/user/anaconda3/envs/qutip/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 106, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/user/Desktop/test.py", line 1, in <module>
from python_library.tools.test_lib import test_func
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 951, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 894, 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: 'python_library'
当库不在子文件夹中时不会发生错误,即
The error does not occur when the library is not in a subfolder i.e.
from python_library.test_lib2 import test_func
经常任意运行.但是我有足够的功能,没有子文件夹会很烦人.
runs arbitrarily often. However I have enough functions that not having subfolders would be very annoying.
这是在 spyder-3.3.2 中出现的,但在 spyder 版本 3.3.0-py36_1 之前也发生过.python版本是3.6.4.,spyder是通过anaconda安装和更新的,'python_library'是通过setup.py安装的(setuptools版本40.6.3,39.2.0-py36_0版本也有).
This was with spyder-3.3.2, but it also occurred earlier with spyder version 3.3.0-py36_1. The python version is 3.6.4., spyder is installed and updated via anaconda and the 'python_library' was installed via setup.py (setuptools version 40.6.3, also occurred with version 39.2.0-py36_0).
注意:同样的错误出现在问题中如何解决导入python时出现KeyError模块?但这个问题没有答案,也没有间谍标签.
Note: The same error occurred in question How do I solve a KeyError when importing a python module? but that question has no answer, and also no spyder tag.
推荐答案
解决办法是子文件夹tools里没有空的__init__.py
文件,只有超文件夹python_library.将文件 __init__.py
添加到工具中使其工作.
The solution was that there was no empty __init__.py
file in the sub-folder tools, only in the super-folder python_library. Adding a file __init__.py
into tools made it work.
这篇关于在 spyder 中导入第二个库时出现 KeyError/frozen importlib._bootstrap 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!