Importing non-standard python modules

Importing non-standard python modules

de Nick Savill -
Número de respuestas: 4

Is it possible to import non-standard python modules such as matplotlib?

En respuesta a Nick Savill

Re: Importing non-standard python modules

de Richard Lobb -

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?

Richard

En respuesta a Richard Lobb

Re: Importing non-standard python modules

de Ian Warren -

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:

import sys
sys.path.insert(0, 'javalang-0.12.0.zip')  # Add .zip file to front of path
import javalang
This works fine in a standalone Python program to "dynamically" load the javalang module, but not in CodeRunner. I don't have a lot of Python experience, and aren't sure of how to go about this.

Could you please share how you've unzipped a .zip file and loaded a module in one of your question types?

Thanks.

Ian

En respuesta a Ian Warren

Re: Importing non-standard python modules

de Richard Lobb -

It works for me!

Here's what I did:

  1. I downloaded javalang from github and made a zip file of the javalang-master/javalang directory. I called the zip javalang.zip.
  2. I created a new Python3 CodeRunner question, and added javalang.zip as a support file.
  3. I set a dummy testcase with 'asdfasdf' as the expected output.
  4. I saved the question.
  5. I previewed it, pasted in the code to import javalang, clicked Check, and got the output below.


En respuesta a Richard Lobb

Re: Importing non-standard python modules

de Ian Warren -

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.

Thanks again.