This works if you got "PortAudio" on your system.
import audiofile as af
import sounddevice as sd
def record_audio(filename, seconds):
fs = 16000
print("recording {} ({}s) ...".format(filename, seconds))
y = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
sd.wait()
y = y.T
af.write(filename, y, fs)
print(" ... saved to {}".format(filename))