简体   繁体   English

使用 pip3 安装 Keras,但出现“No Module Named keras”错误

[英]Installed Keras with pip3, but getting the "No Module Named keras" error

I am Creating a leaf Identification Classifier using the CNN, the Keras and the Tensorflow backends on Windows.我正在使用 CNN、Keras 和 Windows 上的 Tensorflow 后端创建叶识别分类器。 I have installed Anaconda, Tensorflow, numpy, scipy and keras.我已经安装了 Anaconda、Tensorflow、numpy、scipy 和 keras。

I installed keras using pip3:我使用 pip3 安装了 keras:

C:\> pip3 list | grep -i keras
Keras               2.2.4
Keras-Applications  1.0.6
Keras-Preprocessing 1.0.5

However, when i run my project i get following error但是,当我运行我的项目时,出现以下错误

ModuleNotFoundError: No module named 'keras'

Why is the module not found, and how can I fix this error?为什么找不到模块,我该如何解决这个错误?

Installing Anaconda and then install packages with pip seams like confusing the goal of Anaconda(or any other package management tools)安装 Anaconda 然后使用 pip 接缝安装包,就像混淆了 Anaconda(或任何其他包管理工具)的目标

Anaconda is there to help you organize your environments and their dependences. Anaconda 可帮助您组织环境及其依赖项。

Assuming you have conda on your system path, Do:假设您的系统路径上有 conda,请执行以下操作:

Update conda更新畅达

conda update conda

We can create an environment called 'awesome' with python 3.6 and add all awesome datascience packages coming with anaconda(numpy, scipy, jupyter notebook/lab etc), and tensorflow and keras.我们可以使用 python 3.6 创建一个名为“awesome”的环境,并添加 anaconda(numpy、scipy、jupyter notebook/lab 等)以及 tensorflow 和 keras 附带的所有很棒的数据科学包。 you can drop anaconda and have minimal package if desired.如果需要,您可以放弃anaconda并使用最小的包装。

conda create -n awesome python=3.6 anaconda tensorflow keras

After quite a time, and all is well, activate your environment and test if we can import keras.一段时间后,一切都很好,激活您的环境并测试我们是否可以导入 keras。

conda activate awesome
python -c "import keras"

When done doing awesomeness, you can deactivate as so:完成令人敬畏的操作后,您可以像这样停用:

conda deactivate

conda is better than pip because it deal with libraries compatibalities. conda 比 pip 更好,因为它处理库兼容性。 It upgrades and downgrade packages for you.它为您升级和降级软件包。

Sometimes beautiful about Anaconda is that you can just install the main package and it will install all its dependences for you, so you could just do:有时 Anaconda 的美妙之处在于你可以只安装主包,它会为你安装它的所有依赖项,所以你可以这样做:

conda create -n awesome python=3.6 keras

This will automatically find all packages that keras depends on or set to default such as tensorflow and numpy这将自动找到 keras 依赖或设置为默认的所有包,例如 tensorflow 和 numpy

What you are doing wrong :你做错了什么
You get that error because your python sys.path can not locate the packages you install.您收到该错误是因为您的 python sys.path 无法找到您安装的软件包。

You can do:你可以做:

python -c "import sys;print(sys.path)"

This will print the location your python will look for packages.这将打印您的python 将查找包的位置。 It is most likely that the path to keras library is not one them. keras 库的路径很可能不是其中之一。

When you just use pip to install, your default python that has that pip will have access to your installations.当您仅使用 pip 进行安装时,具有该 pip 的默认 python 将可以访问您的安装。 So if you have multiple Pythons, the recommendation is to be explicit like:因此,如果您有多个 Python,建议是明确的,例如:

python3 -m pip install packages 

So here you are sure that it is Python in python3 directory that did installation.所以在这里你确定它是python3目录中的Python进行安装。 This is where we need environments that keeps our Python versions and dependence different and easy to control.这就是我们需要使我们的 Python 版本和依赖项不同且易于控制的环境。 Anaconda, Pipenv, Poetry, piptools and more are there trying to help you managed your systems better ;) Anaconda、Pipenv、Poetry、piptools 等都试图帮助您更好地管理系统;)

Update: For Jupyter Notebook/Lab users更新:对于 Jupyter Notebook/Lab 用户

If you already have Jupyter, say on your base environment, we can add awesome as another kernel:如果你已经有了 Jupyter,比如说在你的基础环境中,我们可以添加 awesome 作为另一个内核:

