Reputation: 2616
There are similar previous questions, e.g. Can't import package after installing in a conda environment or ModuleNotFoundError when importing package that is installed in conda environment, but I couldn't find a solution that worked in any of the existing questions.
I have a conda env called 'keras', and I installed various packages, including keras in it. E.g. if I try to install Keras again I get:
C:\Users\Ori Family>conda activate keras
(keras) C:\Users\Ori Family>conda install -c conda-forge keras
Collecting package metadata (current_repodata.json): done
Solving environment: done
# All requested packages already installed.
But the module is not actually available. E.g.:
(keras) C:\Users\Ori Family>python
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from keras.models import Sequential
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'keras'
Everything seems to be in order:
>>> import platform
>>> print(platform.architecture())
('64bit', 'WindowsPE')
>>> import sys
>>> for p in sys.path:
... print(p)
...
C:\tools\anaconda3\envs\keras\python39.zip
C:\tools\anaconda3\envs\keras\DLLs
C:\tools\anaconda3\envs\keras\lib
C:\tools\anaconda3\envs\keras
C:\Users\Ori Family\AppData\Roaming\Python\Python39\site-packages
C:\tools\anaconda3\envs\keras\lib\site-packages
>>> print(sys.executable)
C:\tools\anaconda3\envs\keras\python.exe
I'm using conda 4.11.0, and conda list -n keras
has this line in the output:
keras 2.6.0 py39hd3eb1b0_0
Any tips on how to solve/diagnose/debug this?
Upvotes: 2
Views: 1535
Reputation: 21958
You need to install Tensorflow and to change the way you import Keras -> tensorflow.keras.models
.
# create a fresh env for keras
conda create -c conda-forge -n keras python=3.7 keras tensorflow
# use the env
conda activate keras
Then you can import it.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
print(f"{keras.__version__}")
# 2.6.0
Upvotes: 2