Why Matplotlib can’t be installed in Visual Studio Code | by Xue Wang | Towards Data Science

We know “Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python”. If Anaconda is permitted to be installed, normally there is no problem using Matplotlib. But recently there are Terms of Service updates for commercial users from Anaconda. The detailed info is explained as below:
“As a response to the dramatic increase in usage and adoption of Python and Anaconda among commercial users over the last few years, we updated our Terms of Service. Moving forward, we are asking commercial users who rely on our packaging & distribution efforts to purchase one of our commercial offerings.” — https://www.anaconda.com/blog/anaconda-commercial-edition-faq
In case the company isn’t ready to pay for Anaconda yet, some engineers might turn to VS Code.
Before python can run properly in VSC, there are some settings that will bother the users. Most of them are the error to ask you to install the library, like “Import Error: No module named numpy”, which can be solved by the commands like “pip” or “pip3” or “python pip” or “python3 -m pip” etc. to solve.
But today, I would like to focus on Matplotlib problem, which might not be solved just by the pip command.
In case:
You have a windows systemAnaconda isn’t installed on your computerPython extension has been installed in VSCThe interpreter (at the bottom left corner of VSC) has been chosen correctlyNo visual studio build tool has been installed (you might also have no idea why you need it until the problem happens, for an engineer (like a mechanical engineer) to write python code.
You have a windows system
Anaconda isn’t installed on your computer
Python extension has been installed in VSC
The interpreter (at the bottom left corner of VSC) has been chosen correctly
No visual studio build tool has been installed (you might also have no idea why you need it until the problem happens, for an engineer (like a mechanical engineer) to write python code.
Now let’s take an example.
Try to test the below py file: standardplot.py:
import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 20, 100)  # Create a list of evenly-spaced numbers over the rangeplt.plot(x, np.sin(x))       # Plot the sine of each x pointplt.show()                   # Display the plot
Whether you get the below beautiful figure:
Or do you get errors? Like below:
Import Error: No module named numpy
Google it, the solution could be:
pip install numpyor pip3 install numpyorpython3 -m pip install numpyorpython3 -m pip install numpy
Similar problems like “no module for sklean” can be solved with a similar solution.
Run it again, now no problem with NumPy, but there might have an error as:
ERROR: Command errored out with exit status 1:......error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
This is because Microsoft C++Build Tools hasn’t been installed in your computer. In order to run matplotlib or a similar interactive interface in Microsoft for Python, Build Tools is needed, not only Microsoft C++ Redistributable.
How:
Click the link, it leads you to the below window:
Download build tools, run the .exe file, do cross C++ build tools as below during the installation. Be sure to choose the correct items.
It takes a bit long time, drink water or walks around during the process. :-)
If the build tool is installed correctly, now install Matplotlib in the normal way.
Run the py file, you will get the beautiful figure as above.
Takeaways:
I wrote this story because when I searched for the solution, most mentioned installing it but very few mentioned this case. Yes, it could be a rare case ( thinking about Anaconda can’t be installed...), but when it happens, it might take your time to solve it. Hope it helps to remind you of the case.The possible reason why Microsoft Visual C++ build tool is a prerequisite to run Matplotlib in python in VSC, I found below an explanation, but please feel free to correct me if I am wrong.
I wrote this story because when I searched for the solution, most mentioned installing it but very few mentioned this case. Yes, it could be a rare case ( thinking about Anaconda can’t be installed...), but when it happens, it might take your time to solve it. Hope it helps to remind you of the case.
The possible reason why Microsoft Visual C++ build tool is a prerequisite to run Matplotlib in python in VSC, I found below an explanation, but please feel free to correct me if I am wrong.
Windows Python needs Visual C++ libraries installed via the SDK to build code, such as via setuptools.extension.Extension or numpy.distutils.core.Extension. For example, building f2py modules in Windows with Python requires Visual C++ SDK as installed above. On Linux and Mac, the C++ libraries are installed with the compiler. -https://www.scivision.dev/python-windows-visual-c-14-required/
Thanks for your time. Hope it helps to solve your Matplotlib problem in VSC.