Ach113
Ach113

Reputation: 1825

ModuleNotFoundError: No module named 'tensorflow.python' Anaconda

I have been using Tensorflow on Anaconda for a while now, but recently I have been getting the mentioned error when trying to import Tensorflow.

This has been asked here multiple times, so I tried suggested solutions but nothing worked so far (reinstalling tensorflow (both normal and gpu versions), reinstalling Anaconda). When running help('modules') tensorflow appears in module list. But even after I run pip uninstall tensorflow and pip uninstall tensorflow-gpu tensorflow still remains in module list when running help('modules').

What can I do to fix this?

Upvotes: 0

Views: 1907

Answers (4)

Fofo Serhan
Fofo Serhan

Reputation: 9

here is what helped me personally:

  1. open terminal with elevated priviliges: in windows you can right-click on the windows symbol, open " Windows Powershell (Admin) "

  2. enable long directory/path commands using command: New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force

  3. open your project/environment and open terminal/shell:

#if you already have tensorflow installed uninstall it using:

" pip uninstall tensorflow  " or " conda remove tensorflow   "

#####update pip

pip install --upgrade pip

#####in a virtual environment or using Python 2

pip install tensorflow

#for python 3 (could also be pip3.10 depending on your version)

pip3 install tensorflow

#####if you get permissions error

sudo pip3 install tensorflow

pip install tensorflow --user

#####if you don't have pip in your PATH environment variable

python -m pip install tensorflow

#####for python 3 (could also be pip3.10 depending on your version)

python3 -m pip install tensorflow

#####using py alias (Windows)

py -m pip install tensorflow

#####for Anaconda

conda install -c conda-forge tensorflow

#####for Jupyter Notebook

!pip install tensorflow

source:

https://bobbyhadz.com/blog/python-no-module-named-tensorflow

Upvotes: -1

Vlad Bezden
Vlad Bezden

Reputation: 89577

As one of the solutions always would to manually delete TensorFlow package and then reinstall it.

sudo rm -rf /usr/local/pip/python3.6/dist-packages/tensorflow

Upvotes: 0

Ach113
Ach113

Reputation: 1825

Apparently the reason was the Python version (which is strange as according to documentation Tensorflow supports Python 3.7). I downgraded to 3.6 and I am able to import Tensorflow again

Upvotes: 1

Sushanth
Sushanth

Reputation: 2342

Even when you uninstall tensorflow using pip, with help('modules') command, if tensorflow is still there, this might be because the packages are not accessed by python from the path "/usr/local/pip/python3.6" but rather from the path "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages".

So to uninstall tensorflow from here, try:

conda remove tensorflow

or:

conda remove -n your_env_name tensorflow

Once you do that install tensorflow using:

 conda install -c conda-forge tensorflow 

Hopefully this will work.

Even if this does not work, then use rm to delete the tensorlflow package folder.

To do this do not assume that it will be present in the "/usr/local/pip/python3.6" path. But instead type:

pip show tensorflow

This will give the path from which the tensorflow module is being imported by python.

Then run:

rm -rf path-you-got-from-pip-show-tensorflow

Upvotes: 0

Related Questions