conda activate awesome 
(awesome ) conda install ipykernel -y
(awesome) python -m ipykernel install --user --name my_env --display-name "Awesome"
conda deactivate

Now if you run Jupyter, you should be able to choose between Base Python and Awesome environment.现在,如果您运行 Jupyter,您应该可以在 Base Python 和 Awesome 环境之间进行选择。

It sounds like you may have used pip3 to install while having multiple python installations on your machine.听起来您可能在机器上安装了多个 python 时使用了 pip3 进行安装。

Did you happen to have python installed on your machine before install Anaconda?在安装 Anaconda 之前,您是否碰巧在您的机器上安装了 python? Sometimes the pip3 in your PATH variable is for a different version than Anaconda.有时 PATH 变量中的 pip3 用于与 Anaconda 不同的版本。

Try this, and then run your code again:试试这个,然后再次运行你的代码:

conda install keras

Run conda list to see if it is installed in your Anaconda python installation:运行 conda list 以查看它是否安装在您的 Anaconda python 安装中:

conda list

Update更新

If it is still not working, then try this:如果它仍然无法正常工作,请尝试以下操作:

\path\to\Anaconda\python\python3 -m pip3 install keras

This uses pip3 but ensures that is selecting the correct installation when installing keras.这使用 pip3 但确保在安装 keras 时选择正确的安装。

It helped me to first check on the command line to see whether Keras was indeed installed as noted in a previous answer here.它帮助我首先检查命令行以查看是否确实安装了 Keras,如先前回答中所述。

python -c "import keras"

If you get an error with that command, you probably haven't installed keras in the right environment.如果该命令出现错误,则您可能没有在正确的环境中安装 keras。

After going through a number of solutions, I was still getting the error.在经历了许多解决方案之后,我仍然遇到错误。 Turns out I had to restart my jupyter notebook for the changes to take effect - just in case you're using notebook.事实证明,我必须重新启动我的 jupyter notebook 才能使更改生效——以防万一你正在使用 notebook。

Same happened to me.我也遇到了同样的情况。 I uninstalled keras then reinstalled a specific version, and it worked.我卸载了 keras,然后重新安装了一个特定的版本,它起作用了。

pip uninstall keras

Then this -->那么这个 -->

pip install keras==2.2.4

Make sure the instance of pip you're using is connected to the same instance of python you're using.确保您使用的 pip 实例连接到您使用的同一个 python 实例。 Since I have several python installations on my machine, I avoid confusion between them by directly using the pip module from the version of python I want, rather than using pip :由于我的机器上有几个 python 安装,我通过直接使用我想要的 python 版本中的 pip 模块来避免它们之间的混淆,而不是使用pip

python3 -m pip install keras
python3 -c "import keras" // <-- should execute without an error

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块未安装:pip3 install morphy - Module Not Getting Installed: pip3 install morphy 出现pip3错误:没有名为“ pip._vendor.pkg_resources”的模块 - Getting an error with pip3: No module named 'pip._vendor.pkg_resources' pip3 出现错误:ModuleNotFoundError: No module named 'pip._vendor.packaging.__about__' - Getting an error with pip3 : ModuleNotFoundError: No module named 'pip._vendor.packaging.__about__' 变压器中的“没有名为 keras 的模块”错误 - "No module named keras" error in transformers 我仍然收到错误ImportError:使用pip3安装请求后,没有模块命名为请求 - I am still getting the error ImportError: No module named requests after installing requests using pip3 如何修复模块未找到错误:当模块绝对安装时,没有名为&#39;keras&#39;的模块 - How to fix Module Not Found Error: No module named 'keras'when the module absolutly has been installed 导入pip3会导致“导入错误:没有名为&#39;pip3&#39;的模块 - Import pip3 results in "ImportError: No module named 'pip3' ModuleNotFoundError:没有名为 &#39;keras_preprocessing&#39; tensorflow2.2 的模块与 pip - ModuleNotFoundError: No module named 'keras_preprocessing' tensorflow2.2 with pip 错误:没有名为“tensorflow.keras”的模块 - Error: No module named 'tensorflow.keras' Keras给出错误“ ModuleNotFoundError:没有名为&#39;tensorflow&#39;的模块”,即使安装了tensorflow-gpu - Keras giving error “ModuleNotFoundError: No module named 'tensorflow'” even though tensorflow-gpu is installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM