Matplotlib and subinterpreter for embedded python in c++ - c++

I just added subinterpreter to my c++ embedded python editor to have a clean interpreter for each execution.
PyThreadState* tmpstate = Py_NewInterpreter();
PyThreadState_Swap(tmpstate);
... run the script ...
Py_EndInterpreter(tmpstate);
My own module are working and I tested numpy without having any problem. The problem is with matplotlib
if I run that the first time everything looks fine. The second time i get :
<class 'TypeError'>: attribute of type 'NoneType' is not callable: File "<string>", line 1, in <module>
File "C:\Python33\lib\site-packages\pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "C:\Python33\lib\site-packages\matplotlib\__init__.py", line 152, in <module>
from matplotlib.rcsetup import (defaultParams,
File "C:\Python33\lib\site-packages\matplotlib\rcsetup.py", line 19, in <module>
from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
File "C:\Python33\lib\site-packages\matplotlib\fontconfig_pattern.py", line 25, in <module>
from matplotlib.pyparsing_py3 import Literal, ZeroOrMore, \
File "C:\Python33\lib\site-packages\matplotlib\pyparsing_py3.py", line 3275, in <module>
_escapedHexChar = Combine( Suppress(_bslash + "0x") + Word(hexnums) ).setParseAction(lambda s,l,t:unichr(int(t[0],16)))
File "C:\Python33\lib\site-packages\matplotlib\pyparsing_py3.py", line 2961, in __init__
self.leaveWhitespace()
File "C:\Python33\lib\site-packages\matplotlib\pyparsing_py3.py", line 2587, in leaveWhitespace
self.expr = self.expr.copy()
File "C:\Python33\lib\site-packages\matplotlib\pyparsing_py3.py", line 705, in copy
cpy = copy.copy( self )
File "C:\Python33\lib\copy.py", line 89, in copy
rv = reductor(2)

This seem to be a bug in Python 3.3 : http://bugs.python.org/issue17408
The problem is :
The reason is in Objects/typeobject.c: import_copyreg() has a static cache of the copyreg module.
When the interpreter stops, the module is filled with None... but gets reused in the next instance.
Resetting this "mod_copyreg" variable to NULL fixes the issue.
pickle is also broken for the same reason.

Related

libcublas issue - tensorflow

I am very new to tensorflow, struggling on its installation. I use ubuntu 16.04. I obviously installed the Nvidia dependencies.
Whenever I type in:
import tensorflow as tf
I get:
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
builtins.execfile(filename, *where)
File "/media/as/KINGSTON/SIEC NEURONOWA/NETWORK.py", line 3, in <module>
from keras.models import Sequential
File "/usr/local/lib/python2.7/dist-packages/keras/__init__.py", line 3, in <module>
from . import activations
File "/usr/local/lib/python2.7/dist-packages/keras/activations.py", line 4, in <module>
from . import backend as K
File "/usr/local/lib/python2.7/dist-packages/keras/backend/__init__.py", line 73, in <module>
from .tensorflow_backend import *
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 1, in <module>
import tensorflow as tf
File "/home/as/.local/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
from tensorflow.python import *
File "/home/as/.local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 51, in <module>
from tensorflow.python import pywrap_tensorflow
File "/home/as/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 52, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/home/as/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/home/as/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/home/as/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: libcublas.so.8.0: cannot open shared object file: No such file or directory
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
I tried many solutions, but the effect remains the same. Many thanks for help!
I had this same problem when installing gputools in R. After hundreds of tries, the command which worked for me is:
sudo ln -s /usr/local/cuda/lib64/libcublas.so.8.0 /usr/lib/libcublas.so.8.0
OR
sudo ln -s /usr/local/cuda-8.0/lib64/libcublas.so.8.0 /usr/lib/libcublas.so.8.0

Python, Networked programs from a book by Dr Charles Severance

I am a beginner in programming, started from Python. I learn by Dr Charles Severance materials. So in his book there is an example:
import urllib
fhand = urllib.urlopen('http://www.py4inf.com/code/rom...
for line in fhand:
print line.strip()
When I copy paste it to Python 2 version (I use PyCharm 5.0.4) there appears:
Traceback (most recent call last):
File "D:/Python4yk/temprehg111.py", line 2, in <module>
fhand = urllib.urlopen('http://www.py4inf.com/code/rom...
File "C:\Python27\lib\urllib.py", line 87, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 208, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 292, in open_http
import httplib
File "C:\Python27\lib\httplib.py", line 79, in <module>
import mimetools
File "C:\Python27\lib\mimetools.py", line 6, in <module>
import tempfile
File "C:\Python27\lib\tempfile.py", line 35, in <module>
from random import Random as _Random
File "random.py", line 3, in <module>
integers
NameError: name 'line' is not defined
When I type another example, gets an error also. What is wrong? I don`t even write a program. I just copy paste an example. Asked Dr Chuck - still no answer.
Try this:
import urllib
fhand = urllib.urlopen('http://www.py4inf.com')
for line in fhand:
print line.strip() # notice the indentation

PyDev: Can't compile after accidentally naming file after Python io.py

So I without thinking stupidly named a file io.py in my working directory. When I tried to compile I got a traceback error. Having realised what I'd done I renamed my file and updated references to it but I still get the following error:
Traceback (most recent call last):
File "C:\Users\Tom\workspace\Converter\get_file.py", line 9, in <module>
from scipy import complex_
File "C:\Python27\lib\site-packages\scipy\__init__.py", line 70, in <module>
from numpy import show_config as show_numpy_config
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 185, in <module>
from . import add_newdocs
File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Python27\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Python27\lib\site-packages\numpy\core\__init__.py", line 46, in <module>
from numpy.testing import Tester
File "C:\Python27\lib\site-packages\numpy\testing\__init__.py", line 13, in <module>
from .utils import *
File "C:\Python27\lib\site-packages\numpy\testing\utils.py", line 15, in <module>
from tempfile import mkdtemp
File "C:\Python27\lib\tempfile.py", line 32, in <module>
import io as _io
File "C:\Users\Tom\workspace\Converter\io.py", line 1, in <module>
"""The io module provides the Python interfaces to stream handling. The
File "C:\Users\Tom\workspace\Converter\get_file.py", line 9, in <module>
from scipy import complex_
ImportError: cannot import name complex_
I have restarted PyDev to no avail. I have refreshed the interpreter to no avail.
I have followed the instructions found in this Python issue but that command returns the correct result on my machine.
Is tempfile.py a temporary file I can remove to resolve this issue?
I have found the solution. Stupidly simple but maybe worth leaving here for other newbies who fall into the same trap.
PyDev compiles the code into *.pyc files. When you update the references and rename the files these aren't immediately recompiled. Deleting the io.pyc file I had allowed compilation too run properly.
I found some more info on common pitfalls too.

py2exe no module named lgamma

I am trying to create an exe file for a python file. py2exe creates the executable, but when I run the executable, I get the following traceback:
C:\Users\gkumar7\Desktop\AL_gui-master\AL_gui-master\dist>app.exe
Traceback (most recent call last):
File "app.py", line 5, in <module>
File "learning_curve.pyc", line 13, in <module>
File "sklearn\metrics\__init__.pyc", line 34, in <module>
File "sklearn\metrics\scorer.pyc", line 30, in <module>
File "sklearn\metrics\cluster\__init__.pyc", line 8, in <module>
File "sklearn\metrics\cluster\supervised.pyc", line 18, in <module>
File "sklearn\metrics\cluster\expected_mutual_info_fast.pyc", line 12, in <mod
ule>
File "sklearn\metrics\cluster\expected_mutual_info_fast.pyc", line 10, in __lo
ad
File "expected_mutual_info_fast.pyx", line 1, in init sklearn.metrics.cluster.
expected_mutual_info_fast (sklearn\metrics\cluster\expected_mutual_info_fast.c:5
007)
ImportError: No module named lgamma
Here is my setup.py file:
dll_excludes = ['MSVCP90.dll', 'OLEAUT32.dll', 'USER32.dll', 'IMM32.dll', 'SHELL32.dll',
'ole32.dll', 'COMDLG32.dll', 'COMCTL32.dll', 'ADVAPI32.dll', 'mfc90.dll', 'msvcrt.dll',
'WS2_32.dll', 'WINSPOOL.DRV', 'GDI32.dll', 'VERSION.dll', 'KERNEL32.dll', 'ntdll.dll']
from distutils.core import setup
import py2exe, matplotlib, numpy
setup(console=['app.py'],
data_files=matplotlib.get_py2exe_datafiles(),
options = {"py2exe": {
"dll_excludes": dll_excludes,
"includes": ['scipy.sparse.csgraph._validation',
'scipy.special._ufuncs_cxx']
}
}, )
I have also tried cx_freeze, but kept receiving similar errors. Any help would be much appreciated.
Include sklearn.utils.lgamma in your py2exe setup file.

'Polygone' object does not support indexing

I am trying to render an SVG map using Kartograph.py. It throws me the TypeError. Here is the python code:
import kartograph
from kartograph import Kartograph
import sys
from kartograph.options import read_map_config
css = open("stylesheet.css").read()
K = Kartograph()
cfg = read_map_config(open("config.json"))
K.generate(cfg, outfile='dd.svg', format='svg', stylesheet=css)
Here is the error it throws
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
K.generate(cfg, outfile='dd.svg', format='svg', stylesheet=css)
File "C:\Python27\lib\site-packages\kartograph.py-0.6.8-py2.7.egg\kartograph\kartograph.py", line 46, in generate
_map = Map(opts, self.layerCache, format=format)
File "C:\Python27\lib\site-packages\kartograph.py-0.6.8-py2.7.egg\kartograph\map.py", line 61, in __init__
layer.get_features()
File "C:\Python27\lib\site-packages\kartograph.py-0.6.8-py2.7.egg\kartograph\maplayer.py", line 81, in get_features
charset=layer.options['charset']
File "C:\Python27\lib\site-packages\kartograph.py-0.6.8-py2.7.egg\kartograph\layersource\shplayer.py", line 121, in get_features
geom = shape2geometry(shp, ignore_holes=ignore_holes, min_area=min_area, bbox=bbox, proj=self.proj)
File "C:\Python27\lib\site-packages\kartograph.py-0.6.8-py2.7.egg\kartograph\layersource\shplayer.py", line 153, in shape2geometry
geom = shape2polygon(shp, ignore_holes=ignore_holes, min_area=min_area, proj=proj)
File "C:\Python27\lib\site-packages\kartograph.py-0.6.8-py2.7.egg\kartograph\layersource\shplayer.py", line 217, in shape2polygon
poly = MultiPolygon(polygons)
File "C:\Python27\lib\site-packages\shapely\geometry\multipolygon.py", line 74, in __init__
self._geom, self._ndim = geos_multipolygon_from_polygons(polygons)
File "C:\Python27\lib\site-packages\shapely\geometry\multipolygon.py", line 30, in geos_multipolygon_from_polygons
N = len(ob[0][0][0])
TypeError: 'Polygon' object does not support indexing
I had a look at shapely and it seems like you are using an outdated version.
Update your current install:
pip install -U shapely