0

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?

user3193317
  • 131
  • 3
  • 14
  • Could you please provide the output of "conda env list" command? – Lakshmi - Intel Dec 24 '19 at 15:05
  • @Lakshmi-Intel I added the output in my post. – user3193317 Dec 24 '19 at 15:33
  • In control panel environment variables add Path as C:\Users\xx\Anaconda3.Then take a new command prompt and check whether you are able to import tensorflow after activating conda environment – Lakshmi - Intel Dec 24 '19 at 15:47
  • Please show the shebang in your `test.py` (i.e., the first line with `#! C:\...`). I'm guessing you have a shebang that points back to another Python interpreter. Related: https://stackoverflow.com/a/58455069/570918 – merv Dec 24 '19 at 16:03

1 Answers1

1

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

merv
  • 67,214
  • 13
  • 180
  • 245
Jason
  • 298
  • 2
  • 11