0

I'm working on Windows 7 64bits with Anaconda 3. On my environment Nifti, I have installed Tensorflow 2.1.0, Keras 2.3.1 and Python 3.7.7.

On Visual Studio Code there is a problem with all of these imports:

from tensorflow.python.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Conv2D, Conv2DTranspose, UpSampling2D, MaxPooling2D, Flatten, ZeroPadding2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam

I get these errors:

No name 'python' in module 'tensorflow'
Unable to import 'tensorflow.python.keras.models'
Unable to import 'tensorflow.keras.layers'
Unable to import 'tensorflow.keras.preprocessing.image'
Unable to import 'tensorflow.keras.optimizers'

Visual Studio Code is using the same anaconda environment: D:\Users\VansFannel\Programs\anaconda3\envs\nifti. I have checked it on "Python: Select Interpreter command" option in Visual Studio.

If I do this on a CMD shell with nifti environment activate, python -c 'from tensorflow.python.keras.models import Model, I don't get any error.

If I do with iPython:

from tensorflow.python.keras.models import Model

I don't get any error either.

I have checked python.pythonpath settings, and it points to: D:\Users\VansFannel\Programs\anaconda3\envs\nifti

And in the bottom left corner I can see:

enter image description here

When I open a new Terminal on Visual Studio Code, I get these messages:

Microsoft Windows [Versión 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Reservados todos los derechos.

D:\Sources\Repos\University\TFM\PruebasPython\Nifty>D:/Usuarios/VansFannel/Programs/anaconda3/Scripts/activate

(base) D:\Sources\Repos\University\TFM\PruebasPython\Nifty>conda activate nifti

(nifti) D:\Sources\Repos\University\TFM\PruebasPython\Nifty>

If I run the code in Visual Studio Code with Ctrl. + F5, it runs without any errors although it displays the errors on the Problems tab.

With pyCharm, I don't get any errors.

How an I fix this problem?

VansFannel
  • 45,055
  • 107
  • 359
  • 626

2 Answers2

1

I had tried your code, and my suggestion is to switch from pylint to other lintings. Keep away from that stupid linting. Maybe you can take a try of flake8: "python.linting.pylintEnabled": false, "python.linting.flake8Enabled": true,

This problem occurs because the pylint can't search the path of Anaconda, as 'tensorflow' only can be installed through conda, you only can choose the environment which created by Anaconda. But pylint can't follow the change of the environment to change the search path, so it will prompt import error. Stupid linting.

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
0

If you're using Anaconda Virtual Environment

  1. Close all Open Terminals in VS Code
  2. Open a new terminal
  3. Write the path to your activate folder of Anaconda in Terminal

    Example: E:/Softwares/AnacondaFolder/Scripts/activate

This should now show (base) written at the start of your folder path

  1. Now, conda activate

    Example: conda activate Nifti

This should now show (Nifti) written at the start of your folder path

Now, if your import something, VS Code will recognize it.

utk09
  • 13
  • 4
  • Thanks for your answer, but it doesn't work. I have updated my question with more information and with what I get when I open a new Terminal on VS Code. – VansFannel Jun 03 '20 at 15:02
  • You can try to disable Pylint: Go to File >> Preferences >> Settings Search **python.linting.pylintEnable** and set it to **false** & Now restart VS Code – utk09 Jun 03 '20 at 15:08
  • If I disable Pylint the errors disappear. But this is a very drastic way of solving the problem. Thanks. – VansFannel Jun 03 '20 at 15:22
  • @VansFannel The issue is with Pylint, so you either disable the specific import warning on each line, disable the specific warning globally via Pylint settings, or disable Pylint itself. – Brett Cannon Jun 04 '20 at 20:57