2

I am performing a simple test to load Python libraries in a Swift app (below is the code). All the Python libraries load OK except librosa and pyaudio (yes I have them installed via pip and yes they work in native Python). When I run the app I get the error message "Python exception: No module named x", where x is librosa or pyaudio. The other Python libraries load and work perfectly. So, what do I have to do in order to get the librosa and pyaudio libraries to work in Swift?

let np = Python.import("numpy")
let plt = Python.import("matplotlib.pyplot")
let rosa = Python.import("librosa")
let audio = Python.import("pyaudio")
let display = Python.import("IPython.display")
let pd = Python.import("pandas")

My system:

macOS Mojave: Version 10.14.2 Beta (18C38b)
Xcode: Version 10.1 (10B61)
Toolchain: Swift for TensorFlow Development Snapshot 2018-10-17
David Cittadini
  • 369
  • 3
  • 15
  • did you try pip install -u librosa maybe? or sudo pip install librosa.... Also what happens when you type import librosa.display ? or run this sudo apt-get install python-pyaudio. If none of those work make sure you ran the setup.py for them – smitty_werbenjagermanjensen Nov 14 '18 at 21:43
  • Are you sure that you're using the same version of Python in Swift? Maybe you're using Py3 in one case and 2 in the other so it doesn't find the modules because you only have them in Py3 (or vice versa). – Marco Bonelli Nov 14 '18 at 21:43

1 Answers1

2

The Swift for TensorFlow Python module is built linked against the system Python. So, if you use something like homebrew to install Python2/Python3 then Swift for TensorFlow will not see those packages installed. Therefore, you have to make sure that you uninstall python@2 and use the system Python. The system Python does not include pip and so you will need to install that. Once you have installed pip you can see that Swift for TensorFlow has already installed a few Python libraries for you (which is why matplotlib.pyplot and numpy could be imported). Now, you can pip install the Python libraries you need and they will indeed now be seen inside Swift for TensorFlow.

David Cittadini
  • 369
  • 3
  • 15
  • This helped me. To make it more clear for others encountering this problem: Swift for TF uses the latest Python executable in `/Library/Frameworks/Python.framework/` **including** `pip`! So you need to use _that_ `pip` to install your required packaged. – Finn Gaida May 20 '19 at 15:59