gpt4 book ai didi

KeyError / frozen importlib._bootstrap error on second library import in spyder(KeyError / frozen importlib._在spyder中导入第二个库时出现引导错误)

转载 作者:bug小助手 更新时间:2023-10-25 12:28:45 28 4
gpt4 key购买 nike



I receive a

我收到了一份



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


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.

在Spyder中第二次从子文件夹导入库时出错,但第一次(重新启动Spyder后)或在Spyder外导入时工作正常。



The code is:

代码是:



from python_library.tools.test_lib import test_func    
test_func()


where test_lib.py is simply

其中,test_lib.py只是



def test_func():    
print('Hello!')


And the output is:

输出结果为:



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.

经常随意奔跑。然而,我有足够的功能,没有子文件夹将是非常恼人的。



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).

这是在Spyder-3.3.2中出现的,但在Spyder版本3.3.0-py36_1中也出现了这种情况。版本为3.6.4。Spyder是通过anaconda安装和更新的,而‘python_库’是通过setup.py安装的(setupTools版本40.6.3,也出现在版本39.2.0-py36_0中)。



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.

注意:同样的错误也发生在如何解决导入Python模块时出现的KeyError的问题上?但这个问题没有答案,也没有世爵的标签。


更多回答
优秀答案推荐

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.

解决方案是,在子文件夹Tools中没有空的__init__.py文件,只有在超级文件夹python库中。将文件__init__.py添加到工具中可以使其正常工作。



command line:

命令行:


touch your_python_file_directory/__init__.py


When I got this error, it was because I had restructured my project and moved all the apps into a folder named 'apps'. I then changed the names of the apps in the my INSTALLED_APPS list to 'apps.***'.

当我收到这个错误时,是因为我重组了我的项目,并将所有的应用程序移到了一个名为‘app’的文件夹中。然后,我将MY INSTALLED_APPS列表中的应用程序名称更改为‘apps.*’。


I forgot, however, that I had context processors in the TEMPLATES setting whereby I needed to change the app names to 'apps.***' as well. So if anyone is in the same boat, just hit Ctrl + F and find all the other places you mention the app and make sure the full app name is correct.

然而,我忘记了模板设置中有上下文处理器,因此我还需要将应用程序名称更改为“apps.*”。因此,如果任何人都处于同样的境地,只需按Ctrl+F组合键,就可以找到你提到的所有其他地方,并确保应用程序的全名是正确的。



I was facing the same error then as suggested I have gone through other answers but it was not working.
Please check the error screenshot below.

我当时面临着同样的错误,就像建议我已经通过了其他答案,但它不起作用。请检查下面的错误截图。


Error:
enter image description here

错误:


Then I have started looking for this file and I have found that there were at 2 places

然后我开始寻找这个文件,我发现有两个地方


  __init__.py file was missing.

Check below both places in your system where python tool is installed if file is not there please take it from any lib subfolder and placed it.

检查系统中安装了Python工具的两个位置,如果文件不在那里,请从任何lib子文件夹中取出并放置它。


Library
enter image description here

图书馆


Lib
enter image description here

自由党



A solution that worked for me (with a similar error) was:

一个对我有效的解决方案(有类似的错误)是:


problem: KeyError "extra" when executing "import extra.good.best.sigma as sig"

问题:执行“IMPORT EXPORT.Good.Best.sigma as sig”时出现KeyError“Extra”


solution:

解决方案:



  1. run this:
    import extra as sig

  2. then this:
    import extra.good as sig

  3. then this:
    import extra.good.best as sig

  4. then this:
    import extra.good.best.sigma as sig


Finally the pycache folder contents seem to sort themselves out.

最后,pycache文件夹的内容似乎会自行理顺。


更多回答

Is there a way you broke it down based on the command line error? I would think these files have the old name for my app, but I don't know where they are. File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked

有没有办法根据命令行错误将其分解?我会认为这些文件与我的应用程序具有旧名称,但我不知道它们在哪里。文件“”,第994行,在_GCD_IMPORT文件“”中,第971行,在_Find_and_Load文件“”中,第941行,在_Find_and_Load_Unlock中

Just replicated the error and both the command line error and the debug page do not seem to give any useful information about where the error is coming from. The debug page does show which app is causing the error though in the 'Exception value' field, but beyond that, doesn't seem like there's any other way than to just Find in every single file.

只是复制了错误,命令行错误和调试页面似乎都没有提供关于错误来源的任何有用信息。调试页面确实会在“异常值”字段中显示哪个应用程序导致了错误,但除此之外,似乎没有其他方法,只能在每个文件中查找。

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