I'm trying to create a program who will :
Retrieve data from a csv file chosen by the user
Plot the data
Display the figure on the window
Allow the user to change the plot parameters (color, linestyle ...)
Save the plot in a directory chosen by the user in the PS format
Steps 1 to 4 are working just fine, but step 5 makes me feel like I've killed matplotlib ...
I'm using the same syntax I used on a previous version for saving, which worked fine :
def savePlot(self):
self.saveDirectory = QFileDialog.getExistingDirectory(None, 'Chose where you want to save','/home',QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
if self.plotParam==[]:
plt.savefig(self.saveDirectory + "/" + "Untiltled.ps", dpi = 1200)
else :
plt.savefig(self.saveDirectory + "/" + "%s.ps" %self.plotParam[0],format = 'ps', dpi = 1200)
Here is a link to the full code, and here is the data file I'm using.
In this portion, self.plotParam is a list which is initially empty and contains the plot parameters if the user wants to change the default ones.
Here is the error log :
Traceback (most recent call last):
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 141, in to_rgba
rgba = _colors_full_map.cache[c, alpha]
TypeError: unhashable type: 'numpy.ndarray'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/antoine/CERB/courbes/courbesv2.py", line 354, in savePlot
plt.savefig(self.saveDirectory + "/" + "essai.ps", dpi = 1200)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 697, in savefig
res = fig.savefig(*args, **kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/figure.py", line 1573, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 222, in print_figure
FigureCanvasAgg.print_figure(self, *args, **kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2252, in print_figure
**kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 957, in print_ps
return self._print_ps(outfile, 'ps', *args, **kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 988, in _print_ps
**kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 1078, in _print_figure
self.figure.draw(renderer)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/figure.py", line 1144, in draw
renderer, self, dsu, self.suppressComposite)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2426, in draw
mimage._draw_list_compositing_images(renderer, self, dsu)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 354, in draw
self._offset_position)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 607, in draw_path_collection
offset_position)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 309, in draw_path_collection
antialiaseds, urls, offset_position):
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 497, in _iter_collection
gc0.set_foreground(fg)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 1027, in set_foreground
self._rgb = colors.to_rgba(fg)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 143, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 204, in _to_rgba_no_colorcycle
raise ValueError("RGBA values should be within 0-1 range")
ValueError: RGBA values should be within 0-1 range
I've tried using different extensions than .ps, and all of them show the same error, except .png, which saves me a disgustingly awfull image, even when it's supposed to be 1200 dpi (I can't post you a link to the saved image as I do not have enough reputation, sorry).
I've also tried doing a "clean path" with os.path.join, but it changed nothing, so I reverted back to the syntax which used to work on the previous version.
I know my code is most likely suboptimal, but I'm learning so it can't be helped ...
Do you have any clues on how to solve this ?
EDIT : Added the minimal, complete and verifiable example :
import matplotlib.pyplot as plt
data = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10],[0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2]]
def plotAttempt():
plt.errorbar(data[0],data[1],data[2],0)
path = '/home/PlotAttempt.ps'
plt.savefig(path)
Calling plotAttempt() through the console gets me the same error log
This appears to be a pyzo bug, the code worked just fine when I ran it through the console and after rebooting it also worked on pyzo.
Related
I've been working a lot on my code on two different computers, using a git repository to commit all of the changes.
Recently I faced a strange problem in which when using figurecanvasTkagg.draw() I get a ZeroDivisionError on one computer always, while the other one runs just fine and executes the code to completion.
It specifically says that it crashed while performing h0 = h / self.height_ratios[0] in the grid_constraints method of matplotlib/_layoutgrid.py
There is nothing I can actually change as this method is well inside the library and I have found no way to acces it or modify it. I've tried searching for days for the solution to this error but I've found nothing.
Here it is the message error I get when executing the draw() function:
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/backends/backend_tkagg.py", line 10, in draw
super().draw()
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 405, in draw
self.figure.draw(self.renderer)
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer)
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/figure.py", line 3065, in draw
self.get_layout_engine().execute(self)
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/layout_engine.py", line 255, in execute
return do_constrained_layout(fig, w_pad=w_pad, h_pad=h_pad,
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/_constrained_layout.py", line 104, in do_constrained_layout
layoutgrids = make_layoutgrids(fig, None, rect=rect)
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/_constrained_layout.py", line 192, in make_layoutgrids
layoutgrids = make_layoutgrids_gs(layoutgrids, gs)
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/_constrained_layout.py", line 234, in make_layoutgrids_gs
layoutgrids[gs] = mlayoutgrid.LayoutGrid(
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/_layoutgrid.py", line 112, in __init__
self.add_constraints()
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/_layoutgrid.py", line 149, in add_constraints
self.grid_constraints()
File "/home/marcjou/anaconda3/envs/tMednet/lib/python3.8/site-packages/matplotlib/_layoutgrid.py", line 247, in grid_constraints
h0 = h / self.height_ratios[0]
ZeroDivisionError: float division by zero
Thanks in advance!
I have installed Cartopy and am now trying to run the basic example code snippet on the tutorial website just to check everything is working:
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
plt.savefig('coastlines.pdf')
plt.savefig('coastlines.png')
plt.show()
I get the following ValueError back:
Traceback (most recent call last):
File "/Users/mattbright/Desktop/SCripts/lattice_geo.py", line 6, in <module>
ax = plt.axes(projection=ccrs.Mollweide())
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 947, in axes
return subplot(111, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 1125, in subplot
ax = fig.add_subplot(*args, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/figure.py", line 1402, in add_subplot
ax = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/axes/_subplots.py", line 42, in __init__
self._axes_class.__init__(self, fig, self.figbox, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 355, in __init__
super(GeoAxes, self).__init__(*args, **kwargs)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 511, in __init__
self.cla()
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 528, in cla
self._boundary()
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 1425, in _boundary
trans_path = proj_to_data.transform_path(path)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/matplotlib/transforms.py", line 1527, in transform_path
return self.transform_path_affine(self.transform_path_non_affine(path))
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 181, in transform_path_non_affine
src_path.vertices, self.source_projection)
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/cartopy/crs.py", line 649, in quick_vertices_transform
if (x.min() >= x_limits[0] and x.max() <= x_limits[1] and
File "/Users/mattbright/opt/miniconda3/lib/python3.7/site-packages/numpy/core/_methods.py", line 43, in _amin
return umr_minimum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation minimum which has no identity
I tried the installation with conda and with pip3 plus the list of dependencies given on the website.
There's one previous question about this issue which has gone unanswered since the questioner claimed that an Anaconda reinstall solved the problem. A similar issue with Seaborn has been raised here and solved by reverting to an earlier Matplotlib release (3.3.0)
None of these solutions have worked for me. Has anybody else encountered this and found a way to fix it?
I get a latex error in matplotlib when running the following commands within python 2.7.15. It occurs when using a logarithmic axis scale. Whether the error appears or not depends on the matplotlib version, I tested with 1.5.1 (error does not occur) and 2.2.3 (error occurs). The error also only occurs when the code is executed within a single block (like in a function). See below for the minimal example (execute in two separate python consoles!). What exactly causes the error and how can I avoid it, while using the new matplotlib and tex mode?
Common code for both cases:
import sys
default_sys_path = sys.path
import numpy as np
def reproduce_error(old_matplotlib, outname):
if old_matplotlib == True:
sys.path = ['/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages'] + default_sys_path
else:
sys.path = ['/scratch/seismo/proxauf/conda/lib/python2.7/site-packages'] + default_sys_path
import matplotlib
import matplotlib.pyplot as plt
print matplotlib.__version__
print matplotlib.__path__
plt.rcParams['text.usetex'] = True
plt.semilogy(np.arange(1,10)*10**(-10))
plt.savefig('/home/proxauf/%s' % outname)
plt.close()
plt.rcParams['text.usetex'] = False
Case 1 code and output (matplotlib 1.5.1):
reproduce_error(old_matplotlib=True, outname='test_mathdefault_old.pdf')
1.5.1
['/opt/local/anaconda/anaconda-2.2.0/lib/python2.7/site-packages/matplotlib']
Case 2 code and output (matplotlib 2.2.3):
reproduce_error(old_matplotlib=False, outname='test_mathdefault_new.pdf')
2.2.3
['/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib']
Traceback (most recent call last):
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 519, in _draw_idle
self.draw()
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 437, in draw
self.figure.draw(self.renderer)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/figure.py", line 1493, in draw
renderer, self, artists, self.suppressComposite)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2635, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axis.py", line 1192, in draw
renderer)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axis.py", line 1130, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/text.py", line 922, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/text.py", line 309, in _get_layout
ismath=ismath)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 236, in get_text_width_height_descent
s, fontsize, renderer=self)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 501, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 365, in make_dvi
texfile], tex)
File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 344, in _run_checked_subprocess
exc=exc.output.decode('utf-8')))
RuntimeError: latex was not able to process the following string:
'$\\\\mathdefault{10^{-10}}$'
Here is the full report generated by latex:
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/TeX Live for SUSE Linux)
restricted \write18 enabled.
entering extended mode
(/home/proxauf/.cache/matplotlib/tex.cache/be547c40948f52354492209662050ad0.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/share/texmf/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texmf/tex/latex/base/size10.clo))
(/usr/share/texmf/tex/latex/type1cm/type1cm.sty)
(/usr/share/texmf/tex/latex/base/textcomp.sty
(/usr/share/texmf/tex/latex/base/ts1enc.def))
(/usr/share/texmf/tex/latex/geometry/geometry.sty
(/usr/share/texmf/tex/latex/graphics/keyval.sty)
(/usr/share/texmf/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texmf/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texmf/tex/generic/ifxetex/ifxetex.sty)
Package geometry Warning: Over-specification in `h'-direction.
`width' (5058.9pt) is ignored.
Package geometry Warning: Over-specification in `v'-direction.
`height' (5058.9pt) is ignored.
) (./be547c40948f52354492209662050ad0.aux)
(/usr/share/texmf/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Undefined control sequence.
<recently read> \mathdefault
l.13 ...000000}{12.500000}{\sffamily $\mathdefault
{10^{-10}}$}
No pages of output.
Transcript written on be547c40948f52354492209662050ad0.log.
From that I understand the error is because of command \mathdefault. It is a command defined in the python, and no is a base command of Latex. Thus, to solve this problem, you need put above of plt.semilogy(np.arange(1,10)*10**(-10)) the below commands:
from matplotlib import rcParams
rcParams['text.latex.preamble'] = r'\newcommand{\mathdefault}[1][]{}'
Thereby,the command \mathdefault will be defined and will do nothing.
I rewrote you code with those suggestions,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['text.usetex'] = True
rcParams['text.latex.preamble'] = r'\newcommand{\mathdefault}[1][]{}'
plt.semilogy(np.arange(1,10)*10**(-10))
# plt.savefig('/home/proxauf/%s' % outname) ## outname????
plt.close()
rcParams['text.usetex'] = False
So, I'm receiving "RuntimeError: Could not allocate memory for image" from matplotlib when I try to re-size my image on the canvas. The images are large DICOM (.dcm) files that have been allocated memory using ctypes' PyMem_Malloc. They've been allocated 16 bits per pixel (they need to be Hi-Res). The problem comes after we've re-sized the canvas and attempt to draw the re-sized image using:
self.view.canvas.draw()
Here's the specific exception being thrown:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_wx.py", line 1355, in _onMotion
FigureCanvasBase.motion_notify_event(self, x, y, guiEvent=evt)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 1625, in motion_notify_event
self.callbacks.process(s, event)
File "C:\Python27\lib\site-packages\matplotlib\cbook.py", line 265, in process
proxy(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\cbook.py", line 191, in __call__
return mtd(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\widgets.py", line 1258, in onmove
self.update()
File "C:\Python27\lib\site-packages\matplotlib\widgets.py", line 1236, in update
self.canvas.restore_region(self.background)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 384, in restore_region
renderer = self.get_renderer()
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 404, in get_renderer
self.renderer = RendererAgg(w, h, self.figure.dpi)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 59, in __init__
self._renderer = _RendererAgg(int(width), int(height), dpi, debug=False)
RuntimeError: Could not allocate memory for image
The images themselves shouldn't need more allocation, correct? So what could be the problem here? Does matplotlib limit the size in memory that the canvas can hold? Has anyone had this problem?
I am trying to run Pyramid with Jinja2 using new Python 2.7 runtime in threadsafe mode and GAE 1.6.0 pre-release SDK. I've made modifications to my app as outlined here, i.e. I've set runtime: python27, threadsafe: true in app.yaml and got rid of main() function. When I generate response by myself it works fine, but when I try to bring jinja2 into the equation, I get the following exception:
ERROR 2011-11-07 00:10:34,356 wsgi.py:170]
Traceback (most recent call last):
File "/gae/google/appengine/runtime/wsgi.py", line 168, in Handle
[...]
File "/myapp/source/myapp-tip/main.py", line 29, in <module>
config.include('pyramid_jinja2')
File "/myapp/source/myapp-tip/lib/dist/pyramid/config/__init__.py", line 616, in include
c(configurator)
File "lib/dist/pyramid_jinja2/__init__.py", line 390, in includeme
_get_or_build_default_environment(config.registry)
File "/lib/dist/pyramid_jinja2/__init__.py", line 217, in _get_or_build_default_environment
_setup_environment(registry)
File "/lib/dist/pyramid_jinja2/__init__.py", line 253, in _setup_environment
package = _caller_package(('pyramid_jinja2', 'jinja2', 'pyramid.config'))
File "/lib/dist/pyramid_jinja2/__init__.py", line 136, in caller_package
for t in self.inspect.stack():
File "/usr/lib/python2.7/inspect.py", line 1056, in stack
return getouterframes(sys._getframe(1), context)
File "/usr/lib/python2.7/inspect.py", line 1034, in getouterframes
framelist.append((frame,) + getframeinfo(frame, context))
File "/usr/lib/python2.7/inspect.py", line 1009, in getframeinfo
lines, lnum = findsource(frame)
File "/usr/lib/python2.7/inspect.py", line 534, in findsource
module = getmodule(object, file)
File "/usr/lib/python2.7/inspect.py", line 506, in getmodule
main = sys.modules['__main__']
KeyError: '__main__'
I tried to mess around a bit with pyramid_jinja2 code to work around this issue, only to be left with another exception:
ERROR 2011-11-04 12:06:38,720 wsgi.py:170]
Traceback (most recent call last):
File "/gae/google/appengine/runtime/wsgi.py", line 168, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
[...]
File "/myapp/source/myapp-tip/main.py", line 29, in <module>
config.add_jinja2_search_path("templates")
File "/myapp/source/myapp-tip/lib/dist/pyramid/config/util.py", line 28, in wrapper
result = wrapped(self, *arg, **kw)
File "/lib/dist/pyramid_jinja2/__init__.py", line 311, in add_jinja2_search_path
env.loader.searchpath.append(abspath_from_resource_spec(d))
File "/myapp/source/myapp-tip/lib/dist/pyramid/asset.py", line 38, in abspath_from_asset_spec
return pkg_resources.resource_filename(pname, filename)
File "/myapp/source/myapp-tip/pkg_resources.py", line 840, in resource_filename
return get_provider(package_or_requirement).get_resource_filename(
File "/myapp/source/myapp-tip/pkg_resources.py", line 160, in get_provider
__import__(moduleOrReq)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate
return func(self, *args, **kwargs)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 1756, in load_module
return self.FindAndLoadModule(submodule, fullname, search_path)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate
return func(self, *args, **kwargs)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 1628, in FindAndLoadModule
description)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 640, in Decorate
return func(self, *args, **kwargs)
File "/gae/google/appengine/tools/dev_appserver_import_hook.py", line 1571, in LoadModuleRestricted
description)
ImportError: Cannot re-init internal module __main__
I'd be happy if anybody could shed some light on what pyramid is trying to do under the hood. Judging by the latter stack trace it seems it's trying to resolve an asset, but why is it trying to reload __main__? I'm not even sure my problem is caused by pyramid or GAE.
Thanks for any insight on this issue.
I'm not familiar with pyramid, but the problem really does seem to be with this line:
config.include('pyramid_jinja2')
Whatever that config thing is, it seems to be doing some dynamic import magic.
Don't do that.
The app engine environment doesn't handle imports the way you would in normal python. Step through that line with a debugger and you'll wind up in the replacement version of the import system, which you'll soon see, only implements a small part of what real python does.
If possible, just use a normal import statement... Otherwise, you're going to have to dig into config.include and get it to play nice with the restricted importing features on GAE.
I managed to make it work using Pyramid 1.3's AssetResolver. First attempt is here. Just not sure what the lifetime/scope of the resolver should be in this case, I will figure it out later.
In pyramid_jinja2/__init__.py add the following code before _get_or_build_default_environment()
class VirtualModule(object):
def __init__(self,name):
import sys
sys.modules[name]=self
def __getattr__(self,name):
return globals()[name]
VirtualModule("__main__")
def _get_or_build_default_environment(registry):
(http://www.inductiveautomation.com/forum/viewtopic.php?f=70&p=36917)