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?

asked Dec 24, 2019 at 14:31
4
  • Could you please provide the output of "conda env list" command? Commented Dec 24, 2019 at 15:05
  • @Lakshmi-Intel I added the output in my post. Commented Dec 24, 2019 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 Commented Dec 24, 2019 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: stackoverflow.com/a/58455069/570918 Commented Dec 24, 2019 at 16:03

1 Answer 1

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
78.3k17 gold badges218 silver badges291 bronze badges
answered Dec 24, 2019 at 15:48
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.