I have a GUI-less cloud server running Bitnami-Django Ubuntu 14.04 LTS that is meant to retrieve and graph data for users, but it cannot produce the graphs. To be clear, I only care that the graph image is produced and saved, not that a user has an option to click a button to save the image. Such functionality would be meaningless for such a server.
On my normal Ubuntu Linux (Mate) 14.04 LTS, the scripts work perfectly, producing a matplotlib.pyplot from the relevant data in a GUI window with save, zoom, rotate and other functionality; however on the cloud server I get this error, even if I don't try to invoke the show() function:
bitnami#StockPredix:/opt/bitnami/apps/django/django_projects/Project$ python api-test_volume.py
Traceback (most recent call last):
File "api-test_volume.py", line 8, in <module>
import matplotlib.pyplot as plt
File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/pyplot.py", line 114, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from matplotlib.externals.six.moves import tkinter as Tk
File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/externals/six.py", line 199, in load_module
mod = mod._resolve()
File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/externals/six.py", line 113, in _resolve
return _import_module(self.mod)
File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/externals/six.py", line 80, in _import_module
__import__(name)
File "/opt/bitnami/python/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
This continued even after I installed other dependencies and tried installing tk-dev, tcl-dev, etc. I think I will need an alternative to matplotlib to produce the graphs, unless one of you kind souls knows a clever workaround. Thanks in advance for your help.
(Quick) Edit: I'm aware of and tried the fix in Save plot to image file instead of displaying it using Matplotlib, but on my cloud server this is a matter of broken dependency with the GUI in the first place, instead of just suppressing the GUI.
This may not be an actual answer because I couldn't test it, but it seems the error comes from the fact that tkinter is tried to be loaded unsuccessfully. So the naturaly solution might be to avoid using the Tk backend.
There are several backends in matplotlib and some are not meant for interactive plotting. You'll get those with
import matplotlib
print matplotlib.rcsetup.non_interactive_bk
(make sure to run this before importing pyplot). The list contains
[u'agg', u'cairo', u'emf', u'gdk', u'pdf', u'pgf', u'ps', u'svg', u'template']
and potentially any of those might suit your case. To select one of them, use
matplotlib.use('<name of backend>')
(still before importing pyplot)
Finally import pyplot and do your stuff, avoiding plt.show(). It may also be that some functionalities are not present for some of the backends - that needs to be tested, e.g. saving an svg from the pdf backend would not work.
Related
so I found this program, "BoxParti" made by user tpvasconcelos and I think it could help me greatly with research. Sadly it uses some packages I have never used before and it's kinda problematic to get it to work for me. I didn't change anything in the code but it doesn't want to work with me. Problem is mainly with tkinter and i'm not really familiar with it. I never used GUI before so it's kinda all tricky to me. At the moment I only want to make the program work and check if it could really be useful for me. I've been trying to make it work by myself but I didn't suceed sadly. It's kinda complex for me since I haven't used python in a while + haven't really ever used GUI with anything. Below I paste the link to download the code. I run it on Python 3.8 or 2.7, on both versions i get same errors. Below the link I paste error logs. I would greatly appreciate any tip that could sort of push me in the right direction because it seems to me I'm very lost
https://github.com/TPVasconcelos/BoxParti
Traceback (most recent call last):
File "BoxParti.py", line 1493, in <module>
app = BoxParti()
File "BoxParti.py", line 53, in __init__
frame = F(container, self)
File "BoxParti.py", line 151, in __init__
self.make_plot(frame)
File "BoxParti.py", line 176, in make_plot
self.canvas.show()
AttributeError: 'FigureCanvasTkAgg' object has no attribute 'show'
This code may work in early versions of Python, e.g. python 3.5. Several things have been deprecated. That means, you have to find correct packages.
For instance, if you uncomment the following the code runs on Python 3.8.
Uncomment: #self.canvas.show() in lines; 177, 610, 646, 692, 817, 1268, 1299, 1371
also uncomment; self.canvas.get_tk_widget()#.grid(row=0, column=1), in lines 179, 819.
But it does not show 2D or 3D anime. This could be because of uncommenting self.canvas.show().
As a result, if you want to run this code, you have to find older versions required libraries. Which I do not suggest.
Instead of that, BoxParti have been expanded to mdsea, you can install mdsea which may work better.
I have a simple graph and need to draw it on my screen, here is my code:
def gera_grafo(matriz):
grafo = nx.to_networkx_graph(matriz, create_using=nx.Graph)
nx.draw(grafo)
plt.show()
return grafo
Where matrix is an adjacency list containing the weights of the connections. The coda was working just fine, but i had to create a new python virtualenv and since then, even though all the required libraries are correctly installed it throws an error on the nx.draw() call. The error I got is:
Traceback (most recent call last):
File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/networkx/utils/decorators.py", line 396, in _random_state
random_state_arg = args[random_state_index]
IndexError: tuple index out of range
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/criacao_do_grafo.py", line 58, in <module>
grafo = gera_grafo(matriz)
File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/criacao_do_grafo.py", line 39, in gera_grafo
nx.draw(grafo)
File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/networkx/drawing/nx_pylab.py", line 123, in draw
draw_networkx(G, pos=pos, ax=ax, **kwds)
File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/networkx/drawing/nx_pylab.py", line 333, in draw_networkx
pos = nx.drawing.spring_layout(G) # default to spring layout
File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/decorator.py", line 214, in fun
return caller(func, *(extras + args), **kw)
File "/run/media/luisola/A216C03316C009ED/Users/Luis/Documents/Iniciação Científica/inicia-o-cient-fica/venv/lib/python3.9/site-packages/networkx/utils/decorators.py", line 400, in _random_state
raise nx.NetworkXError("random_state_index is incorrect") from e
networkx.exception.NetworkXError: random_state_index is incorrect
Is this an error on my code? If so, what can i do? Thanks in advance
As stated for this question: networkx shows random_state_index is incorrect
Their was a problem with decorator=5.0.0. As discussed in the related issue on GitHub (https://github.com/networkx/networkx/issues/4718) decorator>=5.0.X should be available soon. So either wait a little bit to upgrade or downgrade to an old version as suggested in the SO question above.
Edit decorator==5.0.5 or >=5.0.7 fixes the error
As discussed in the issue linked above, decorator has now been updated and fixed.
I was using decorator==5.0.6, and got the same error.
However, upgrading to 5.0.7 solves my problem.
If you are using MacOS - BigSur, networkx won't work the way you want it to. I needed to go and open my project in Ubuntu.
Its worked for me, after downgrade the networkx and decorator
networkx 2.3 pypi_0 pypi
decorator 4.3.0 pypi_0 pypi
OS: Mac OS BigSur
Since I am using Windows 10, the suggestion given by Sparky05 worked for me by updating the version of decorator module in python by using pip install decorator==5.0.7.
Also you can update the version of networkx library. I will share the link for updating it later, but it will not work if you are using Spyder IDE. It will work in VSCode.
I have tried running the code found at http://computationallegalstudies.com/2009/11/15/programming-dynamic-models-in-python-3-outbreak-on-a-network/ but at the end the following shows up:
File "/Library/Python/2.7/site-packages/igraph/drawing/__init__.py", line 446, in plot
result = Plot(target, bbox, background=kwds.get("background", "white"))
File "/Library/Python/2.7/site-packages/igraph/drawing/__init__.py", line 117, in __init__
self._surface_was_created = not isinstance(target, cairo.Surface)
File "/Library/Python/2.7/site-packages/igraph/drawing/utils.py", line 396, in __getattr__
raise TypeError("plotting not available")
TypeError: plotting not available
It's a code for modelling SIR model on networks. I really need someone's help as I am not able to use a computer , a mac in this case. Thank you in advance
You need to install the Cairo library and its Python bindings for plotting. If you are using Homebrew, it is simply a matter of brew install cairo py2cairo, assuming that you use the system Python (which you seem to be, judging from the stack trace).
I read in the howto documentation to install Trigger, but when I test in python environment, I get the error below:
>>> from trigger.netdevices import NetDevices
>>> nd = NetDevices()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/__init__.py", line 913, in __init__
with_acls=with_acls)
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/__init__.py", line 767, in __init__
production_only=production_only, with_acls=with_acls)
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/__init__.py", line 83, in _populate
# device_data = _munge_source_data(data_source=data_source)
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/__init__.py", line 73, in _munge_source_data
# return loader.load_metadata(path, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/loader.py", line 163, in load_metadata
raise RuntimeError('No data loaders succeeded. Tried: %r' % tried)
RuntimeError: No data loaders succeeded. Tried: [<trigger.netdevices.loaders.filesystem.XMLLoader object at 0x7f550a1ed350>, <trigger.netdevices.loaders.filesystem.JSONLoader object at 0x7f550a1ed210>, <trigger.netdevices.loaders.filesystem.SQLiteLoader object at 0x7f550a1ed250>, <trigger.netdevices.loaders.filesystem.CSVLoader object at 0x7f550a1ed290>, <trigger.netdevices.loaders.filesystem.RancidLoader object at 0x7f550a1ed550>]
Does anyone have some idea how to fix it?
The NetDevices constructor is apparently trying to find a "metadata source" that isn't there.
Firstly, you need to define the metadata. Second, your code should handle the exception where none is found.
I'm the lead developer of Trigger. Check out the the doc Working with NetDevices. It is probably what you were missing. We've done some work recently to improve the quality of the setup/install docs, and I hope that this is more clear now!
If you want to get started super quickly, you can feed Trigger a CSV-formatted NetDevices file, like so:
test1-abc.net.example.com,juniper
test2-abc.net.example.com,cisco
Just put that in a file, e.g. /tmp/netdevices.csv and then set the NETDEVICES_SOURCE environment variable:
export NETDEVICES_SOURCE=/tmp/netdevices.csv
And then fire up python and continue on with your examples and you should be good to go!
I found that the default of /etc/trigger/netdevices.xml wasn't listed in the setup instructions. It did indicate to copy from the trigger source folder:
cp conf/netdevices.json /etc/trigger/netdevices.json
But, I didn't see how to specify this instead of the default NETDEVICES_SOURCE on the installation page. But, as soon as I had a file that NETDEVICES_SOURCE pointed to in my /etc/trigger folder, it worked.
I recommend this to get the verifying functionality examples to work right away with minimal fuss:
cp conf/netdevices.xml /etc/trigger/netdevices.xml
Using Ubuntu 14.04 with Python 2.7.3
I've narrowed down to this call:
fig.canvas.tostring_argb() #fig=matplotlib.pyplot.figure()
this function raises an AttributeError when I run the code as a python script.
AttributeError: 'FigureCanvasGTKAgg' object has no attribute 'renderer'
However, this code works properly if run in the ipython --pylab command line.
As far as I can tell from the documentation, the Agg renderer should work OK.
The context is that I'm trying to make a movie from figures, without saving the frames
to disk; as per this question. I'm using the approach that streams the pixel arrays
to ffmpeg (running as a separate process) to do this, I need the argb array of values from the frame.
Is there some configuration setting I can make to get matplotlib to work correctly from within a script?
Edit
Tried use('Agg') as per a comment; still fails; this is a minimal working example.
[dave#dave tools]$ python -c "import matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot; fig=matplotlib.pyplot.figure(); fig.canvas.tostring_argb()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 416, in tostring_argb
return self.renderer.tostring_argb()
AttributeError: FigureCanvasAgg instance has no attribute 'renderer'
I suspect that you have missed out the call to:
fig.canvas.draw()
before
fig.canvas.tostring_argb()
as
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.tostring_argb()
fails for me, but
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
fig=matplotlib.pyplot.figure()
fig.canvas.draw()
fig.canvas.tostring_argb()
works.
I ended up installing and using the WXAgg backend; the Agg,and default GTKAgg, didn't work for me.