Python 导入 Tensorflow 2 时出错。凯拉斯

Python error in importing Tensorflow 2 . Keras

我有时确实写了一些深度学习代码ago.Now我想运行再写一遍。但起初它给了我这些错误。 这是出现问题的第一段代码:

import tensorflow.keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation,Flatten,MaxPooling2D,Conv2D
from tensorflow.keras.utils import to_categorical

这里是错误:

ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

这是回溯错误:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-177d4802053d>", line 1, in <module>
    import tensorflow.keras
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\msi-pc\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\__init__.py", line 64, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\core\framework\graph_pb2.py", line 7, in <module>
    from google.protobuf import descriptor as _descriptor
ImportError: cannot import name 'descriptor' from 'google.protobuf' (unknown location)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2045, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'ImportError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1170, in get_records
    return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 316, in wrapped
    return f(*args, **kwargs)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 350, in _fixed_getinnerframes
    records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 1502, in getinnerframes
    frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 1460, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 696, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 733, in getmodule
    if ismodule(module) and hasattr(module, '__file__'):
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\msi-pc\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\__init__.py", line 42, in <module>
    from . _api.v2 import audio
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\_api\v2\audio\__init__.py", line 10, in <module>
    from tensorflow.python.ops.gen_audio_ops import decode_wav
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\ops\gen_audio_ops.py", line 10, in <module>
    from tensorflow.python.eager import context as _context
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\eager\context.py", line 25, in <module>
    from absl import logging
ModuleNotFoundError: No module named 'absl'
Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-177d4802053d>", line 1, in <module>
    import tensorflow.keras
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\msi-pc\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\__init__.py", line 64, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\core\framework\graph_pb2.py", line 7, in <module>
    from google.protobuf import descriptor as _descriptor
ImportError: cannot import name 'descriptor' from 'google.protobuf' (unknown location)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2045, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'ImportError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3338, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3435, in run_code
    self.showtraceback(running_compiled_code=True)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2048, in showtraceback
    value, tb, tb_offset=tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1437, in structured_traceback
    self, etype, value, tb, tb_offset, number_of_lines_of_context)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1337, in structured_traceback
    self, etype, value, tb, tb_offset, number_of_lines_of_context
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1194, in structured_traceback
    tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1151, in format_exception_as_a_whole
    last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 451, in find_recursion
    return len(records), 0
TypeError: object of type 'NoneType' has no len()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2045, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'TypeError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1170, in get_records
    return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 316, in wrapped
    return f(*args, **kwargs)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 350, in _fixed_getinnerframes
    records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 1502, in getinnerframes
    frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 1460, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 696, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 733, in getmodule
    if ismodule(module) and hasattr(module, '__file__'):
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\msi-pc\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\__init__.py", line 42, in <module>
    from . _api.v2 import audio
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\_api\v2\audio\__init__.py", line 10, in <module>
    from tensorflow.python.ops.gen_audio_ops import decode_wav
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\ops\gen_audio_ops.py", line 10, in <module>
    from tensorflow.python.eager import context as _context
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\eager\context.py", line 25, in <module>
    from absl import logging
ModuleNotFoundError: No module named 'absl'
Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-177d4802053d>", line 1, in <module>
    import tensorflow.keras
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\msi-pc\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\__init__.py", line 64, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\core\framework\graph_pb2.py", line 7, in <module>
    from google.protobuf import descriptor as _descriptor
ImportError: cannot import name 'descriptor' from 'google.protobuf' (unknown location)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2045, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'ImportError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3338, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3435, in run_code
    self.showtraceback(running_compiled_code=True)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2048, in showtraceback
    value, tb, tb_offset=tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1437, in structured_traceback
    self, etype, value, tb, tb_offset, number_of_lines_of_context)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1337, in structured_traceback
    self, etype, value, tb, tb_offset, number_of_lines_of_context
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1194, in structured_traceback
    tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1151, in format_exception_as_a_whole
    last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 451, in find_recursion
    return len(records), 0
TypeError: object of type 'NoneType' has no len()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2045, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'TypeError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2923, in _run_cell
    return runner(coro)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3147, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3357, in run_ast_nodes
    self.showtraceback()
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2048, in showtraceback
    value, tb, tb_offset=tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1437, in structured_traceback
    self, etype, value, tb, tb_offset, number_of_lines_of_context)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1337, in structured_traceback
    self, etype, value, tb, tb_offset, number_of_lines_of_context
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1212, in structured_traceback
    chained_exceptions_tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1151, in format_exception_as_a_whole
    last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 451, in find_recursion
    return len(records), 0
TypeError: object of type 'NoneType' has no len()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 2045, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'TypeError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 1170, in get_records
    return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 316, in wrapped
    return f(*args, **kwargs)
  File "C:\Users\msi-pc\AppData\Roaming\Python\Python37\site-packages\IPython\core\ultratb.py", line 350, in _fixed_getinnerframes
    records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 1502, in getinnerframes
    frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 1460, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 696, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "C:\Users\msi-pc\Anaconda3\lib\inspect.py", line 733, in getmodule
    if ismodule(module) and hasattr(module, '__file__'):
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\msi-pc\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\__init__.py", line 42, in <module>
    from . _api.v2 import audio
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\_api\v2\audio\__init__.py", line 10, in <module>
    from tensorflow.python.ops.gen_audio_ops import decode_wav
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\ops\gen_audio_ops.py", line 10, in <module>
    from tensorflow.python.eager import context as _context
  File "C:\Users\msi-pc\Anaconda3\lib\site-packages\tensorflow_core\python\eager\context.py", line 25, in <module>
    from absl import logging
ModuleNotFoundError: No module named 'absl'

我很困惑为什么它不能 run.it 表示检查中的内部 Python 错误 module.it 对我来说是一个令人困惑的回溯。有人知道代码或其他问题是什么吗?

在错误的最后部分,我们可以看到ModuleNotFoundError: No module named 'absl

理论上,你应该能够通过安装 absl

来解决这个问题

尝试通过运行安装 pip install absl-py