[Python] How do I use the MIDIUtil library without raising an Import Error?

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have already got MIDIUtil to work in the past without an import error on my Mac. For some reason, it has now stopped working and throws an import error, no matter how I try to change the file directory or the name of the module in my code:

    ImportError: No module named midiutil


    I am just trying to run the 'example' file located in the package:

    #!/usr/bin/env python

    from midiutil import MIDIFile

    degrees = [60, 62, 64, 65, 67, 69, 71, 72] # MIDI note number
    track = 0
    channel = 0
    time = 0 # In beats
    duration = 1 # In beats
    tempo = 60 # In BPM
    volume = 100 # 0-127, as per the MIDI standard

    MyMIDI = MIDIFile(1) # One track
    MyMIDI.addTempo(track, time, tempo)

    for i, pitch in enumerate(degrees):
    MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)

    with open("major-scale.mid", "wb") as output_file:
    MyMIDI.writeFile(output_file)


    I have read some stack overflow posts on this subject, who recommend:

    from midiutil.MidiFile import MIDIFile


    and:

    from midiutil.MidiFile3 import MIDIFile


    I seem to have tried every option, but there is definitely something that I am missing!

    I am a beginner at Python and coding in general so thanks in advance for the help!

    Continue reading...

Compartilhe esta Página