Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python not finding Tensorflow module under Anaconda

I'm a newbie trying to execute the code in first_steps_with_tensor_flow.ipynb locally on Windows 10. I have installed Anaconda Navigator 1.8.2, created an environment where I've installed, among others, tensorflow package. I then launch Visual Studio Code from Anaconda and run

import math
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Dataset

The last line is marked with red in VS Code, saying E0401:Unable to import 'tensorflow.python.data'. Running, I get ModuleNotFoundError: No module named 'tensorflow'. However, if I change the last line to print (tf.__version__) I do get 1.2.1 as output, so obviously Tensorflow is installed. Oddly VS Code complains E1101:Module 'tensorflow' has no __version__ member, but that line works.

What am I doing wrong here?

like image 613
Gruber Avatar asked Jun 16 '26 17:06

Gruber


2 Answers

are you using Visual Studio Code (VSC) or just pylint in general? I found out the reason why this is happening.

For VSC, the python extension, use pylint for intellisense of python. Pylint seems to have a bug with the sub-module. For me, the error only shows in VSC and not in prompt.

I solved this problem by doing the following steps:

Click "Code" -> Click "Preferences" -> Click "Settings"

Now in the settings, you have a search bar on top, search:

python.linting.pylintEnable and set it to false

Now there is alternative for linting, I am using the pep8 as a example here since it come with the anaconda, search this

python.linting.pep8Enabled and set it to true

Now pylint is not the default linter anymore, we are now using the pep8. Just to make sure, quit VSC and reopen it. there should not be any error anymore.

I am fairly sure this is a problem with pylint, instead of the TF you installed. By default, the Microsoft python extension in the VSC is using pylint as the linting tool. By changing it to pep8 or other we can avoid the error.

like image 160
Haomin Avatar answered Jun 19 '26 06:06

Haomin


Tensorflow is on version 1.7.0 currently and you have version 1.2.1 installed. Having reviewed the tensorflow repository, the tensorflow.python.data was first used in 1.4.0-rc0, before that it was in contrib.

I'd suggest to upgrade to the current version and retry. Alternatively look up the correct path in version 1.2.1.

like image 38
Peter Szoldan Avatar answered Jun 19 '26 06:06

Peter Szoldan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!