31

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.

Vera
  • 769
  • 1
  • 6
  • 16

5 Answers5

28

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.

Vera
  • 769
  • 1
  • 6
  • 16
3

command line:

touch your_python_file_directory/__init__.py
alper
  • 2,919
  • 9
  • 53
  • 102
陳子軼
  • 187
  • 1
  • 5
1

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.

Tommy Wolfheart
  • 440
  • 4
  • 16
  • 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 "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 941, in _find_and_load_unlocked – Nick Nov 06 '21 at 17:33
  • 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. – Tommy Wolfheart Nov 07 '21 at 18:47
1

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ajeet Verma
  • 1,021
  • 1
  • 7
  • 25
0

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

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

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.