Matplotlib + Pylab import error - python

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 *

Related

Python Pandas.tests.extension Module Not Found

I'm quite new to python and have been trying to run code for someone's project. However, it kept giving me the ModuleNotFound error message that says - "No module name 'pandas.tests.extension.numpy_' " in the ubuntu terminal.
Inside the code itself, the import statement is
from pandas.tests.extension.numpy_.test_numpy_nested import np
I do have the pandas module installed. Do you guys know what the issue could be and how to fix this? Thank you
Terminal and import line screenshot
Use two import lines
import pandas.tests.extension
import numpy_.test_numpy_nested as np

No module named matplotlib with matplotlib installed Python 2.7

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')

pylab cannot find reference for its modules

I have a mac OS X Yosimite and I'm using python 2.7.10 and Pycharm as my IDLE. I have pylab installed properly but I cannot use any of its modules.
When a try:
from pylab import show
(or any module) it says
ImportError: cannot import name show
But when I run just the line import pylab I get no errors!
I tried leaving that way and calling the module anyway.
pylab.imshow(...)
But I got the same error obviously. Do I have to install those modules separately?
PS: I'm almost sure the problem has nothing to do with the interpreter
Try importing from matplotlib.pyplot, rather than from pylab (this is now the recommended way to import matplotlib):
From example:
from matplotlib.pyplot import imshow
imshow()
Or:
import matplotlib.pyplot as plt
plt.imshow()

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!

Building wxpython exe using PyInstaller

I am trying to build an exe using PyInstaller (with Python 2.7) and have been stumped. My python code uses the modules wx, matplotlib, basemap (a part of matplotlib), and pylab. There are others, but these seem to be the main ones.
I have installed PyInstaller and then i did:
python pyinstaller.py C:/Python27/Convertthisfile.py
It goes through the whole process, but when i try to run the final executable, it comes up with the error:
"No module named PyQt4.QtCore"
I have installed PyQt GPL 4.9.1 for Python 2.7. However, I have no idea where PyQt is even being used in my code. I don't specify it anywhere that i know of.
Anyone have any thoughts? Nothing I do seems to work. I have even tried the GUI2EXE.py -- i can't get py2exe, pyinstaller, or cx_freeze to work.
HELP!
I have added my code below to hopefully help. To answer the comments, yes, my code is located in C:\Python27. I have no problem "building" it with pyinstaller, but the above error comes up when i try to run the given executable. I have searched the code and do not see any use of PyQt4.
When i run cx_freeze, i have issues with the basemap data files -- they do not seem to be included in the ".zip" when i build it. Also, none of my modules seem to get included either.
Here is what i import for my code (some of these functions are my own -- mainly the last ones listed).
import wx
import time
from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
from mpl_toolkits.basemap import Basemap
from matplotlib.figure import Figure
from datetime import datetime
import wx.calendar as cal
import wx.lib.mixins.listctrl as listmix
from pylab import *
from decimal import *
import adodbapi
import annote_new
import cPickle as pickle
import calc_dist
import Game_Score
import Calculate_Distance
import Duplicate_Finder
import copy
Hopefully this clears things up?

Categories