简体   繁体   English

无法导入 conda 环境中安装的 package

[英]Can't import package that is installed in conda environment

There are similar previous questions, eg 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.之前有类似的问题,例如Can't import package after installed in a conda environmentModuleNotFoundError when importing package that installed in conda environment ,但我找不到适用于任何现有问题的解决方案。

I have a conda env called 'keras', and I installed various packages, including keras in it.我有一个名为“keras”的 conda env,我安装了各种软件包,其中包括 keras。 Eg if I try to install Keras again I get:例如,如果我再次尝试安装 Keras,我会得到:

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.但该模块实际上并不可用。 Eg:例如:

(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:我正在使用 conda 4.11.0,并且conda list -n keras在 output 中有这一行:

keras                     2.6.0            py39hd3eb1b0_0

Any tips on how to solve/diagnose/debug this?有关如何解决/诊断/调试此问题的任何提示?

You need to install Tensorflow and to change the way you import Keras -> tensorflow.keras.models .您需要安装 Tensorflow 并更改导入方式 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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM