Unable to import 'matplotlib.pyplot' - python

I know this question is asked a lot, I searched for the last three hours for an answer, but couldn't solve my problem.
As soon as I try to:
import matplotlib.pyplot as plt
my Visual Studio Code IDE tells me that: Unable to import 'matplotlib.pyplot'
My current version of Python is:
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
as you can see, I used the Anaconda package, hence matplotlib should be included. I am Using Mac OS 10.15.2
If I enter
import matplotlib.pyplot as plt
it does import that, however, as soon as I try a basic example, like
x = [1,2,3]
y = [2,4,1]
plt.plot(x, y)
I obtain:
[<matplotlib.lines.Line2D object at 0x10c23d950>]
And a blank token in my Dashboard pops up, which I can only force to quit.
In the course of trying to solve this problem, I tried anything I saw that was related to this topic, although I did not understand everything of it. I hope I did not make a real damage.
My last step was completely deinstalling anaconda and reinstalling.
Thanks for taking your time!

The module can be imported, but your IDE says module not found, means your linter (vscode uses pylinter) is not configured correctly.
Start PyLint from correct anaconda environment in Visual Studio Code

First you need to install package matplotlib using conda console in your project
conda install -c conda-forge matplotlib
You also can install package using PIP
python -m pip install -U matplotlib
And Finally import your package in your source code
import matplotlib.pyplot as plt
Or another Way you can import
from matplotlib import pyplot as plt

Having come here with this error myself, especially if you're using VS Code you need to make sure that the version of Python you're running in VS Code is the same one that's running on your OS. Otherwise, you can conda install or pip3 install all you want into the OS, but VS Code and Jupyter Notebooks won't know anything about those modules. There are a few ways to figure this out, but mostly it's making sure that the Python version you see when typing python --version in the terminal matches the interpreter shown in the top-right corner of your VS Code window.

Related

VSCode is running two different versions of Python - one for environment and one for system

I am trying to create geographical maps of data, and to do this I am trying to use the basemap package. As I was using Python 3.9 on Windows 10, installing basemap was nearly impossible. So I created a new environment running Python 3.6, which allowed me to install basemap with no issues. Now basemap shows up under conda list command, and my new environment py36 is selectable in VSCode. Once selected, it tells me I am using Python 3.6.12. However, when I simply try to import packages:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
#import basemap
import sys
print(sys.version)
...basemap cannot be imported. (It is commented out in this version so that I could print the version of Python that was running in VSCode). So I commented the import basemap line out (all other packages imported correctly) and added two lines to print the version of Python that the code snippet was using. The result was surprising: Despite selecting an interpreter that was using 3.6.12 (see image attached, bottom of screen shot), the return for print(sys.version) was
3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]
How can I get VSCode to run the version of Python that is defined explicitly in the environment, so that I can import basemap?
Thank you in advance for any help on the subject.
Brad

How I do properly import serial data from arduino using pycharm

I am newbie in python.
After I installed python 3.8 latest version, libraries ...etc
Pycharm IDE, and for every time you create project you have to install packages so your code look nice with no red line under.
after I spent hours trying to find whats going on, Error, visual studio ++14 and not 15 or 19.
The code line, import serial doesn't seem recognized by pycharm or by python.
I have read many people having the same problem but few were successful to solve, other are still not.
My Problem is that I need to plot data from arduino using python over serial.
Anyone have an idea how to solve the import serial not working?
The code implemented:
import serial , error
import numpy as np package not found but managed to install it and have to install it again
import matplotlib.pyplot as plt
%matplotlib qt
ser = serial.Serial('COM4',9600) Syntax error
Just tried to install matplotlib package, I got this error
ModuleNotFoundError: No module named 'Cython'
An hour ago did work just fine downloading the package.
Any recommendation for another python IDE than pycharm ?
U can use anaconda navigator. It comes with many inbuilt libraries such as pandas numpy matlab etc according to me it is best to use anaconda navigator in which u can either use spyder ide or jyputer notebook do upvode if u found helpful

New To Visual Studio Code, having problems with pandas/numpy

