0

I am new to python and pip, but wanted to tinker around with sound synthesis via python. So I tried to install pyaudio like it is described on the pyaudio website. Installing portaudio via homebrew was successful

  /usr/local/Cellar/portaudio/19.6.0: 33 files, 452KB

When I ran the install command for pyaudio I got this:

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pyaudio.pyc'
Consider using the `--user` option or check the permissions.

So I tried this:

pip install --user pyaudio

And got this:

Collecting pyaudio
Installing collected packages: pyaudio
Successfully installed pyaudio-0.2.11

Now, when i run this script (which i found here) I get the following error message

import math        #import needed modules
import pyaudio     #sudo apt-get install python-pyaudio

PyAudio = pyaudio.PyAudio     #initialize pyaudio

#See https://en.wikipedia.org/wiki/Bit_rate#Audio
BITRATE = 16000     #number of frames per second/frameset.      

FREQUENCY = 500     #Hz, waves per second, 261.63=C4-note.
LENGTH = 1     #seconds to play sound

if FREQUENCY > BITRATE:
    BITRATE = FREQUENCY+100

NUMBEROFFRAMES = int(BITRATE * LENGTH)
RESTFRAMES = NUMBEROFFRAMES % BITRATE
WAVEDATA = ''    

#generating wawes
for x in xrange(NUMBEROFFRAMES):
 WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128))    

for x in xrange(RESTFRAMES): 
 WAVEDATA = WAVEDATA+chr(128)

p = PyAudio()
stream = p.open(format = p.get_format_from_width(1), 
                channels = 1, 
                rate = BITRATE, 
                output = True)

stream.write(WAVEDATA)
stream.stop_stream()
stream.close()
p.terminate()

Error message

/Users/Mogli/PycharmProjects/aCoherentJourney/venv/bin/python /Users/Mogli/PycharmProjects/aCoherentJourney/test.py
Traceback (most recent call last):
  File "/Users/Mogli/PycharmProjects/aCoherentJourney/test.py", line 2, in <module>
    import pyaudio     #sudo apt-get install python-pyaudio
ModuleNotFoundError: No module named 'pyaudio'

Process finished with exit code 1

I've been googling this problem for days now and have similar problems with other modules. My guess is, that it might have to do something with different python versions being installed on my system. So i also tried installing it via

pip3 install --user pyaudio

Python Versions

python --version
Python 2.7.10

python3 --version
Python 3.6.4

Python3 was installed via homebrew. brew info python returns:

python: stable 3.7.1 (bottled), HEAD
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python/3.7.0 (4,781 files, 102MB)
  Poured from bottle on 2018-11-06 at 21:04:42
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/python.rb
==> Dependencies
Build: pkg-config ✘
Required: gdbm ✔, openssl ✔, readline ✔, sqlite ✘, xz ✔
Optional: tcl-tk ✘
==> Options
--with-tcl-tk
    Use Homebrew's Tk instead of macOS Tk (has optional Cocoa and threads support)
--HEAD
    Install HEAD version
==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

If you need Homebrew's Python 2.7 run
  brew install python@2

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Analytics
install: 282,353 (30 days), 711,193 (90 days), 2,944,083 (365 days)
install_on_request: 172,678 (30 days), 469,977 (90 days), 1,781,137 (365 days)
build_error: 0 (30 days)

I don't know what to try next. Any help would be appreciated :)

Moglash
  • 259
  • 1
  • 5
  • 23
  • Thanks for your quick answer! I forgot to mention that I installed python via homebrew. When I repeat the info with `brew info python`i get: `python: stable 3.7.1 (bottled), HEAD` – Moglash Nov 09 '18 at 18:28
  • 1
    The first line of the error message implies that you're running the script inside a virtual environment. Did you install pyaudio inside the virtualenv? – jwodder Nov 09 '18 at 18:42
  • Uhm, not that i know of... I think I don't run a virtual environment. I did use virtualbox sometime ago, but that was for web development. – Moglash Nov 09 '18 at 18:54
  • @jwodder it looks like the venv was created by pycharm, without the OP really noticing this. @DennisFink run `/Users/Mogli/PycharmProjects/aCoherentJourney/venv/bin/pip install pyaudio` and then rerun the script in pycharm. – hoefling Nov 09 '18 at 20:21
  • thanks for all your help! i did what @hoefling suggested. now the module is found. i get a new error message now :D `NameError: name 'xrange' is not defined` but that's a new topic I guess. – Moglash Nov 09 '18 at 21:17
  • I have removed my earlier comments as the issue appears to have been elsewhere. Glad it's resolved. – Mark Setchell Nov 09 '18 at 22:33

0 Answers0