Search code examples
pythonnumpyimporterrorlinux-mintjython-music

How to fix numpy importerror in JythonMusic


I am working on a music composition AI program using JythonMusic on Linux Mint 19.1. When I attempt to test my code, I get an import error say that the numpy module is not found. I have verified that numpy is installed for both Python 2.7 and Python 3.6 by trying to import the module in IDLE, and that has worked both times. Is there something that I can do to get all of my installed modules to import normally in JythonMusic?


Solution

  • There is a difference between Python and Jython. Python is built in C, whereas Jython is built on top of the Java VM. Therefore, packages installed under Python 2.7 and Python 3.6 are NOT available directly to Jython.

    However, there is a Java version of numpy called numjy, available here.

    I just downloaded it, unzipped it, and copied the numjy folder into the JythonMusic folder (same folder as where JEM.jar is stored).

    Then, I restarted JEM, and was able to run the following code:

    import numjy as np
    
    a = np.arange(10)
    print a
    
    a = a.reshape(2, 5)
    print a
    
    print a * 10
    

    which gave me the following output:

    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    array([[0, 1, 2, 3, 4]
          [5, 6, 7, 8, 9]])
    array([[0, 10, 20, 30, 40]
          [50, 60, 70, 80, 90]])
    

    I hope this helps!