Reputation: 250
I have used Keras remotely, such as on Colab and AWS. Today, I tried to install Keras on my laptop using pip3 install keras, and the message was that keras was already installed on the computer (I must have installed it before):
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Requirement already satisfied: keras in /usr/local/lib/python3.7/site-packages/Keras-2.4.3-py3.7.egg (2.4.3)
Requirement already satisfied: h5py in /usr/local/lib/python3.7/site-packages/h5py-2.10.0-py3.7-macosx-10.15-x86_64.egg (from keras) (2.10.0)
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.7/site-packages (from keras) (1.16.4)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/site-packages/PyYAML-5.3.1-py3.7-macosx-10.15-x86_64.egg (from keras) (5.3.1)
Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python3.7/site-packages (from keras) (1.2.0)
Requirement already satisfied: six in /usr/local/lib/python3.7/site-packages (from h5py->keras) (1.12.0)
However, when I tried to import keras
, I received this message:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-21-88d96843a926>", line 1, in <module>
import keras
ModuleNotFoundError: No module named 'keras'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2018, in showtraceback
stb = value._render_traceback_()
AttributeError: 'ModuleNotFoundError' object has no attribute '_render_traceback_'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/IPython/core/ultratb.py", line 1095, in get_records
return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
File "/usr/local/lib/python3.7/site-packages/IPython/core/ultratb.py", line 313, in wrapped
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/IPython/core/ultratb.py", line 347, in _fixed_getinnerframes
records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 1502, in getinnerframes
frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 1460, in getframeinfo
filename = getsourcefile(frame) or getfile(frame)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 696, in getsourcefile
if getattr(getmodule(object, filename), '__loader__', None) is not None:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 733, in getmodule
if ismodule(module) and hasattr(module, '__file__'):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/__init__.py", line 50, in __getattr__
module = self._load()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow/__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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 "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/__init__.py", line 32, in <module>
from tensorflow._api.v1 import app
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/_api/v1/app/__init__.py", line 10, in <module>
from tensorflow.python.platform.app import run
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/platform/app.py", line 23, in <module>
from absl.app import run as _run
ModuleNotFoundError: No module named 'absl'
I Googled to find similar problems and I found some suggestions to try sudo pip3 install keras
. Keras was downloaded again. However, when I typed import keras
, it still produced errors.
Also, I read this Stack Overflow question: How to fix import keras error with python console, but the solution does not work for me as I am calling a library which calls import keras
, so I need the import keras
command to work (I can't make it call import tensorflow.keras
).
Why is this error happening?
Note: I am using Python 3.7.5. I checked the version of pip through pip --version
, getting this message:
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
Also, I am running this code in Jupyter Notebook. I have checked that Python 3.7 is being run by Jupyter Notebook.
Upvotes: 0
Views: 1337
Reputation: 11
Try installing tensorflow first:
pip install tensorflow
Then access keras via tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import tensorflow as tf
Upvotes: 1
Reputation: 710
Make sure you're running your script with python3
. Macs and some other computers come with Python 2.7 installed as the default for python
, which will bamboozle you daily.
If that doesn't work, set up a virtual environment: Using Python 3 in virtualenv
Upvotes: 0