Matplotlib

Matplotlib

de către Andreas Siebel-
Număr de răspunsuri: 14

Hello,

I tried setting up the python-sandpit, but I get the following error:

Matplotlib created a temporary config/cache directory at /tmp/matplotlib-8125qscx because the default path (/home/jobe00/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.

I installed
Ca răspuns la Andreas Siebel

Re: Matplotlib

de către Andreas Siebel-

I found the error:

pip3 uninstall matplotlib
apt-get install python3-matplotlib

Solved the problem

Ca răspuns la Andreas Siebel

Re: Matplotlib

de către Laurent Berger-
Unfortunalety It does not work for me :

Mauvaise sortie de la notation :
Run result: Run error
Output:
{"epiloguehtml": "png;base64,
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-yca8mrb_ because the default path (/home/jobe00/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
. L'exécution de votre programme a peut-être été interrompue (par exemple, une limite de temps ou une limite de mémoire dépassée).
Ca răspuns la Laurent Berger

Re: Matplotlib

de către Laurent Berger-
It does not work because I use python 3.10.4 and that's not official distribution.

How can I set MPLCONFIGDIR in jobe config?
Ca răspuns la Laurent Berger

Re: Matplotlib

de către Laurent Berger-
Ok solved i used those lines in template :

import getpass
import os
os.environ['MPLCONFIGDIR'] = "/tmp/" + getpass.getuser()
import matplotlib.pyplot as plt
import json
import base64
import subprocess
import re
import os.path

# Define lines of code to insert before student's code
PREFIX = ["import matplotlib",
"matplotlib.use('Agg')",
"import matplotlib.pyplot as __plt__",
]

def make_data_uri(filename): ......
Ca răspuns la Laurent Berger

Re: Matplotlib

de către Richard Lobb-
Good fix! Our own workaround (e.g. see here) is

if 'MPLCONFIGDIR' not in os.environ or os.environ['MPLCONFIGDIR'].startswith('/home'):
    import tempfile
    os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()

Would you mind telling us what question type you're using as your starting point? It looks like one of our own (University of Canterbury) in-house types. Some of the ones lying around in various historic postings are very out-dated and probably should be deleted or edited to point to more recent versions. Hopefully yours isn't too old. It sounds like you've got it working, anyway.

Ca răspuns la Laurent Berger

Re: Matplotlib

de către Richard Lobb-
Thanks for the info. Good to know that blog post has been useful. Since it's just introducing general principles, it's not something that will have gone out of date. Enjoy!
Ca răspuns la Richard Lobb

Re: Matplotlib

de către Chris Nelson-
Apologies for raising a dead thread, but we too at The Open University UK are starting to look at Matplotlib and receive this error.

Unfortunately the links to example question XMLs appear to be all dead.

if 'MPLCONFIGDIR' not in os.environ or os.environ['MPLCONFIGDIR'].startswith('/home'):
    import tempfile
    os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
...seems to be the most-recommended solution here, GitHub etc., but were exactly would we add this?
Ca răspuns la Chris Nelson

Re: Matplotlib

de către Richard Lobb-
Sorry about the dead links. The ucquestiontypes repo is mostly for in-house use, and for convenience I had packaged all the support files for our python3_stage1 question type into Files.zip rather than manage them all separately. I hadn't realised there were links to the individual files. I've now unzipped and you'll see the __pytask.py file there now. I doubt it's quite what you want, though, as it's specific to our own very complex in-house question type.

I thought my blog was so old that no-one would want it any more, so I had hidden it. I just unhid it. It might be a bit more useful to you than __pytask, though it doesn't include the specific lines of code you're trying to insert.

The short answer to your question is that you just need to insert those lines into the Python program that you're about to run, i.e. before the student answer. For example, if you're using the standard python3 question type, and you click the Customise checkbox, you'll see the template:

{{ STUDENT_ANSWER }}

__student_answer__ = """{{ STUDENT_ANSWER | e('py') }}"""

SEPARATOR = "##"

{% for TEST in TESTCASES %}
{{ TEST.testcode }}
{% if not loop.last %}
print(SEPARATOR)
{% endif %}
{% endfor %}
@17943918#@>

So you can insert the extra code to set the matplotlib environment at the very beginning of that template.

A downside of this, which our much more complex question types avoid one way or another, is that the error messages the students see will all be wrong (in the students' eyes) because of the extra hidden code at the start.

A much better fix would be to modify the runguard environment in Jobe to add the appropriate environment variable when running Python if the string matplotlib is present anywhere in the source code. That way you wouldn't have to do anything to the Python source code at all. I just haven't got around to doing that yet.

Ca răspuns la Richard Lobb

Re: Matplotlib

de către Chris Nelson-
Thanks Richard, that's enough for us to continue testing on our test system for the moment :-)
Ca răspuns la Chris Nelson

Re: Matplotlib

de către Chris Nelson-
Simply to update this thread in case it of use to any other users of CodeRunner dabbling in exotic libraries, I had to add an "import os" line:

import os
if 'MPLCONFIGDIR' not in os.environ or os.environ['MPLCONFIGDIR'].startswith('/home'):
    import tempfile
    os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
In our OU theme, it therefore looks like this:

Ca răspuns la Richard Lobb

Re: Matplotlib

de către Felix Di Lenarda-
Thank you for your time and effort! :)

As i understand a person has to add the additional code for setting 'os.environ['MPLCONFIGDIR']' for each question which uses mathplotlib?

I'm searching for a general solution but my attempts to alter the "runguard.c" file do not work unfortunately.
Could you point me to a line in runguard.c where you would implement this?
Do I just need to run the command "sudo ./install --purge" after the change for compiling?
Ca răspuns la Felix Di Lenarda

Re: Matplotlib

de către Felix Di Lenarda-
I solved the problem by adding:
setenv("MPLCONFIGDIR", "/tmp", 1);
to here and compile runguard.c with gcc.
NOTE: this sets the environment variable for every process without checking if matplotlib is really used or not!
Ca răspuns la Felix Di Lenarda

Re: Matplotlib

de către Richard Lobb-
Thanks for posting, Felix. I've had this on my todo list for some time, but it hasn't been a priority, since our question type solves the problem instead. But doing it on Jobe is much cleaner. I'll put something like your fix in the next update to Jobe.