简体   繁体   中英

Anaconda prompt: ModuleNotFoundError when running Python script

I am trying to run the following script "test.py" on Anaconda Prompt:

from tensorflow.keras.applications.resnet50 import ResNet50

...with the following command:

(conda_env) C:\dev>test.py

This results in the following error:

ModulNotFoundError: No module named 'tensorflow'

When I run the same script on Anaconda Prompt with the following command I don't get any errors:

(conda_env) C:\dev>python test.py

I have installed tensorflow in the Anaconda environment 'conda_env'

(conda_env) C:\dev\>conda env list
# conda environments:
#
base                    C:\Users\xx\Anaconda3
conda_env            *  C:\Users\xx\Anaconda3\envs\conda_env
keras_1                 C:\Users\xx\Anaconda3\envs\keras_1
tf-gpu                  C:\Users\xx\Anaconda3\envs\tf-gpu

Why is this like that?

You won't get errors if you do

(conda_env) C:\dev> python test.py

because then you're following the correct syntax for running python scripts in a terminal. By adding python before the .py file, you initiate the Python interpreter that executes your script. Without it, the terminal won't know what Python interpreter to use to execute your script and may end up using an interpreter that doesn't have the modules you require. There are ways to skip writing python before executing if that's what you want.

For example, see: Calling a python script from command line without typing "python" first

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