Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyError / frozen importlib._bootstrap error on second library import in spyder

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.

The code is:

from python_library.tools.test_lib import test_func    
test_func()

where test_lib.py is simply

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

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.

like image 438
Vera Avatar asked Dec 19 '18 16:12

Vera


4 Answers

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.

like image 85
Vera Avatar answered Sep 21 '22 21:09

Vera


command line:

touch your_python_file_directory/__init__.py
like image 34
陳子軼 Avatar answered Sep 23 '22 21:09

陳子軼


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.***'.

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.

like image 41
Tommy Wolfheart Avatar answered Sep 20 '22 21:09

Tommy Wolfheart


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.

Library enter image description here

Lib enter image description here

like image 1
Ajeet Verma Avatar answered Sep 22 '22 21:09

Ajeet Verma