I've read these and tried the solutions but still not getting anything to show:
matplotlib show nothing although i called show
Why doesn't pyplot.show() work? [duplicate]
matplotlib does not show my drawings although I call pyplot.show()
I'm using Python 2.7.6 |Anaconda 1.9.0 (64-bit) on Ubuntu 12.04.
How can I go about diagnosing the problem and trying to solve it?
EDIT:
-First I edited matplotlibrc and change the backend to GtkAgg (was set to QtAgg).
-Then I tried to change the backend via code: matplotlib.rcParams['backend'] = "GtkAgg"
EDIT2 - adding detail from the Spyder console (no plot is shown at the end):
Python 2.7.6 |Anaconda 1.9.0 (64-bit)| (default, Jan 17 2014, 10:13:17)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Imported NumPy 1.8.0, SciPy 0.13.3, Matplotlib 1.3.1
Type "scientific" for more details.
>>> scientific
This is a standard Python interpreter with preloaded tools for scientific
computing and visualization. It tries to import the following modules:
>>> import numpy as np # NumPy (multidimensional arrays, linear algebra, ...)
>>> import scipy as sp # SciPy (signal and image processing library)
>>> import matplotlib as mpl # Matplotlib (2D/3D plotting library)
>>> import matplotlib.pyplot as plt # Matplotlib's pyplot: MATLAB-like syntax
>>> from pylab import * # Matplotlib's pylab interface
>>> ion() # Turned on Matplotlib's interactive mode
Within Spyder, this interpreter also provides:
* special commands (e.g. %ls, %pwd, %clear)
* system commands, i.e. all commands starting with '!' are subprocessed
(e.g. !dir on Windows or !ls on Linux, and so on)
>>> plot(range(3))
[<matplotlib.lines.Line2D object at 0x3f3cd50>]
>>> show()
I ended up solving by doing this in Spyder:
Preferences -> Console -> External modules -> check Install Spyder's input hook for Qt
In order for your matplotlib backend choice to work, you need to have the Python bindings for that backend installed. You may be running a GTK-based windows manager like GNOME, for example, but still not have the Python bindings around. According to this, the proper Ubuntu package is python-gtk. So, open up your favorite Terminal emulator and enter
sudo apt-get install python-gtk
(you'll need admin privileges to do it). Restart your python/matplotlib session, and you should be good to go.
I'm on Mavericks, solved the problem by changing backend to Qt4Agg in matplotlibrc. (as instructed in another answer here: Why doesn't pyplot.show() work?)
Related
When I get to my beloved Spyder console and it welcomes me with
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
Type "copyright", "credits" or "license" for more information.
IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
I go on and type this:
In [1]: from PIL import Image
And it goes and shows another line like everything was right.
Then, on the next line, I type this:
In [2]: from PIL import ImageTk
And it returns this:
Traceback (most recent call last):
File "<ipython-input-2-47edf18ebb7f>", line 1, in <module>
from PIL import ImageTk
ImportError: cannot import name ImageTk
Okay, this means I should have a problem with my libraries. No sweat.
A fellow programmer had a similar error here, and:
I am sure there are no typos in my import
Ubuntu reports the python-imaging package as up-to-date:
Ubuntu reports the following on the python-imaging-tk package:
These Bash lines:
python-imaging is already the newest version (3.1.2-0ubuntu1.1).
<more bash lines />
The following packages have unmet dependencies:
python-imaging-tk : Depends: python-imaging (= 1.1.7-4) but 3.1.2-0ubuntu1.1 is to be installed
This is the juiciest part. When I go back to Spyder and type to get the version of PIL.Image, it returns this:
In [3]: Image.VERSION
Out[3]: '1.1.7'
I am at a loss here. Please send help.
Doing a
sudo apt-get install python-imaging=1.1.7-4
and then
sudo apt-get install python-imaging-tk
solved the problem.
It still bothers me why but at least my dependencies are working now.
sudo apt-get install python-imaging-tk
this just solved my problem. This is for python 2.7
when i run command import numpy as np or import scipy as sp, it gives me error like:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import numpy as np
ImportError: No module named numpy
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
import scipy as sp
ImportError: No module named scipy
(Disclaimer: there are already tons of well established way / tutorials on the internet. I'm merely posting this to hopefully help out quickly)
What you want to achieve
To install a library (e.g. numpy, scipy) locally on a machine (e.g. laptop, server, etc.) and import that library from a Python code.
One of the solutions: Anaconda
One of the popular / quick ways in the Python scientific community is to do this via Anaconda (disclaimer 2: I personally prefer Anaconda due to its ease of enabling me to switch / play with different Python environments). Here is the step by step instructions:
Download and install the Anaconda distribution onto the machine locally.
Create a file environment.yml and store it anywhere you like (e.g. a subdirectory within your home directory). The file looks like this gist file - tweak it to your taste (e.g. choose Python version 2.x vs 3.x, add/remove/edit dependencies, etc.)
Within the same directory where you've created the environment.yml, create a conda environment by: conda env create -f environment.yml. For this particular gist file, it will create a conda environment (called "helloworld") with the specific Python version (2.7) and the anaconda package (which includes the popular numpy and scipy libraries).
Activate the environment (i.e. "go inside that environment") by: source activate helloworld (replace "helloworld" with whatever name you specified in environment.yml).
Now you are in the "helloworld" conda environment, start up a python console: jupyter console.
Now try importing stuff within this console:
Python 2.7.13 |Anaconda 4.4.0 (x86_64)| (default, Dec 20 2016, 23:05:08)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy as np
In [2]: import scipy as sp
In [3]: np.version.version
Out[3]: '1.12.1'
In [4]: sp.version.version
Out[4]: '0.19.0'
(to quit console just do a Ctrl + D to go back to command line)
For step 5 above, also try out:
jupyter notebook
jupyter qtconsole
And play with the python commands.
When you are done with the conda environment, just "deactivate" (i.e. get out of) it by doing this in the command line: source deactivate.
Top tip: don't forget step 4 - this defines which conda environment you are in (i.e. which python version and libraries available etc.). I occasionally ommited step 4 by accident and get that error "no module named numpy", etc.)
See this Anaconda get started guide for more info.
The non Anaconda way
If you would like to avoid Anaconda all together, just simply do this in the command line:
Install the numpy and scipy libraries:
pip install numpy
pip install scipy
Start a Python interpreter:
python
Do the library import stuff within the Python interpreter:
>>> import numpy as np
>>> import scipy as sp
>>> np.version.version
'1.11.3'
>>> sp.version.version
'1.11.3'
Additional alternatives
You could try out the Python VirtualEnv - though I've never really used it since I've started using Anaconda.
I am a python newbie and am using the book Learn Python the Hard Way. Exercise 25 from the book asks us to import the python module into the python interpreter.
I used "/usr/bin/python" to invoke the interpreter:
S-MacBook-Air:~ s$ /usr/bin/python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
But i get an error when importing the file:
>import ex26_test
>>Traceback (most recent call last):
>>File "< stdin >", line 1, in < module >
>ImportError: No module named ex26_test
the file sits on the following folder
(~/documents/1Webdev/LPTHW/ex26_test.py)
How can i import the module "ex26_test" into the interpreter please?
thank you in advance!
SD
When a module named spam is imported, the interpreter first searches
for a built-in module with that name. If not found, it then searches
for a file named spam.py in a list of directories given by the
variable sys.path. sys.path is initialized from these locations:
1) The directory containing the input script (or the current directory
when no file is specified).
2) PYTHONPATH (a list of directory names,
with the same syntax as the shell variable PATH).
3) The installation-dependent default.
python module import documentation
so you can cd to ~/documents/1Webdev/LPTHW and run python from there.
Added path to your module in sys.path
import sys
sys.path.append(r'~/documents/1Webdev/LPTHW')
import ex26_test
you need to open python in the same folder where the file is located because python can't see folders that aren't in the sys.path. There is of course way to add that folder temporary or permanently to sys.path that I will show in a moment
solution 1: move to that folder, use de cd (change directory) command to do it, that should look something similar to this:
$ cd ~/documents/1Webdev/LPTHW
$ ~/documents/1Webdev/LPTHW: python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ex26_test
>>>
note that if your python is correctly configured you should be able to call it be just typing python
solution 2: add that folder to sys.path to do it, open python and do the follow
>>> import sys
>>> sys.path.append("~/documents/1Webdev/LPTHW")
>>> import ex26_test
>>>
this is the temporary solution, but it will work regardless where you open python
solution 3: permanently add that folder to sys.path, to do this you need to modify/create the environ variable PYTHONPATH, I don't know how to do that in mac, but it should not be that difficult, in that regard some of those should be helpful: https://stackoverflow.com/search?q=pythonpath+on+mac in particular this looks like the correct one: How can I edit PYTHONPATH on a Mac?
Ok, it should be easy, but previous answers don't give all the solution. You can see where python find its modules using the sys.path function, but before you can use it, you have to import the module sys :
>>> import sys
>>> print(sys.path)
Next you have to add the folder where your module is with the commande path.append
>>> sys.path.append("~/documents/1Webdev/LPTHW")
And to load your module :
>>> from ex26_test import *
Now you can access all functions defined in your ex26_test.py file.
After asking this question, I installed environments for both python 2.7 and python 3.5: Easiest way to have Python 2 and 3 coexist on Mac OSX 10.8 with Anaconda installed
i also manually installed kernels for both python versions. If I start in my terminal the py35 environment (source activate py35) and then ipython notebook, i can choose a kernel that lets me use either python 2.7 or python 3.5.
Up to this point, everything is good.
However, when I run a check of all the modules I'm going to need, including ipython, numpy, scipy, pandas, scikitlearn, etc...using commands such as:
import pandas
print("Pandas version: %6.6s " % pandas.__version__)
the notebook crashes when it gets to pandas, even though I checked manually with conda list and pandas is indeed installed and updated to the last version.
What's interesting is that if i run the same checks using the python 2.7 kernel, everything runs smoothly, including the pandas command.
What's going on?
EDIT: Here's the kind of feedback I get upon crashing:
ValueError Traceback (most recent call last)
<ipython-input-2-3b71b0be8baa> in <module>()
12
13 # Pandas makes working with data tables easier
---> 14 import pandas
15 print("Pandas version: %6.6s (need at least 0.16.2)" % pandas.__version__)
16
then more of that...and at the very end:
ValueError: unknown locale: UTF-8
I've been having very serious issues installing Basemap from mpl_toolkits. Fortunately, it looks like I'm not the only one having these issues... I was wondering if someone could help me here.
I'm currently running OS X 10.10 (Yosemite).
Long story short, I was able to install Basemap via Anaconda after much difficulties. Now whenever I try to import Basemap, I get the following error:
28 from matplotlib.lines import Line2D
29 from matplotlib.transforms import Bbox
---> 30 from mpl_toolkits.basemap import pyproj
31 from mpl_toolkits.axes_grid1 import make_axes_locatable
32 from matplotlib.image import imread
ImportError: cannot import name pyproj
...which doesn't make sense to me at all. I have pyproj installed, and I physically see it in my Anaconda pkgs folder. It's called pyproj-1.9.3-0. I also have basemap-1.0.7-np19py27_0 in my pkgs folder.
Does anyone have any idea what's going on? Maybe I stupidly forgot to do something? Sorry if it's a dumb question - I'm an amateur when it comes to these sorts of things.
I just managed to install Basemap on Yosemite with Anaconda without any problems, so maybe there's something unusual about your set-up (what issues are you refering to?)
What I did was simply
conda install Basemap
But here are some things to think about:
Did you install anything after issuing this command (on my system, conda updated my matplotlib to 1.4.3 as part of this installation. You could try conda update, but you might need to do some fixing if you changed anything "by hand".
Are you using the right virtual environment (the one from which you installed Basemap)?
import Basemap with from mpl_toolkits.basemap import Basemap rather than the import Basemap that your question implies: see e.g. this example.
type which python and check that the output is along the lines of
/Users/<username>/anaconda/bin/python
or
/Users/<username>/anaconda/envs/<virtual-env>/bin/python
and that you're not using your Mac's system Python which would be something like
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python