2

I've been practicing with matplotlib, then after a short break I opened the codes again and to my surprise, got this error of missing member, which I wasn't getting before.

Module 'matplotlib.cm' has no 'Blues' memberpylint(no-member)

The thing is:

  1. This supposedly missing member is is there, as it shows in the terminal:
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.    
>>> import matplotlib.pyplot as plt
>>> plt.cm.Blues
<matplotlib.colors.LinearSegmentedColormap object at 0x000002D00AF42B20>
>>>

It is there. Not missing at all;

  1. plt stands for matplotlib.pyplot, not just matplotlib, as the error suggests.

Here is the script:

import matplotlib.pyplot as plt
from random_walk_mod import RandomWalkMod

print("This program displays a random walk")
# Keep making new walks, as long s the program is active
while True:
    # Make a random walk
    rw = RandomWalkMod()
    rw.fill_walk()

    # Plot the points in the walk
    plt.style.use('classic')
    fig, ax = plt.subplots(figsize=(15,9), dpi=128)
    point_numbers = range(rw.num_points)
    ax.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues,
        edgecolors='none', s=1)

    # Emphasize the first and last points
    ax.scatter(0,0,c='green', edgecolors='none', s=100)
    ax.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100)
    
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    plt.show()

    keep_running = input("Make another walk? (y/n): ")
    if keep_running == 'n':
        break

I have found a solution at this forum (https://www.zhihu.com/question/433655496), which is just pass in the color instead of the path, as in:

cmap='Blues'

Instead of:

cmap=plt.cm.Blues

And it works. However, it doesn't seem to wrap it up for me, because I really feel there has to be a workaround to writing the path ("plt.cm.Blues", which a few weeks ago was working fine), because:

  1. plt refers to matplotlib.pylint, NOT just matplotlib. Why is this error pointing at plt (i.e. matplotlib.pyplot) and reporting a missing member from matplotlib.cm (i.e. ignoring pyplot)?
  2. "cm" and its many colors are obviously there. A simple dir search proves it. Actually, both matplotlib and pyplot have an attribute called "cm", which are even the same object:
>>> dir(plt.cm) == dir(matplotlib.cm)
True

So, there are two possible paths for calling "Blues", but VSCode doesn't seem to reach them out. The objects are there and the paths are corectly written, so what is going on? Not even importing the whole matplotlib solves the problem. Also, I couldn't find anything related in matplotlib's docs. I hope I'm just very bad at reading docs :)

Any help is much appreciated. Thank you!

[UPDATE: solved] I had two possible solutions for this, which would be either:

  1. setting up pylint in settings.json so it would ignore the no-member error, or
  2. update Python, which I just had to do.

So I chose the latter, but then VSCode wouldn't recognize the update. No matter what I did (update Python, update VSCode, uninstall and reinstall everything...), VSCode would still not show the updated python interpreter(3.9.5), but instead the old version I had(3.9.1). I set the path correctly, but to VSCode it was like I'd never updated Python.
So after some thorough research, I tried uninstalling VSCode but this time clearing its cache by deleting the folder "Code" (Win+R, then type "%appdata", hit Enter). Finally, VSCode reads the python app as expected, and, even better, the linting problem is gone.

101is5
  • 309
  • 2
  • 12

2 Answers2

1

Is it possible that VSCode is using a different python interpreter than your system python that you're using in the terminal? My guess is the VSCode python has a different version of matplotlib that would account for the inconsistency across your two scripts. Your script worked just fine for me on python 3.9.5, with matplotlib 3.4.2

sample plot

falafelocelot
  • 548
  • 3
  • 12
  • Thanks! But now VSCode won't recognize the 3.9.5 version. I have already updated, but in the blue bar at the bottom left it stil says "Python 3.9.1 64-bit" – 101is5 Jun 07 '21 at 18:19
  • What version of matplotlib are you using? – falafelocelot Jun 07 '21 at 18:32
  • Just can't find anything about the Python version issue. My current version is obviously 3.9.5 (cmd prompt says so, file properties say so, the installer said so...) but to VSCode, it's 3.6.1 (at least it's waht it says in the blue bar at the bottom). Paths are the same, all I did was update. The file "python.exe" in the folder is now the version 3.9.5 of Python, but... – 101is5 Jun 07 '21 at 18:47
  • Sorry, I meant 3.4.2 (version of matplotlib). @falafelocelot – 101is5 Jun 07 '21 at 19:05
  • By the way, I managed to make VSCode find an updated interpreter by creating a virtual environment, which apparently creates a dedicated copy of the Python app inside the venv folder. So now 3.9.5 shows in the interpreters list, but that instance from the venv folder. I wanted it to make it "stop thinking" the version inside the Python folder is 3.9.1, so I could make it default. – 101is5 Jun 07 '21 at 20:57
0

You can run the code, right?

It's a problem with pylint instead of your code.

You can refer to the official docs, explaining the no-member problem in pylint.

And you can add this to the settings.josn file to fix this:

"python.linting.pylintArgs": [
    "--c-extension-no-member",
]
Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
  • Thank you, Steven. Yes, I could run the code without errors, probably because the version of Python that ran the code in the terminal was correct, the updated one. What made the errors go away was fully uninstalling VSCode: running uninstaller and, the crucial part, deleting the "Code" folder. Some bug was not letting VSCode read the interpreter as expected: although I had already updated Python to 3.9.5, VSCode would show it as 3.6.1, and what is most bizarre, apparently running it – 101is5 Jun 08 '21 at 08:17
  • My issue went from the pylint problem to a "VSCode not recognizing a Python update" problem, which made me think it was better to create another thread, here: (https://stackoverflow.com/questions/67877806/vscode-displays-older-version-of-interpreter?noredirect=1#comment119976621_67877806) – 101is5 Jun 08 '21 at 08:18
  • @101is5 hi, very glad to hear your reply. I think the Python version problem was caused by the cache, I had met that too. You can try to uninstall and reinstall the 'Python' extension. – Steven-MSFT Jun 08 '21 at 08:49
  • @101is5 Could you have a test of my method? I think it should work. And the problem with the interpreter version was just a cache problem. Your code has no problem at all, it's just a linting mistake. hope to hear from you. – Steven-MSFT Jun 08 '21 at 08:55
  • Don't mention it! Your reply is absolutely enlightening, but I happened to read it after doing the full uninstalling thing. So now that the problem is gone, I can't really test your method, but I'll probably need it sometime. Thanks for all help and support! – 101is5 Jun 08 '21 at 12:23