No module named matplotlib with matplotlib installed Python 2.7 - python

I am fairly new to python as well as matplotlib and I can't get it to work. From the code :
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node("spam")
G.add_edge(1,2)
plt.show()
I get the error:
Traceback (most recent call last):
File "test.py2", line 2, in <module>
import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot
This occurs even though it seems to be installed in:
usr/lib/python2.7/dist-packages/matplotlib/
Do you guys have any ideas?
Thanks in advance

You have 2 pythons installed on your machine, one is the standard python that comes with MacOSX and the second is the one you installed with ports (this is the one that has matplotlib installed in its library, the one that comes with macosx does not).
/usr/bin/python
Is the standard mac python and since it doesn't have matplotlib you should always start your script with the one installed with ports.
If python your_script.py works, then change the shebang (#!) to:
\#!/usr/bin/env python
Or put the full path to the python interpreter that has the matplotlib installed in its library.

thanks for your help. It appeared the wrong Python Version was used. By using
alias python=/usr/lib/python
it was fixed, but only temporarly.
To permanently set the alias correctly, I had to edit the ~/.bash_aliases and insert:
alias python=/usr/bin/python2.7
The other python version installed was 3.0 which was set as the defualt one, but without the matplotlib library.

You can check whether usr/lib/python2.7/dist-packages (if you are pretty sure matplotlib is installed here) is in your sys.path.
>>> import sys
>>> sys.path
If you don't find the path in the list, you can add lines below before importing matplotlib.
import sys
sys.path.insert(0, '/path/to/matplotlib')

Related

import matplotlib fails with "'module' object not callable" error

This question may appear similar to previously asked questions, but it is not.
I have a Python script with a single line:
import matplotlib
This fails with the error:
'module' object is not callable
random.py - print a random integer between 1 and 100
(followed by 3 more lines of usage of random.py)
If I start python from the command line, then type
import matplotlib
That works. I can instantiate classes from the module, plot figures and so on.
I am completely lost as to what is going on. Any clue appreciated.
Python version 2.6.6 on 64 bit x86 Linux machine.
"Matplotlib is the whole package; matplotlib.pyplot is a module in matplotlib; and pylab is a module that gets installed alongside matplotlib.
Pyplot provides the state-machine interface to the underlying object-oriented plotting library"
I took this from matplotlib manual website so You might want to take a look at.
https://matplotlib.org/faq/usage_faq.html
I dont know how you are trying you access your module but mistake is about matplotlib is not a module but its a whole package so you should call a module from inside of it. So you should call your module as one of style shown in the below
import matplotlib.pyplot
from matplotlib import figure or pyplot this part upto your module.
Matplotlib is an entire library, so if you are using import matplotlib as plt in your code, it might not work. Use 'import matplotlib.plyplot as plt' instead.

matplotlib RuntimeError: Python is not installed as a framework

This question has been asked before, in here, also here. However, the solution didn't fix the problem for my case.
The original error is, when I try to import matplotlib.pyplot, I got:
Traceback (most recent call last): File "", line 1, in
File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py",
line 114, in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/init.py",
line 32, in pylab_setup
globals(),locals(),[backend_name],0) File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py",
line 24, in
from matplotlib.backends import _macosx RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to
function correctly if Python is not installed as a framework. See the
Python documentation for more information on installing Python as a
framework on Mac OS X. Please either reinstall Python as a framework,
or try one of the other backends. If you are Working with Matplotlib
in a virtual enviroment see 'Working with Matplotlib in Virtual
environments' in the Matplotlib FAQ
I followed the solutions to add a ~/.matplotlib/matplotlibrc file with the code: backend: TkAgg. After doing that, my error changed to:
/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. warnings.warn('Matplotlib is building the font
cache using fc-list. This may take a moment.') objc[25120]: Class
TKApplication is implemented in both
/Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class TKMenu is
implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class
TKContentView is implemented in both
/Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class TKWindow
is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined.
I have no idea how to fix that. I'm not using a virtual machine.
PS: I found out that by adding:
import matplotlib
matplotlib.use('TkAgg')
before import matplotlib.pyplot, it seems to work. But adding those two lines of code every time is annoying...
What's going on, and how I can fix it?
I run my script in virtualenv. Python version is 3.5.
Add a line:
backend: TkAgg
in file:
~/.matplotlib/matplotlibrc
This solved the problem.
If you want to know more about why adding this solves the problem, you can read about customizing matplotlib's backend. And TkAgg solves this issue because of it's dependency with Tkinter.
Below worked for me:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Reference:
https://github.com/tensorflow/tensorflow/issues/2375
I installed Jupyter Notebook in virtualenv and below worked for me:
At Console:
echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
pip install matplotlib==2.1.0
On Notebook:
import matplotlib
Restart the kernel:
dataframe.plot.hist()
...
In my opinion, it will fix the error.
I had the same issues in python 2.7 in virtualenv and I managed to fix those by simply downgrading matplotlib to version 2.1.0
I was also having the same error. What I have done is to install miniconda packages and using the pythonw. Python app was already installed in my mac. Installation is as simple as breaking an egg. Just bash the .sh file in the terminal.
I was using pyenv so the matplotlibrc path wasn't in the home directory, so I created this script to figure the path out and change the backend to Tkagg:
vim $(python -c "import os,matplotlib; print(os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/matplotlibrc'));")
This script should work with any python on your system though.

no module named sys and time

I wrote a GUI, for which I used these imports:
import os
import sys
import serial
import scipy
import string
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from collections import deque
from numpy import array
from pylab import xlabel, ylabel, subplot
from scipy.fftpack import fft
from pylab import *
There is a red line below sys and time, I am using pycharm community edition 4.5.3, it is showing the reason for this error is 'no module named sys' and same for time.
But when i tried to run it, it works perfectly.
What is the reason behind it and will it affect my code in future?
Change the python interpreter from python to python2.7. It's helped me.
This is something I just ran into with Intellij 2017.3.4 where it could find every module except sys and time in the editor, but everything would run fine. I had both 2.7 and 3.5 version of python and it did not seem to matter which one I selected as the SDK. I tried adding and removing them.
When I went to Project Structure -> Platform Settings -> SDKs -> Python 3.5 -> Packages it prompted a warning that Python packaging tools not found. and had an install link. I installed it and the editor no longer complained about sys and time. When I switched SDKs to 2.7 (without installing the packaging tools) it complained again.
So I am not exactly sure what is happening that that seemed to fix it for me it other people run into this problem.

Importing & running matplotlib via CGI

I'm having some dramas with matplotlib and CGI, despite a night spent searching for solutions.
In brief, I'm running Python2.7 with matplotlib through a Bluhost server. I have want a simple script to display an image, but it's getting stuck on the import of matplotlib:
import cgitb, os
cgitb.enable()
import matplotlib
The traceback yields the following:
: No module named matplotlib
args = ('No module named matplotlib',)
message = 'No module named matplotlib'
Any clues? It seems most of the matplotlib/cgi issues pertain to specification of a writable HOME folder, yet inserting the following doesn't achieve anything:
os.environ[ 'HOME' ] = '/tmp/'
Any ideas or suggestions would be very welcome!
Cheers,
Hugh
OK, so it seems the problem has been solved. There were two issues: 1) The paths to the packages weren't properly specified in the PYTHONPATH belonging to the CGI-version of Python, and 2) The CGI ran a different version of python(p2.6), hence there were some incompatibilities with the various packages. I used "python2.6" when installing the numpy & matplotlib modules to ensure they were compatible with p2.6, then temporarily add a link to the package location when I call my script using CGI. Not ideal, but good enough for starters. Thanks for your help!

Matplotlib + Pylab import error

Been struggling to resolve this for most of the day, hoping someone can assist...
I'm running python 2.7, have installed matplotlib but when attempting to get started and import pylab I receive errors saying that no module is found, even when there is clearly a pylab module in the matplotlib directory and we're using:
from matplotlib.pylab import *
Any ideas?
Solved!
I had the script I was testing with in the same directory as the original 'matplotlib' directory from install. Using print sys.path I saw this was where the script was looking for the pylab files rather than the matplotlib directory in site-packages.
Thanks.
An easy mistake could be to name the file the same way as the module. I already made the mistake for example, when trying curses, and naming the file curses.py. I had to remove the .py and .pyc files to make it works.
Try importing like this:
from pylab import *

Categories