VansFannel
VansFannel

Reputation: 45941

Pylint doesn't recognize my Python imports on VS Code, but the code runs without any error

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?

Upvotes: 0

Views: 1211

Answers (2)

Steven-MSFT
Steven-MSFT

Reputation: 8431

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.

Upvotes: 1

utk09
utk09

Reputation: 13

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.

Upvotes: 0

Related Questions