我收到一个

File " <frozen importlib._bootstrap_external> ", line 978, in _get_parent_path
KeyError: 'python_library'

第二次在spyder中从子文件夹导入库时出错,但第一次(重新启动spyder后)或在spyder之外导入库时,它工作正常。
代码是:
from python_library.tools.test_lib import test_func
test_func()

其中test-lib.py只是
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'

当库不在子文件夹(即。
from python_library.test_lib2 import test_func

经常任意奔跑。不过,我有足够的功能,没有子文件夹将是非常恼人的。
这是在spyder-3.3.2中发生的,但在spyder版本3.3.0-py36{1中也发生过。python版本是3.6.4.,spyder是通过anaconda安装和更新的,python_库是通过setup.py安装的(setuptools版本40.6.3,也出现在版本39.2.0-py36_0中)。
注意:同样的错误出现在问题中
How do I solve a KeyError when importing a python module?
但这个问题没有答案,也没有间谍标签。

最佳答案

解决方案是在子文件夹工具中没有空的__init__.py文件,只有在超级文件夹python_库中。在工具中添加一个文件__init__.py使其工作。

关于python - 在spyder中第二次库导入时出现KeyError/卡住的importlib._bootstrap错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53855836/

10-14 10:03