ImportError: No module named 'matplotlib' - python

Brand new to Python (typically program in MSDN C#) and I'm trying to make use of the matplotlib to generate some graphics from .csv files
I've downloaded & installed Python as well as Anaconda onto my Windows 10 machine, the versions are Python 3.5.2 and Anaconda 4.1.1
I open up the Python "notepad" interface and do
import matplotlib.pyplot as plt
plt.plot([1,2,3],[3,2,1])
plt.show()
but when I run the code I get the error:
ImportError: No module named 'matplotlib'
I've looked at some other posts for this but they all seem to be in regard to Mac OSX or Linux. Some have pointed to multiple installs of matplotlib, but I haven't turned up such a situation so far. What might be causing this, or how can I troubleshoot it?
**Edit:
The path returned to me from the import sys recommended in the comments gave me this response
['C:\Users\a.watts.ISAM-NA\Desktop',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\python35.zip',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\DLLs',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib\site-packages',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib\site-packages\setuptools-26.1.1-py3.5.egg']

You essentially have 2 versions of python on your system - the standard one you downloaded and the one that ships with Anaconda. When you are running code in the IDLE you are using the standard version (in C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\python.exe) where matplotlib isn't installed which is why you are getting the error.
You need to use the Anaconda version (C:\Users\a.watts.ISAM-NA\AppData\Local\continuum\anaconda3\python.exe) that comes with the scientific stuff already setup. It looks like your system is using this one from the cmd so if you run scripts from there it should use the Anaconda version. If you want to use something more interactive you can also use spyder - the Anaconda version of the IDLE - or run jupyter notebook from cmd to get a browser based platform for interactive development

Related

Module not found in Spyder outside of Anaconda

For a training course, I had to install Anaconda and launch Spyder from there. This used to work. However, I want to understand more about Python, because I have little experience with programming environments (I only use R and Stata up to now).
I stumbled on this issue: when I open Spyder directly (presumably I have downloaded a standalone version), most syntax runs well, but I cannot get the 'seaborn' module to work.
I tried pip install seaborn, pip3 install seaborn, conda install seaborn, but to no avail (outside of Anaconda, that is). I also tried updating spyder, homebrew, python and anaconda to the most recent versions. Probably this caused further issues discussed below.
My Python version is 3.9.7 (in terminal), Python3 is 3.9.9 (in terminal and through Anaconda), and through Spyder directly it is 3.9.5. Spyder IDE is 5.2.1, under Anaconda it is 5.1.5. I have no idea why these are all different, where to find the right paths, and how to update the Spyder version of Python and how to add the 'seaborn' module to that environment. It is possible to select a path for the Python interpreter in the Spyder preferences, but which path to take is unclear to me.
In short: how can I get seaborn to work in Spyder without having to rely on Anaconda (which seems a bit bloated to me and is slower to start up)?
Meanwhile, real problems occurred: I can't open Spyder from Anaconda any longer (so seaborn is not totally out of reach). This is the message:
/Users/myname/opt/anaconda3/bin/pythonw: line 3: /Users/myname/opt/anaconda3/python.app/Contents/MacOS/python: No such file or directory
This is the simple syntax I wanted to try:
# Seaborn example
import numpy as np
import pandas as pd
import seaborn as sns
sns.set_theme(style="whitegrid")
rs = np.random.RandomState(365)
values = rs.randn(365, 4).cumsum(axis=0)
dates = pd.date_range("1 1 2016", periods=365, freq="D")
data = pd.DataFrame(values, dates, columns=["A", "B", "C", "D"])
data = data.rolling(7).mean()
sns.lineplot(data=data, palette="tab10", linewidth=2.5)
Giving the error message: ModuleNotFoundError: No module named 'seaborn'
Probably this question is related: installed module in anaconda prompt shell but module not found in spyder?
I am working on an M1 MBA under Monterey.
Many thanks
I found the solution, which is to create a new environment and link to it, on the Spyder FAQ:
https://docs.spyder-ide.org/5/faq.html#using-packages-installer
The video was helpful: https://youtu.be/i7Njb3xO4Fw
It seems I can use the Python version installed through conda (in opt/anaconda3) and base an 'environment' on this, adding modules by first activating the environment and then using conda install.
Meanwhile, I still cannot launch Spyder from Anaconda-Navigator.
/Users/myname/opt/anaconda3/bin/pythonw: line 3:
/Users/myname/opt/anaconda3/python.app/Contents/MacOS/python:
No such file or directory
Probably reinstalling will solve this. But I'm good for now.

Module installed in Anaconda, same interpreter with Spyder yet Spyder gets "ModuleNotFoundError"

I have installed the netcdf4 library through
conda install -c anaconda netcdf4
And it shows up fine on my conda environment:
However, when I try to import it in Spyder, I am getting an
"ModuleNotFoundError: No module named 'netcdf4'"
I have tried the solutions here. As you can see below, both the interpreters are the same (in tools/preferences it's set as python.exe, not pythonw.exe):
Yet when I try to import the netcdf4 module, it says there is no module named this.
Any ideas what the cause could be? It's been two days of trying to get Anaconda to work and it has not been a smooth experience at all, really close on giving up on Anaconda as a whole.
Added per request, a screenshot of my entire IDE. Notice in the console I ran pip install netcdf4, where it says I already installed it. Directly below is importing the module and the error:
And how the console looks on each startup:
The docs/tutorial used stated
import netcdf4
While now it is case sensitive
import netCDF4
Such a simple fix..

ImportError: No module named pygal

I'm using:
macOS Catalina 10.15.1
VSCode 1.40.2
Python 3.8.0 64-bit
I'm stalling pygal trough: pip3 install --user pygal and OK DONE.
But, when import pygal on VScode it return error like this-> ImportError: No module named pygal
Someone can help me? Look the 1st line of the code.
enter image description here
Please verify that Sublime uses the same Python interpreter that you installed Pygal in. I am not too familiar with macOS; Sublime might use the system interpreter by default, or a system-wide side-packages folder, rather than a local one. Start an interactive session by calling python3 in your console and try to import Pygal from there. If that works, your problem is likely related to Sublime using a different interpreter/env than what your installation was perfomed in.
Check if you installed the pygal interpreter and if you don't have it installed installed it. Happens to me all the time.

Python packages are imported on terminal but not on IDLE

So I am new in Python. I downloaded Anaconda (and Homebrew) and using Terminal on my Mac I confirmed that, say, Numpy is installed:
pip install numpy
to get as a result
Requirement already satisfied: numpy in ./anaconda/lib/python3.6/site-packages
Then opening Python within Terminal I can indeed import the package. The story is not the same once I open IDLE. So when I open it and try to import, say, Numpy I get the following message:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
I guess there is something I do not know, I assume it is a common rookie problem. Can you help me with this?
Since your IDLE is using Python 3 you should use
pip3 install numpy
to get it installed for the correct Python. If you want to use your default installed Python (2.x) instead, IDLE is located at /usr/bin/. From your terminal you can open it with
/usr/bin/idle
this should have the numpy you installed with pip install numpy
See when you are working with Anaconda it creates its own virtual environment.
Now, If you are new in python on MAC then you might not familiar with Virtual Environment.
I suggest you to download python from the official website www.python.org
or Click here to directly download Python 3.6.2 on your MAC.
Then, Download Pycharm(The Best IDE for Python)
Download the community version for beginner and it is also free.
Pycharm Community Version -> Click Here
And do whatever you want and also must read about Virtualenv
According to me, Pycharm is better then Anaconda's Jupyter Notebook
Wish you good luck and show your creativity in python ! !
Erm... well it appears as though - since you have 2 versions on your computer - that it might be because it imported it into the other file path directory. Now, I've never worked with Mac, but I think if you just specify which pip you want to download from, it might work.
For example, on Windows:
C:\Python34\Scripts\pip.exe install numpy
or if you were doing it for 3.6, you would follow the path to the folder, find pip and install. So, I suggest to install the normal IDLE before any other platform built upon it just because it is easier to import modules and is not as bad to break like yours has.
There can be two issues -
You are using python 2 as a kernel for IDLE, since numpy is installed for python3.6. This will raise an error.
The issue is with anaconda's configuration with IDLE. Anaconda installs numpy in ./anaconda/lib/python3.6/site-packages. If IDLE uses systems default python instead of anaconda's. You will not be able to import numpy.
You can try running this snippet -
import sys
print('\n'.join(sys.path))
to track the location of python exactly.
One of the workaround that I can think of is -
create a virtual environment using anaconda. Something like
conda create -n py352 python=3.5.2 anaconda
and then fire IDLE from your terminal. Though I am not entirely sure, if this works for mac. If it doesn't, let me know the output of -
import sys
print('\n'.join(sys.path))

Importing OpenCV with python 2.7 in Virtualenv and PyCharm

I am struggling with installing opencv for python 2.7.11 on OSX for almost three days now.
After some failures, I accomplished a first success by following the instructions here. So my basic setup is python 2.7.11 running from ~/.virtualenvs/cv_env/bin/python and I have a cv2.so located in ~/.virtualenvs/cv/lib/python2.7/site-packages/.
So good so far. Using source ~/.virtualenvs/cv_env/bin/activate, I can activate the virtualenv and than use import cv2. For some reasons, this does not work always. From time to time, I have to deactivate first and than reactivate (any guesses?).
Next, I wanted to use opencv in PyCharm. Under "preferences - Project interpreter", I selected the virtualenv interpreter and this is also working. I can import other moduals like numpy and pandas (previously installed into the vortualenv using pip). But for some reasons, I am not able to import opencv (import cv2). It always gives me
ImportError: No module named cv2
So my question is, why I am able to import opencv in terminal (at least sometimes) but not in PyCharm. Any ideas are welcomed.
Best,
Fabian
Your cv2.so located in a different directory. And you activating from another directory. I mean cv_env and cv.

Categories