简体   繁体   中英

Keras is installed in Anaconda virtual environment but can't be imported

I have keras installed into my Anaconda virtual environment but I can't import the module:

$ conda list | grep keras
keras                     2.2.4                    py37_1    conda-forge
keras-applications        1.0.8                      py_0  
keras-preprocessing       1.1.0                      py_1  
keras-retinanet           0.5.0                    pypi_0    pypi

$ python
Python 3.7.3 | packaged by conda-forge | (default, Mar 27 2019, 23:01:00) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'keras'

What might be amiss?

The best option is to use venv for your virtual environment. For instructions:

Mac OS & Linux: How To Setup Virtual Environment

1) After cloning the repo, cd into the repo and run the command: python3 -m venv venv

    This will create the virtual environment. Make sure to name it venv because the .gitignore file
    has been initialized to ignore it by default. 

2) Activate the virtual environment by running the following command: source venv/bin/activate

3) To download the required packages simply run: pip install -r requirements.txt

    This will go to the requirements file that is already generated in the repo and download any dependecies.

4) After adding any packages, make sure to update the requirements.txt file by running: pip freeze > requirements.txt

5) At any moment, you can run deactivate to leave the virtual environment.

Windows: How To Setup Virtual Environment

1) After cloning the repo, cd into the repo and run the command: python -m venv venv

    This will create the virtual environment. Make sure to name it venv because the .gitignore file
    has been initialized to ignore it by default. 

2) Activate the virtual environment by running the following command: venv\\Scripts\\activate.bat

    To activate the virtual environment inside of a code editor's bash, run: venv\Scripts\activate.ps1

3) To download the required packages simply run: pip install -r requirements.txt

4) After adding any packages, make sure to update the requirements.txt file by running: pip freeze and then pasting them into the requirements.txt file.

5) At any moment, you can run deactivate to leave the virtual environment.

Finally, simply type pip install keras

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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