pylab cannot find reference for its modules - python

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

Related

How do I change the backend in CoCalc to show a matplotlib scatter graph?

I have a program that is working fine locally, but I need to use it in CoCalc on the cloud to share with a group. Everything works fine until the end, when I want to create a scatter graph, in standard way. There are a bunch of other specs, but essentially, it's just
plt.scatter(x, y, color = 'blue')
plt.show()
Nothing tricky. But in Cocalc, it tells me "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure."
Here are the imports from the beginning of the file:
import nltk
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
I've searched for this error and tried the solutions I've found, such as importing tkinter. So, for instance, if I change the headers to
import nltk
import tkinter as tk
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
I get a different error: "Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running."
I've seen instructions about pip installations and such, but a) I'm not sure the installations would make everything work for my group, and b) CoCalc generally has most of the standard packages ready to go, so maybe that's not the problem?
Obviously, I'm not experienced with these issues, so help is much appreciated.
If you're using a Jupyter notebook in CoCalc, then the above will just work fine. Here's an example:
https://cocalc.com/wstein/support/matplotlib-scatter-in-jupyter
If you're writing all of your code as part of a Python program (so, e.g., .py files) and running this from a Terminal, CoCalc will also fully support Tk and this working! However, you have to start a new X11 desktop session in CoCalc by clicking +New --> "Linux Graphical X11 Desktop", then run your Python program from the terminal in the upper left. You'll see any windows that pop up in the main window off to the right.
See https://cocalc.com/wstein/support/scatter-matplotlib-x11

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.

ipython notebooks sometimes not finding functions in file

EDIT: This is not solved in the suggested duplicate; reloading the module
after editing the file doesn't help.
I have a python file "/home/Misc/misc_def.py" collecting some functions that I'm using in several ipython notebooks. The first cell in each notebook is
import csv
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline
sns.set_style('white')
from sys import path
path.append('/home/Misc')
import misc_def
However, the strange thing is that sometimes this works (the notebook can find the functions in the file) and sometimes it doesn't. I'm using notebooks in different folders, but I think this shouldn't matter since it's all absolute paths. The errors I get are standard for not finding functions; e.g.
NameError: name 'get_overlap_data' is not defined
Is there something unstable about the way I do it above?

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.

bpython configuration - importing numpy and matplotlib by default

Is it possible to start the bpython interpreter so that it always runs some custom commands when it launches?
In my case I simply want to do:
import numpy as np
import matplotlib.pyplot as plt
I can't see anything in the docs. Anyone know a way?
It is written in the docs, just not clearly labelled as such at: http://docs.bpython-interpreter.org/django.html
Gist of it is you can have an environment variable called PYTHONSTARTUP. bpython will execute this file before you get dropped in the interpreter.
While ikanobori's answer is the way to go here, I thought I show another simple alternative.
import numpy as np
import matplotlib.pyplot as plt
import bpython
bpython.embed(locals_=locals())
This will start up the bpython REPL and load in the local variables and other symbols. This would be useful if you wanted to have more than one customized shell.

Categories