There is the music21 python project from MIT http://web.mit.edu/music21/
It's been awhile since I've used it but it worked well and could generate and analyze music in the classical Western tradition.
I used it in a project to generate midi music in the style of specific composers based on Markov chains. Sadly I don't have the source anymore otherwise I'd link you to it.