I normally use PyCharm for python coding, but just for the heck of it, I tried to use Visual Studio Code today, and I'm having some problems.
So I followed the steps shown in the "Getting Started with Python in VS Code" page, and I copied this to my new python project:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x)) # Plot the sine of each x point
plt.show() # Display the plot
just to test if it works well, and for some random reason, whenever I run this code through terminal/cmd in VSCode, I get this:
ImportError: Missing required dependencies ['numpy']
BUT when I use the Debug Mode, it seems to work perfectly fine.
similar thing happened when I tried to run my previous projects through VSCode. So I thought maybe it was just the problem with my environment, so I changed it to the one where I have my tools installed, but nope, I still got the error.
I tried uninstalling then installing again and that didn't work as well.
I Seriously don't know what's happening right now. Why does it work well in Debug Mode but not in terminal/cmd? does anyone know what to do in this situation?
Thanks!
...In Python, packages are how you obtain any number of useful code libraries, typically from PyPI. For this example you use the matplotlib and numpy packages to create a graphical plot as is commonly done with data science. (Note that matplotlib cannot show graphs when running in the Windows Subsystem for Linux as it lacks the necessary UI support.)
Return to the Explorer view (the top-most icon on the left side, which shows files), create a new file called standardplot.py, and paste in the following source code:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x)) # Plot the sine of each x point
plt.show() # Display the plot
Tip: if you enter the above code by hand, you may find that auto-completions change the names after the as keywords when you press Enter at the end of a line. To avoid this, type a space, then Enter.
Next, try running the file in the debugger using the "Python: Current file" configuration as described in the last section. (If you still have "stopOnEntry": true in that configuration, you need to select the run command again to continue.)
Unless you're using an Anaconda distribution or have previously installed the matplotlib package, you should see the message, "ModuleNotFoundError: No module named 'matplotlib'". Such a message indicates that the required package isn't available in your system.
To install the matplotlib package (which also installs numpy as a dependency), stop the debugger and run Terminal: Create New Integrated Terminal from the Command Palette (⌃⇧(Windows, Linux Ctrl+Shift+))). This command opens a command prompt for your selected interpreter. Then enter the following commands as appropriate for your operating system (commands may require elevation if the Python interpreter is installed in a protected area of the file system):
Note: If you are unable to install the package or encounter other problems, please file an issue on GitHub so we can help you investigate.
# Don't use with Anaconda distributions because they include matplotlib already.
# macOS
sudo python3 -m pip install matplotlib
# Windows (may require elevation)
py -3 -m pip install matplotlib
# Linux (Debian)
sudo apt-get install python3-tk
python -m pip install matplotlib
from: https://code.visualstudio.com/docs/python/python-tutorial
Got this error and was able to fix it by running conda init in the Python Debug Console terminal, and then closing the terminal before starting a new debug session.
In VS Code undee Windows open Terminal -> New Terminal and run
pip3 install pandas
and then
pip3 install matplotlib
While installing pandas, numpy will be installed as well
I believe it may have something to do with the PATH variables. I just had the same problem in Windows 10 and found out that if I launch VS Code through an anaconda prompt it works just fine, no need to mess around with the PATH variables.
So, instead of opening VS Code through the start menu, just open an anaconda prompt (or anaconda shell), cd to your directory and type code .. That solved my issue.

Anaconda / IPython / matplotlib Issue

I've just completed the installation of Anaconda on my MacBook Pro (running OS X 10.10 Yosemite). I installed the recommended package, which includes Python 2.7.
The versions I have of the (I think) pertinent packages are:
Numpy: 1.9.0
Scipy: 0.14.0
IPython: 2.2.0
matplotlib: 1.4.0
I was following the instructions for testing the installation listed here:
https://python4astronomers.github.io/installation/python_install.html#test-the-installation
Everything was fine, except for running IPython with matplotlib integration. Based on the guide on that page (which do not tell you to explicitly import matplotlib), and on my limited knowledge of IPython, I was under the impression that invoking IPython with
ipython --matplotlib
would "automagically" import the matplotlib modules somehow.
However, when I typed the line below into the IPython interpreter session:
print(matplotlib.__version__)
an error message was displayed which indicated that matplotlib was unavailable:
NameError
Traceback (most recent call last)
<ipython-input-1-dc737d9da186> in <module>()
----> 1 print(matplotlib.__version__)
NameError: name 'matplotlib' is not defined
However, IPython does say this when it is invoked:
IPython 2.2.0 -- An enhanced Interactive Python.
...
Using matplotlib backend: MacOSX
I know that IPython develops at a rapid pace, so my question is:
Did I miss something in the installation process (although it was performed using the Anaconda pre-compiled binary installer), or has the was matplotlib integration is achieved with IPython changed, or could the set of instructions I followed for testing the installation be missing something? I checked that site for a way to contact those guys before coming here, but I could not find it.
I've been through period of extraordinary hair loss due to having pulled huge tufts of it out while trying to sort out $PATH issues with Python and related problems while using Python on OS X, so this time I went with Anaconda and am just trying to quash any problems that may arise before I further complicate the situation by installing other modules not included with Anaconda.
Thanks in advance!
All the --matplotlib option does is set IPython up to display images generated by matplotlib so they are not blocking. It doesn't import anything. You still have to import matplotlib manually.

Trouble in installing Basemap in matplotlib

I am trying to install Basemap, and beforehand I already have the prerequisite versions of Matplotlib, Python and Numpy working on my Mac. To install Basemap I am following the instructions in this website http://matplotlib.org/basemap/users/installing.html
I seemed to progress quite well with the steps, until the very end which is "Check your installation by running from mpl_toolkits.basemap import Basemap at the python prompt." I got "ImportError: cannot import name pyproj" from a line that reads "---> 30 from mpl_toolkits.basemap import pyproj".
Does anyone have any idea? Many thanks!
The compiled module could be accessible by python. To achieve that you should put the module into python path or you need to add the location of the module to PYTHONPATH environment variable.
To see the python installations paths you can write these lines to python shell:
import sys
for path in sys.path:
print(path)
The code will show the paths python is looking for modules.
After you compile the c library, you need to go to upper folder and run python setup.py install as it's said in the installation page. This will put python modules into one of your python paths.
I had the same problem installing basemap-1.0.7.
I found that the file
/usr/lib64/python2.6/site-packages/mpl_toolkits/basemap/init.py
had a reference to axes-grid1, but python lists only module axes_grid.
So I changed grid1 to grid and now basemap imports without error.
Had the same issue on OSX, found after much faffing that Basemap worked fine with a non-native version of python (for me 2.7.12 with everything under /opt/local/Library/Frameworks...) but not with the native version (for me 2.7.10 with most stuff under /System/Library/Frameworks... and Basemap under /Library/...). I did note also that under the native version there was already some mpl_tooklit content and without the permission to add Basemap there you end up with mpl_toolkit contents in multiple places. I wasn't sure if this was the problem specifically but as I said having it all under opt using non-native python was what solved this for me.

Categories