Julia PyPlot not working [Conda, Python issues] - python

This is the error I get when I run my program. I am using Ubuntu 16.04 and had Python 2.7.12 installed. I installed anaconda hoping that would help solve the problem but it didn't. Now python -V gives me Python 2.7.14 :: Anaconda, Inc.
When I run Conda.list() inside julia, I do get "matplotlib" and "basemap"
(includes mpl_toolkits, I think) and when I run conda list outside julia (bash?) I get a longer list which also contains "matplotlib" and "basemap".
sys:1: UserWarning: This call to matplotlib.use() has no effect because the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.
The backend was *originally* set to u'Qt5Agg' by the following code: File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 14, in <module>
line for line in traceback.format_stack()
WARNING: No working GUI backend found for matplotlib WARNING: Couldn't initialize pyplot. (might need to install it?) INFO: To do a standard install of pyplot, copy and run this:
if !Plots.is_installed("PyPlot")
Pkg.add("PyPlot") end withenv("PYTHON" => "") do
Pkg.build("PyPlot") end
# now restart julia!
ERROR: LoadError: InitError: PyError (ccall(#pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)
The Python package matplotlib.pyplot could not be found by pyimport. Usually this means that you did not install matplotlib.pyplot in the Python version being used by PyCall.
PyCall is currently configured to use the Python version at:
/home/myusername/anaconda2/bin/python
and you should use whatever mechanism you usually use (apt-get, pip, conda, etcetera) to install the Python package containing the matplotlib.pyplot module.
One alternative is to re-configure PyCall to use a different Python version on your system: set ENV["PYTHON"] to the path/name of the python executable you want to use, run Pkg.build("PyCall"), and re-launch Julia.
Another alternative is to configure PyCall to use a Julia-specific Python distribution via the Conda.jl package (which installs a private Anaconda Python distribution), which has the advantage that packages can be installed and kept up-to-date via Julia. As explained in the PyCall documentation, set ENV["PYTHON"]="", run Pkg.build("PyCall"), and re-launch Julia. Then, To install the matplotlib.pyplot module, you can use `pyimport_conda("matplotlib.pyplot", PKG)`, where PKG is the Anaconda package the contains the module matplotlib.pyplot, or alternatively you can use the Conda package directly (via `using Conda` followed by `Conda.add` etcetera).
) <type 'exceptions.ImportError'> ImportError("/lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/../../.././libpng16.so.16)",) File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 29, in <module>
import matplotlib.colorbar File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
import matplotlib.contour as contour File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/contour.py", line 21, in <module>
import matplotlib.font_manager as font_manager File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/font_manager.py", line 58, in <module>
from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
Stacktrace: [1] pyerr_check at /home/myusername/.julia/v0.6/PyCall/src/exception.jl:56 [inlined]....

LD_LIBRARY_PATH="/home/myusername/.julia/v0.6/Conda/deps/usr/lib/" julia
did it!
Thanks Github!

I am also using Ubuntu 16.04 and use pyplot with julia. I installed matplotlib with pip and use it with the TK backend (the most light-weight and easy to install backend). Please try these shell commands to install python-tk and matplotlib in your home directory:
sudo apt-get install python-tk
unset PYTHON # to use the default python interpreter /usr/bin/python
pip2 install --user --upgrade matplotlib
julia --eval 'Pkg.build("PyCall")'
julia --eval 'Pkg.build("PyPlot")'
To use the default python interpreter, make sure that you do not set the environment variable PYTHON. The following should return nothing:
julia --eval '#show get(ENV,"PYTHON",nothing)'
If PyCall are PyPlot are not already installed, then use add instead of build:
julia --eval 'Pkg.add("PyCall")'
julia --eval 'Pkg.add("PyPlot")'

Related

Python package dependency is causing an error

The folder Python37\site-packages\scipy\sparse\linalg\isolve\ inside Scipy package in Python has the following file: _iterative.cp37-win_amd64.pyd.
When I run a Python file, I get the following error :-
File "C:\......\Python37\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 10, in <module>
from . import _iterative
ImportError: DLL load failed: The specified module could not be found
(I am new to Python on Windows)
As I cannot comment due to a lack of reputation:
Have you installed scypy with all the mentioned dependencies on their website?
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
(Taken from https://www.scipy.org/install.html)
This is how I was able to resolve the issue (on Windows) :-
1. I uninstalled Python using python.exe program.
2. Installed anaconda for Windows - it installs python along with dependencies and many common libraries.

PyError using PyCall in Julia

I am using PyCall to call Python in Julia.
My environment is set to my Anaconda distribution (xxx in the following) and PyCall builds correctly.
If I use
using PyCall
np = pyimport("numpy")
I get
ERROR: LoadError: PyError (PyImport_ImportModule
The Python package numpy could not be found by pyimport. Usually this means
that you did not install numpy in the Python version being used by PyCall.
PyCall is currently configured to use the Python version at:
xxx\Anaconda3\python.exe
and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the numpy module.
One alternative is to re-configure PyCall to use a different Python
version on your system: set ENV["PYTHON"] to the path/name of the python
executable you want to use, run Pkg.build("PyCall"), and re-launch Julia.
Another alternative is to configure PyCall to use a Julia-specific Python
distribution via the Conda.jl package (which installs a private Anaconda
Python distribution), which has the advantage that packages can be installed
and kept up-to-date via Julia. As explained in the PyCall documentation,
set ENV["PYTHON"]="", run Pkg.build("PyCall"), and re-launch Julia. Then,
To install the numpy module, you can use `pyimport_conda("numpy", PKG)`,
where PKG is the Anaconda package the contains the module numpy,
or alternatively you can use the Conda package directly (via
`using Conda` followed by `Conda.add` etcetera).
) <class 'ImportError'>
ImportError('DLL load failed: Das angegebene Modul wurde nicht gefunden.')
File "xxx\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "xxx\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
from . import _mklinit
Despite numpy is installed in the named directory.

using netcdf without installation

I need to use netcdf but do not have install permission for python modules. I have downloaded netcdf-0.1.2.tar.gz from here: https://pypi.python.org/simple/netcdf/ and unzipped the tar ball. I have been following this stack overflow post in an attempt to use the module but have had no luck so far:
(Python) Use a library locally instead of installing it
here is what I have tried:
Installing virtualenv:
I do not have permission to do this
python setup.py install -- user:
again, I don't have permission
running my script with netcdf as my current working directory:
I tried this as well, here are the issues I have run into:
first I went into netcdf-0.1.2 and made a new file called asdf.py
which contains the following:
import netcdf
print("testing")
running python asdf.py gives the following error:
Traceback (most recent call last):
File "asdf.py", line 1, in <module>
import netcdf
File "/.../Downloads/netcdf-0.1.2/netcdf/__init__.py", line 1, in <module>
from netcdf import *
File "/.../Downloads/netcdf-0.1.2/netcdf/netcdf.py", line 1, in <module>
from netCDF4 import Dataset, numpy
ImportError: No module named netCDF4
I'm not sure how to fix this error, any help would be greatly appreciated
in case this is somehow relevant, the version of Linux I am using is 3.2.0-23-generic
also I have numpy installed already
Easest would be to install Anaconda or Miniconda with your user rights.
Anaconda already as netCDF4installed. In case of Miniconda install with:
conda install netcdf4
If you have Python 3 installed, then you will have the venv package in the standard library, so you do not need "virtualenv" to be installed for you separately (as would be the case with Python 2). Instead use python3 -mvenv , in a similar way to how you would use virtualenv, for example:
python3 -mvenv /path/to/my_venv
or to include any non-standard packages already installed on the system:
python3 -mvenv --system-site-packages /path/to/my_venv
After that, you should be able to activate the environment and pip install packages, e.g.
source /path/to/my_venv/bin/activate # for csh use activate.csh instead
pip install netCDF4
Remember to source the activate script at run time as well as installation time:
source /path/to/my_venv/bin/activate
python
and you should then find that in your python session you have the netCDF4 package available, e.g.
import netCDF4
my_dataset = netCDF4.Dataset('myfile.nc')
Of course, substitute the actual path in place of /path/to/my_venv above.
None of this requires any root privileges.
(And as someone else has suggested, another option for you is to use conda.)
I'd also like to highlight that the package is imported using capitals
import netCDF4 as nc
This might not matter on a mac, but for Windows it is key.

Gtk* backend requires pygtk to be installed

From within a virtual environment, trying to load a script which uses matplotlib's GTKAgg backend, I fail with the following traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 97, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 16, in <module>
raise ImportError("Gtk* backend requires pygtk to be installed.")
ImportError: Gtk* backend requires pygtk to be installed.
The code which I ran in order to produce that ImportError is as follows:
import matplotlib as mpl
mpl.use('GTKAgg')
import matplotlib.pyplot as plt
When running the very same code after deactivating my virtual environment, everything goes well.
I assumed this may be due to version differences; indeed, such differences exist on my machine. However, the version in the virtual environment is newer (1.2.0 versus 1.1.1rc), so I am not expecting less support.
In case it is not clear: my question is how to allow importing pyplot with GTKAgg backend on a new version of matplotlib, or at least an attempt to understand the reasons for this import failure.
You probably created your virtual evn by something like:
$ virtualenv ~/.virtualenvs/my_env
by default this can see none of your system-installed packages (including pygtk) so when you try to run mpl it rightly complains that you do not have pygtk installed because (with in the context of the virtualenv) you do not.
You can either build and install pygtk within your virtualenv or you can use
$ virtualenv --system-site-packages ~/.virtualenvs/my_env
(doc) which will make your virtualenv inherit from your global packages.
I'm not sure if you're on Ubuntu, but to solve this I had to install matplotlib from source to get this to work. The key instructions (from http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/) are:
$ workon plotting
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install
Changing backends and using --system-site-packages didn't work for me.

ImportError: No module named _backend_gdk

I am starting to get some insight into interactive plotting with python and matplotlib using pyGTK+. Therefore I took a look at the example given at the matplotlib website.
This is a short exerpt of the Code:
#!/usr/bin/env python
"""
Example of embedding matplotlib in an application and interacting with
a treeview to store data. Double click on an entry to update plot
data
"""
import pygtk
pygtk.require('2.0')
import gtk
from gtk import gdk
import matplotlib
matplotlib.use('GTKAgg') # or 'GTK'
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
from numpy.random import random
from matplotlib.figure import Figure
Ones I try to run this Script in the Terminal I get the following error:
Traceback (most recent call last):
File "gtk_spreadsheet.py", line 15, in <module>
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 33, in <module>
from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gdk.py", line 29, in <module>
from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array
ImportError: No module named _backend_gdk
I have python 2.7 and pygtk 2.24 installed.
Can anyone figure out where the error is located? I think it might be connected to some linking issues?
Note that the Debian/Ubuntu package you need is not 'pygtk2-devel' but 'python-gtk2-dev':
sudo apt-get install python-gtk2-dev
should fix this error on these platforms.
This was a symptom of using a pip-installed matplotlib instead of an apt-installed matplotlib on my system, just now. If on Ubuntu/Debian, try:
pip uninstall matplotlib
apt install python-matplotlib
I believe what was happening is that the pip-install didn't build the C extension required for GTK output, but the apt package has the extension prebuilt. I don't have logs from the initial pip install of matplotlib, so I can't confirm that that's what happened.
In addition to Haldean Brown's answer, note that if you really need to use pip you can force it to recompile matplotlib locally and get "the deep magic that setup.py does" with the --no-binary option:
pip uninstall matplotlib
pip install matplotlib --no-binary=matplotlib
This will solve your problem, provided that you already installed gtk2 with sudo apt-get install python-gtk2-dev
As you want to use the GTKAgg backend, Using pip may prove useful in the future to freeze matplotlib at a version where it is supported (the deprecation warning states it will be dropped in 3.0):
pip install matplotlib==2.2.2 --no-binary=matplotlib
Ubuntu 18.04 has a broken matplotlib package. See this bug: https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1785458
You can either compile matplotlib yourself (e.g. with pip) or use the PPA linked from the bug report.
if you are running py2.7 applications, try:
pip2 install matplotlib --no-binary=matplotlib

Categories