无法在macOS High Sierra上通过pip安装pyaudio

时间:2018-11-09 18:04:03

标签: python macos pip pyaudio

我是python和pip的新手,但想通过python完善声音合成。因此,我尝试安装pyaudio,如pyaudio website.中所述 通过自制软件安装portaudio成功

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

当我为pyaudio运行安装命令时,我得到了:

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.

所以我尝试了这个:

pip install --user pyaudio

得到了:

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

现在,当我运行此脚本(发现here)时,出现以下错误消息

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()

错误消息

/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

我已经搜索这个问题好几天了,并且在其他模块上也有类似的问题。我的猜测是,可能必须对系统上安装的不同python版本进行某些处理。所以我也尝试通过

安装
pip3 install --user pyaudio

Python版本

python --version
Python 2.7.10

python3 --version
Python 3.6.4

Python3是通过自制程序安装的。 brew info python返回:

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)

我不知道下一步该怎么做。任何帮助将不胜感激:)

0 个答案:

没有答案