2

I am trying to run a Python program that uses pyaudio:

  1. I download the program (from Github because the .deb is too old)
  2. I install python-pyaudio
  3. I run the program
  4. The program says ImportError: No module named 'pyaudio'

What am I doing wrong?

$ sudo apt-get install python-pyaudio
[sudo] password for nico: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  python-pyaudio-doc
The following NEW packages will be installed:
  python-pyaudio
0 upgraded, 1 newly installed, 0 to remove and 49 not upgraded.
Need to get 0 B/24.1 kB of archives.
After this operation, 109 kB of additional disk space will be used.
Selecting previously unselected package python-pyaudio.
(Reading database ... 336001 files and directories currently installed.)
Preparing to unpack .../python-pyaudio_0.2.8-1build2_amd64.deb ...
Unpacking python-pyaudio (0.2.8-1build2) ...
Setting up python-pyaudio (0.2.8-1build2) ...

$ ./runanki 
Traceback (most recent call last):
  File "./runanki", line 3, in <module>
    import aqt
  File "/home/nico/src/anki/aqt/__init__.py", line 4, in <module>
    from anki import version as _version
  File "/home/nico/src/anki/anki/__init__.py", line 13, in <module>
    from anki.storage import Collection
  File "/home/nico/src/anki/anki/storage.py", line 12, in <module>
    from anki.collection import _Collection
  File "/home/nico/src/anki/anki/collection.py", line 26, in <module>
    from anki.sound import stripSounds
  File "/home/nico/src/anki/anki/sound.py", line 207, in <module>
    import pyaudio
ImportError: No module named 'pyaudio'

Environment:

$ which python
/usr/bin/python
$ python --version
Python 2.7.11+
Nicolas Raoul
  • 11,941
  • What does which python say? – edwinksl Jul 14 '16 at 07:59
  • Hmm, I initially thought you perhaps had your own Python installed in addition to the default one, so it couldn't see the pyaudio that was installed using apt. That doesn't seem to be what is happening here though. – edwinksl Jul 14 '16 at 08:03
  • I think I found the problem. I went to read the runanki script at https://github.com/dae/anki/blob/master/runanki and the shebang explicitly states Python 3. Are you sure anki will work for Python 2? If so, you could change the shebang to Python 2. The more straightforward solution in my opinion is to install the Python 3 version of pyaudio using sudo apt install python3-pyaudio. – edwinksl Jul 14 '16 at 08:05
  • That did the trick, thanks! Would you mind posting an answer, including the shebang thing? Cheers! – Nicolas Raoul Jul 14 '16 at 08:10

1 Answers1

3

The cause of the problem is that the shebang of the runanki script at https://github.com/dae/anki/blob/master/runanki explicitly states Python 3, so ./runanki invokes the Python 3 interpreter but you had installed pyaudio for Python 2. As a result, Python 3 is not able to find pyaudio and therefore raises an ImportError. The easiest solution here is to install the Python 3 bindings for pyaudio by running:

sudo apt update && sudo apt install python3-pyaudio
edwinksl
  • 24,149