Matplotlib pylab savefig runtime error in python 3.2.3 - python-3.x

I am trying to test whether matplotlib installed properly in python3. I am following this basic tutorial: http://www.scipy.org/Plotting_Tutorial
This question is similar to one asked previously but unfortunately did not report the final solution explicitly and this is a different operating system.
The operating system for this is Mac OS X 10.6.8. Running the script in Python 3.2 does not work, but running it in Python 2.7 does. Unfortunately I need 3.2.
When the savefig line is commented out, it works fine.
"""
Example: simple line plot.
Show how to make and save a simple line plot with labels, title and grid
"""
import numpy
import pylab
t = numpy.arange(0.0, 1.0+0.01, 0.01)
s = numpy.cos(2*2*numpy.pi*t)
pylab.plot(t, s)
pylab.xlabel('time (s)')
pylab.ylabel('voltage (mV)')
pylab.title('About as simple as it gets, folks')
pylab.grid(True)
pylab.savefig('/Users/USERNAME/Documents/simple_plot.png', format='png')
pylab.show()
The code above returned the same error regardless of whether the path was set explicitly. I tried the following in addition to the above:
pylab.savefig('simple_plot.png', format='png')
pylab.savefig('simple_plot')
I tried setting the path explicitly as the other question said (as in the longer example above), but that resulted in the following errors. Note: mplex.py is the name of the script.
libpng warning: Application was compiled with png.h from libpng-1.2.44
libpng warning: Application is running with png.c from libpng-1.4.11
libpng warning: Incompatible libpng version in application and library
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mplex.py", line 16, in <module>
pylab.savefig('/Users/USERNAME/Documents/simple_plot.png', format='png')
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/pyplot.py", line 474, in savefig
return fig.savefig(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/figure.py", line 1225, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/backend_bases.py", line 2075, in print_figure
**kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/backend_bases.py", line 1846, in print_png
return agg.print_png(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/matplotlib/backends/backend_agg.py", line 497, in print_png
filename_or_obj, self.figure.dpi)
RuntimeError: Could not create write struct
Ideas? Is there another way to explicitly set the destination aside from what I tried in the savefig line?

I'm not sure what package manager you're using, and I'm on Python 2.7 (not 3.2.3), but I'm using Fink and had a similar problem (with different libpng version numbers).
In iPython --pylab, I was seeing this:
In [3]: savefig('foo.png')
libpng warning: Application is running with png.c from libpng-1.4.11
libpng warning: Incompatible libpng version in application and library
libpng warning: Application was compiled with png.h from libpng-1.5.11
Checking my Fink installations of libpng, I saw this:
$ fink list libpng
Information about 6268 packages read in 0 seconds.
i libpng14 1.4.11-1 PNG image format handling library
i libpng14-shlibs 1.4.11-1 Shared libraries for libpng14 package
libpng15 1.5.10-1 PNG image format handling library
libpng15-32bit 1.5.10-1 PNG library (32-bit)
libpng15-32bit-shli 1.5.10-1 Shared libraries for libpng15 package
i libpng15-shlibs 1.5.10-1 Shared libraries for libpng15 package
libpng3 1:1.2.49-1 PNG image format handling library
libpng3-shlibs 1:1.2.49-1 Shared libraries for libpng3 package
i system-pkgconfig-li 1.5.11-1 [virtual pkgconfig package representing libpng]
i system-pkgconfig-li 1.5.11-1 [virtual pkgconfig package representing libpng15]
After running "fink install libpng15", my libpng package versions looked like this:
$ fink list libpng
Information about 6268 packages read in 1 seconds.
libpng14 1.4.11-1 PNG image format handling library
i libpng14-shlibs 1.4.11-1 Shared libraries for libpng14 package
i libpng15 1.5.10-1 PNG image format handling library
libpng15-32bit 1.5.10-1 PNG library (32-bit)
libpng15-32bit-shli 1.5.10-1 Shared libraries for libpng15 package
i libpng15-shlibs 1.5.10-1 Shared libraries for libpng15 package
libpng3 1:1.2.49-1 PNG image format handling library
libpng3-shlibs 1:1.2.49-1 Shared libraries for libpng3 package
i system-pkgconfig-li 1.5.11-1 [virtual pkgconfig package representing libpng]
i system-pkgconfig-li 1.5.11-1 [virtual pkgconfig package representing libpng15]
As you can see, libpng14 is no longer installed and libpng15 is installed.
Next, "pip uninstall matplotlib" and "pip install matplotlib" yields a matplotlib build summary like this:
$ pip install matplotlib
Downloading/unpacking matplotlib
Downloading matplotlib-1.2.0.tar.gz (36.9Mb): 36.9Mb downloaded
Running setup.py egg_info for package matplotlib
basedirlist is: ['/usr/local/', '/usr', '/usr/X11', '/opt/local']
============================================================================
BUILDING MATPLOTLIB
matplotlib: 1.2.0
python: 2.7.3 (default, Jul 27 2012, 16:40:57) [GCC 4.2.1
Compatible Apple Clang 3.1
(tags/Apple/clang-318.0.61)]
platform: darwin
REQUIRED DEPENDENCIES
numpy: 1.6.2
freetype2: 15.0.9
OPTIONAL BACKEND DEPENDENCIES
libpng: 1.5.10
Tkinter: Tkinter: 81008, Tk: 8.5, Tcl: 8.5
Gtk+: no
* Building for Gtk+ requires pygtk; you must be able
* to "import gtk" in your build/install environment
Mac OS X native: yes
Qt: no
Qt4: no
PySide: no
Cairo: no
OPTIONAL DATE/TIMEZONE DEPENDENCIES
dateutil: 1.5
pytz: 2012b
OPTIONAL USETEX DEPENDENCIES
dvipng: no
ghostscript: 9.05
latex: no
You can see that the build will now pick up libpng 1.5.10. Previously, it was picking up 1.4.11 for the build.

Avoid using the standard console and the error will not appear by double clicking the yourcode.py or yourcode.pyw file.

Related

How to solve the import cv problem in vs studio?

File "c:\Users\csany\Documents\PYTHON_BEGINEERS\read.py", line 1, in
import cv2 as cv
File "C:\Users\csany\AppData\Local\Programs\Python\Python39\lib\site-packages\cv2_init_.py", line 5, in
from .cv2 import *
ImportError: DLL load failed while importing cv2: The specified module could not be found.
Every time I try to run the code i get this error. I have installed OpenCV module through its source I have changed its path yet it still won't work in vs studio for python. Please help me out
You can download the latest OpenCV 3.2.0 for Python 3.6 on Windows 32-bit or 64-bit machine, look for file starts withopencv_python‑3.2.0‑cp36‑cp36m, from this unofficial site. Then type below command to install it:
pip install opencv_python‑3.2.0‑cp36‑cp36m‑win32.whl (32-bit version)
pip install opencv_python‑3.2.0‑cp36‑cp36m‑win_amd64.whl (64-bit version)
I think it would be easier.
Update on 2017-09-15:
OpenCV 3.3.0 wheel files are now available in the unofficial site and replaced OpenCV 3.2.0.
Update on 2018-02-15:
OpenCV 3.4.0 wheel files are now available in the unofficial site and replaced OpenCV 3.3.0.
Update on 2018-06-19:
OpenCV 3.4.1 wheel files are now available in the unofficial site with CPython 3.5/3.6/3.7 support, and replaced OpenCV 3.4.0.
Update on 2018-10-03:
OpenCV 3.4.3 wheel files are now available in the unofficial site with CPython 3.5/3.6/3.7 support, and replaced OpenCV 3.4.1.
Update on 2019-01-30:
OpenCV 4.0.1 wheel files are now available in the unofficial site with CPython 3.5/3.6/3.7 support.
Update on 2019-06-10:
OpenCV 3.4.6 and OpenCV 4.1.0 wheel files are now available in the unofficial site with CPython 3.5/3.6/3.7 support.
src : https://stackoverflow.com/a/43190144/13319197

How to have virtualbox python bindings working with chosen Python version?

I'm using Python3 bindings for virtualbox (pyvbox). It works with the initial python3 version of my system which is 3.7. Now, I would like to use the very same bindings with python3.8. I have installed all needed python3.8 packages, but i get this error:
$ python3
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import virtualbox
>>> _vbox = virtualbox.VirtualBox()
m=VBoxPython3_8 x=No module named 'VBoxPython3_8'
m=VBoxPython3 x=No module named 'VBoxPython3'
m=VBoxPython x=/usr/lib/virtualbox/VBoxPython.so: undefined symbol: _Py_ZeroStruct
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home//.local/lib/python3.8/site-packages/virtualbox/library_ext/vbox.py", line 22, in __init__
manager = virtualbox.Manager()
File "/home//.local/lib/python3.8/site-packages/virtualbox/__init__.py", line 145, in __init__
self.manager = vboxapi.VirtualBoxManager(mtype, mparams)
File "/usr/local/lib/python3.8/dist-packages/vboxapi/__init__.py", line 989, in __init__
self.platform = PlatformXPCOM(dPlatformParams)
File "/usr/local/lib/python3.8/dist-packages/vboxapi/__init__.py", line 750, in __init__
import xpcom.vboxxpcom
File "/usr/lib/virtualbox/sdk/bindings/xpcom/python/xpcom/vboxxpcom.py", line 78, in <module>
raise Exception('Cannot find VBoxPython module (tried: %s)' % (', '.join(_asVBoxPythons),))
Exception: Cannot find VBoxPython module (tried: VBoxPython3_8, VBoxPython3, VBoxPython)
I have found that these modules have to be in /usr/lib/virtualbox, which currently contains only these files:
$ ls /usr/lib/virtualbox/ | grep VBoxPython
VBoxPython2_7.so
VBoxPython3_7m.so
VBoxPython.so
These .so files are installed via the virtualbox package. However, I cannot manage to have neither VBoxPython3.so nor VBoxPython3.8.so by reinstalling. I have looked at the virtualbox code and it seems that the c macro PY_VERSION_HEX indicates 3.7 instead of 3.8, which would be caused by the wrong Python.h header being included.
How can I force virtualbox package to include /usr/include/python3.8/Python.h instead of /usr/include/python3.7/Python.h ?
Uninstalling python3.7 is not an option.
The pyvbox package docs (redirected from pyvbox) imply that the .so files aren't installed by it. But, they do note that the Python package is dependent on the VirtualBox SDK. Which, in turn, recommend installing the SDK to the system Python manually. That latest seems irky, especially when done outside a package manager but maybe they know better.
Digging around, I found the .so files are owned by the OS package virtualbox. In other words, virtualbox must be installed with Python3.8 support.
# Who owns/provides this file (use your dist equivalent or duckduckgo)
$ yay -Fy /usr/lib/virtualbox/VBoxPython3_8.so
...
usr/lib/virtualbox/VBoxPython3_8.so is owned by community/virtualbox 6.1.6-1
I suggest you reinstall the virtualbox package for your distribution. Installing the latest version will provide the latest .so library files Virtualbox supports.
Let's test the theory
# Install Virtualbox and the Virtualbox SDK
yay -S virtualbox virtualbox-sdk python-virtualbox
# Assert we aren't getting an error
$ python -c "import virtualbox; _vbox = virtualbox.VirtualBox(); print('Success!')"
Success!
Otherwise, if the package doesn't include 3.8 support, you'll have to compile it yourself while making sure LD_LIBRARY_PATH points to your Python 3.8 dev libraries. But, I strongly recommend against this. It is infinitely better to work along with your package manager and benefit from upstream work.
Good luck!

Why do I get an ImportError for opencv2 when I run on python3.7.3?

I'm trying to import opencv2 using python3.7.3 on Mac OS 10.10 but an ImportError occurs. How should I solve it?
I tried to install opencv3 using pip, pip3, and homebrew. Probably not install right.
import cv2
import numpy as np
import sys
import pytesseract as py
import matplotlib.pyplot as plt
Error
Traceback (most recent call last):
File "/Users/wujian/Desktop/Project/Proj.py", line 1, in <module>
import cv2
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/cv2.cpython-37m-darwin.so, 2): Symbol not found: _clock_gettime
Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/.dylibs/libavutil.56.22.100.dylib (which was built for Mac OS X 10.12)
Expected in: /usr/lib/libSystem.B.dylib
in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/.dylibs/libavutil.56.22.100.dylib
The issue seems to be (oddly) related to your copy of libavutil which is part of ffmpeg.
It's hinted at by this part of the error message:
ImportError: dlopen(/.../cv2/cv2.cpython-37m-darwin.so, 2): Symbol not found: _clock_gettime
Referenced from: /...python3.7/site-packages/cv2/.dylibs/libavutil.56.22.100.dylib (which was built for Mac OS X 10.12)
Installing OpenCV requires ffmpeg. If you installed it via homebrew, it's listed as a dependency.
$ brew info opencv
...
==> Dependencies
Build: cmake ✓, pkg-config ✓
Required: eigen ✓, ffmpeg ✓, glog ✓, ...
Installing ffmpeg will include the libavutil library, which can be checked by downloading pre-built shared libraries or by Homebrew:
$ brew list ffmpeg | grep libavutil.*dylib
/usr/local/Cellar/ffmpeg/4.1.4_1/lib/libavutil.56.22.100.dylib
/usr/local/Cellar/ffmpeg/4.1.4_1/lib/libavutil.56.dylib
/usr/local/Cellar/ffmpeg/4.1.4_1/lib/libavutil.dylib
Now, your problem is that the libavutil (and ffmpeg) on your system is not backwards-compatible with your Mac 10.10. It was built targeting a newer Mac 10.12, which seems to have introduced changes to the clock_gettime API, causing the "Symbol not found" error. This has also been reported to the opencv-python issues:
This is not related to OpenCV. The error comes from libavutil
(=FFmpeg) which is installed via homebrew. The error is related to
binary backward compatibility (apparently Apple has changed the
libsystem interface). I'm not too familiar with Apples ecosystem but
building FFmpeg against earlier OS version might fix the issue if the
older symbols are present also in the latest OS versions. Full
backward compatibility on macOS might be impossible to achieve.
I was just bitten by this issue the hard way deep in nested
dependencies; FFmpeg targets 10.12 but I'm on 10.11. Hoping for a
fixed release soon.
Note that while downgrading Xcode may work too, the proper way to
target an earlier version of macOS is to add e.g.
-mmacosx-version-min=10.11 or -mmacosx-version-min=10.6 to CFLAGS/CXXFLAGS/LDFLAGS; this will tune headers, compiler and linker
to produce a binary that is compatible with at least that version of
macOS.
The only solution it seems, is to install an older version of ffmpeg (with libavutil) that is compatible with your Mac 10.10, and then tell OpenCV to link to that older version. You'll probably need to also build OpenCV from source (see Configure and Build OpenCV to Custom FFMPEG Install).
You can also try checking this post over at SuperUser:
Which ffmpeg package I should download for macOS?
Try installing the older ffmpeg#2.8 from Homebrew (though I'm not sure if that's going to work, I don't have a Mac 10.10 to test it on).
Try downloading pre-built static/shared builds from ffmpeg.org.
Try building it from source (see CompilationGuide/macOS)

