3

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?

2
  • 1
    This might be issue because having different python interpreter in your system Commented Apr 1, 2018 at 18:39
  • Your code work fine in my system. Please make sure that correct python interpreter enable with VS code. Commented Apr 1, 2018 at 18:46

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

Pylint was not installed for VS to work with. I found this by searching (in VS) for lint, inspired by Haomin above. A messagebox appeared and the first suggestion was to install pylint. "pip install pylint". I clicked this and it all worked! This has taken ages to find.

Comments

-2

Use tf not tensorflow. You have imported tensorflow as tf, not tensorflow.

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 tf.python.data import Dataset 

3 Comments

Does it work on yours @DinushaDilanka. If not, then I got it wrong.
You can not import like this from tf.python.data import Dataset . Completely wrong way you did.
Fair enough. I'll leave the ansewr here as an example of what not to do.

Your Answer

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.