beeprogrammer
beeprogrammer

Reputation: 649

import tensorflow works in jupyter notebook but not from command line. Using conda to install tensorflow and jupyter notebook

I am running a python script in Jupyter notebook and it works fine. I converted the notebook to python file and when I try to run it from the terminal I get an error saying

>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow'

I have installed tensorflow and jupyter notebook using conda in a conda environment. I am on Ubuntu 18.04. I am trying to run the python script from within the environment. When I give which python I get the following output

/usr/bin/python

My limited understanding is that this is probably happening because the script is running on the base python whereas tensorflow is pointing to anaconda installation which base python cannot access. How can I run the script from the terminal?

Upvotes: 5

Views: 1035

Answers (1)

S.Au.Ra.B.H
S.Au.Ra.B.H

Reputation: 457

It looks like the script on the terminal is getting executed with a python version that is different from the one which executes in Jupyter Notebook. Since conda is being used, the issue may get resolved with following approaches:

  1. If a virtual environment is being used to run Jupyter Notebook, you can activate the virtual environment with conda activate <name> on the terminal and then run the python code.
[usr@usr]#conda activate myenv
(myenv)[usr@usr]#
  1. In case a virtual environment is not being used, the default conda python can be loaded to the PATH variable with conda activate
[usr@usr]#conda activate
(base)[usr@usr]#

In both steps 1 and 2, we can verify if tensorflow is installed in that environment with conda list command.

Alternatively, we can check what python the Jupyter Notebook uses and run the script pointing to same python in terminal.

Hope this helps.

Upvotes: 1

Related Questions