Can't install lxml with Python3.5, Windows 10, 32 bit

Python 3.5 on Windows 10, 32-bit box; all I want to do is run this:
import quandl
import pandas as pd
import html5lib
import lxml
# retrieve web page with list of 50 states
fiddy_states = pd.read_html('https://simple.wikipedia.or /wiki/List_of_U.S._states')
But for the life of me I can't seem to get a properly installed lxml, which is required by pd.read_html. Following advice from several online sources I have MinGW installed in my system and I have also added the following to C:\Python35-32\Lib\distutils\distutils.cfg:
[build]
compiler=mingw32
I have MinGW installed and included in PATH. I have tried installing lxml using both pip3 as well as the binaries found at Unofficial Windows Binaries for Python Extension Packages.
Here's all installed packages:
['beautifulsoup4==4.4.1', 'cffi==1.6.0', 'cryptography==1.3.2', 'cycler==0.10.0', 'cython==0.24', 'html5lib==0.9999999', 'idna==2.1', 'inflection==0.3.1', 'lxml==3.4.4', 'matplotlib==1.5.1', 'more-itertools==2.2', 'ndg-httpsclient==0.4.0', 'numpy==1.11.0', 'pandas-datareader==0.2.1', 'pandas==0.18.1', 'pip==8.1.2', 'pyasn1==0.1.9', 'pycparser==2.14', 'pyopenssl==16.0.0', 'pyparsing==2.1.4', 'python-dateutil==2.5.3', 'pytz==2016.4', 'quandl==3.0.1', 'requests-file==1.4', 'requests==2.10.0', 'scikit-learn==0.17.1', 'setuptools==18.2', 'six==1.10.0']
As shown above, lxml==3.4.4 appears to be installed, however when I try to run the line containing pd.read_html I get the following error message:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Jose Manuel\AppData\Local\Programs\Python\Python35-32 \lib\site-packages\pandas\io\html.py", line 874, in read_html
parse_dates, tupleize_cols, thousands, attrs, encoding)
File "C:\Users\Jose Manuel\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pandas\io\html.py", line 726, in _parse
parser = _parser_dispatch(flav)
File "C:\Users\Jose Manuel\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pandas\io\html.py", line 685, in _parser_dispatch
raise ImportError("lxml not found, please install it")
ImportError: lxml not found, please install itenter code here
Your help is very much appreciated
I have been struggling with this today. I found, elsewhere on stackoverflow.com, this two-part and quick solution, which resulted in python no longer complaining when I tried to use lxml:
go to this repository and download a version which matches your Python installation (the version number, and 32- vs 64-bit. I use Python 3.5.1 64-bit, installed on Windows 10, so on that page, I chose lxml-3.6.0-cp35-cp35m-win_amd64.whl. You say you have 32-bit Python, so use a version that matches that (like lxml-3.6.0-cp35-cp35m-win32.whl.
My download directory is d:\Downloads. Python must be in your PATH environment variable for the next step to work. Use a command like the following, changing "D:\Downloads" to the pathname to your download directory. Then, at a DOS prompt, type:
python -m pip install "D:\Downloads\lxml-3.6.0-cp35-cp35m-win_amd64.whl" lxml-3.6.0-cp35-cp35m-win_amd64.whl

How can i use matplotlib's plot-directive with python-3 in ReadTheDocs?

I'm having a python-3 project that uses the plot-directive to generate and embed matplotlib's diagrams on the fly, and i'm using ReadTheDocs for auto-generating the project's documentation.
The plot-directive indeed works ok in python-2, but it currently fails in python-3.
Specifically the failure i'm getting on the logs of RTD is this:
Build Standard Error
html
-----
Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/wltp/envs/master/lib/python3.4/site-packages/sphinx/application.py", line 325, in setup_extension
mod = __import__(extension, None, None, ['setup'])
ImportError: No module named 'matplotlib'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/wltp/envs/master/lib/python3.4/site-packages/sphinx/cmdline.py", line 253, in main
warningiserror, tags, verbosity, parallel)
File "/home/docs/checkouts/readthedocs.org/user_builds/wltp/envs/master/lib/python3.4/site-packages/sphinx/application.py", line 119, in __init__
self.setup_extension(extension)
File "/home/docs/checkouts/readthedocs.org/user_builds/wltp/envs/master/lib/python3.4/site-packages/sphinx/application.py", line 328, in setup_extension
err)
sphinx.errors.ExtensionError: Could not import extension matplotlib.sphinxext.plot_directive (exception: No module named 'matplotlib')
Extension error:
Could not import extension matplotlib.sphinxext.plot_directive (exception: No module named 'matplotlib')
And the culprit can be traced to matplotlib not being compiled due to mnissing 'freetype' C lib:
Setup Output
...
requirements
-----
...
BUILDING MATPLOTLIB
matplotlib: yes [1.4.2]
python: yes [3.4.0 (default, Apr 11 2014, 13:05:11) [GCC
4.8.2]]
platform: yes [linux]
REQUIRED DEPENDENCIES AND EXTENSIONS
numpy: yes [not found. pip may install it below.]
six: yes [six was not found.]
dateutil: yes [dateutil was not found. It is required for date
axis support. pip/easy_install may attempt to
install it after matplotlib.]
pytz: yes [pytz was not found. pip will attempt to install
it after matplotlib.]
tornado: yes [tornado was not found. It is required for the
WebAgg backend. pip/easy_install may attempt to
install it after matplotlib.]
pyparsing: yes [pyparsing was not found. It is required for
mathtext support. pip/easy_install may attempt to
install it after matplotlib.]
pycxx: yes [Official versions of PyCXX are not compatible
with matplotlib on Python 3.x, since they lack
support for the buffer object. Using local copy]
libagg: yes [pkg-config information for 'libagg' could not
be found. Using local copy.]
freetype: no [The C/C++ header for freetype2 (ft2build.h)
could not be found. You may need to install the
development package.]
OPTIONAL LATEX DEPENDENCIES
dvipng: yes [version 1.14]
ghostscript: yes [version 9.10]
latex: yes [version 3.1415926]
pdftops: no
============================================================================
* The following required packages can not be built:
* freetype
To make the doc-generation pass, I'm forced to "disable" plot-directive by mocking it out, as instructed in the RTD FAQ, using the following code in the ./conf.py file.
I've tried with various combinations of virtualenv (with or without site-package visibility), rtd-specific requirements.txt, with no success.
Has anybody found a way to do it?
These are some hints for those willing to dig further into the issue:
The "official" list of pre-installed native libraries on RTD
The actual pip-requirements.txt file used to setup the build-server, as found from RTD's sources.
Since today, the problem is officially solved, according to rtfd issue #896.
All required dependencies (matplotlib, scipy and numpy) are also installed for python-3, so mocking is not necessary anymore.
To make use of it, in the Advanced settings make the following choices:
Check: Install Project: Install your project inside a virtualenv using setup.py install
Select: Python interpreter: CPython 3.x
Check: Use system packages: Give the virtual environment access to the global site-packages dir.
...although with a little training, it is easy to write python-2/3 compatible code.
I have this same problem and it seems to have to do with the RTD interpreter. My project is in Python 3, and I selected as much in RTD Settings and it fails (can't find matplotlib). I was able to work around it by using selecting the Python 2.x interpreter (in Advanced Settings; also allow the global site packages), as opposed to Python 3.x. I use a RTD-specific requirements file (which only has what I need beyond the global site packages: sphinxcontrib-napoleon). Matplotlib is now seen, but now I have issues from Python 2 backwards compatibility. But at least that issue is sorta solved.

Resources