Is it possible to import non-standard python modules such as matplotlib?
You can install any non-standard modules on the Jobe server first (probably just a one-line command) and then you can import it as usual.
Or, if you have the non-standard module available in a suitable form, you can add it to a question or question prototype as a support file and import it from the working directory. That saves having to install it on the Jobe server, and is more maintainable. One of our standard in-house question type has a module as a .zip support file which we unzip and import in the template.
But the use of matplotlib is questionable. How are you going to capture, grade and display the output from matplotlib?
I am wanting to do exactly what you have said in your response Richard,
"One of our standard in-house question type has a module as a .zip support file which we unzip and import in the template."
I've tried to do this, but get a ModuleNotFoundError when running my CodeRunner test.
The code I'm using to load a zip file and import the contained module is as follows:
sys.path.insert(0, 'javalang-0.12.0.zip') # Add .zip file to front of path
Could you please share how you've unzipped a .zip file and loaded a module in one of your question types?
It works for me!
Here's what I did:
- I downloaded javalang from github and made a zip file of the javalang-master/javalang directory. I called the zip javalang.zip.
- I created a new Python3 CodeRunner question, and added javalang.zip as a support file.
- I set a dummy testcase with 'asdfasdf' as the expected output.
- I saved the question.
- I previewed it, pasted in the code to import javalang, clicked Check, and got the output below.
Thanks for your quick and helpful response. I am now getting the same behaviour as you.
The difference seems to be in the contents of the zip file. I followed a similar to process to you initially, but the notable difference was in the contents of the zip file.
I recreated the zip file, so that it only contained one folder named 'javalang' that in turn contained the .py files. This minimal zip works fine.
Before I made my earlier post, I created the zip file such that it contained not only the 'javalang' folder, but also a 'javalang-egg-info' folder and other configuration files at the root-level of the zip file. Removing all the these superfluous files seems to explain why things weren't working correctly earlier.