Swift + Python: No module named librosa or pyaudio - python

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

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.

Related

Importing the numpy C-extensions fails when importing a package that depends on numpy

When I import a package I built that depends on numpy (via pandas), the numpy import fails with the message below. VS Code is not in the picture.
I'm running macOS Monterey (12.3.1) on a MacBook Pro with an M1 chip.
I installed Python 3.10 from binary.
I'm using virtualenv and VS Code for development, but the failure also occurs when I run a script from Terminal.
I built a package (PyTables) that uses pandas and numpy: PyTables. It runs just fine in that project on my machine.
However, when install that built package in another project -- pip install -i https://test.pypi.org/simple/ pytables -- and then import it -- import pytables as tp -- I get the numpy import error below.
I've isolated the problem to just that import statement.
I installed pandas 1.4.1 and numpy 1.22.1 in this virtualenv using pip3, just as I did in the env where I built the package.
I also have Python 3.9.1 on my system. I need 3.10 for PySimpleGUI.
If I simply comment out the import, the code runs fine.
Import error:
ImportError: Unable to import required dependencies: numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
The Python version is: Python3.10 from
"/Users/alecramsay/.virtualenvs/tstudio/bin/python" * The NumPy
version is: "1.22.1"
and make sure that they are the versions you expect. Please carefully
study the documentation linked above for further help.
Original error was:
dlopen(/Users/alecramsay/.virtualenvs/tstudio/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so, 0x0002): tried:
'/Users/alecramsay/.virtualenvs/tstudio/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need
'x86_64'))

ImportError: No module named cv2 after install VS2017

Before:
Install VS2015.
Install opencv 3.2.0.
Now, i install VS2017 and create python project with the following code:
import cv2
When I run it, compiler show the error: "ImportError: No module named cv2"
How to update some libraries - which i installed before - as opencv, numpy,... for VS2017?
Thanks!
UPDATE 1
I knew the reason. Because when i installed VS2017, i also installed other version of Python. VS2017 used the wrong version (it does not have opencv library). And then i created new universal windows python project, VS2017 auto generate PythonUwpIoT (python 3.5 - i don't know and how to have it).

ImportError SciKit-learn

I am trying to get started with machine learning, so I have installed the packages: numpy, Scikit-learn, matplotlib, scipy. Some I have installed directly from pip with:
python -m pip install "package name"
and and others i have downloaded the binary files and then installed with pip. It shows no errors when I import matplotlib, numpy and sklearn, but when I write:
from sklearn import svm
it gives me the error:
ImportError: cannot import name 'svm'
I am on Python 3.5.1 and on Windows 10. Does anyone have any solutions?
import sklearn.svm as svm
model = svm.SVC()
....
http://scikit-learn.org/stable/modules/classes.html#module-sklearn.svm
It does seem that you didn't install it properly. Since you're on windows I would recommend using the Unofficial Windows Binaries for Python Extension Packages website to install future packages.
Make sure you also install the proper binaries as I pointed out in this post Installing scipy in Python 3.5 on 32-bit Windows 7 Machine. The windows version doesn't matter just make sure you're downloading Visual C++ 2015 redistributable package.

Install Opencv for Python on Windows

I try to install opencv for python. I use windows x64. I install numpy1.6 and python2.7. I have installed opencv2.4.9. I install them succesfully. I guide from this page. It says that
Now open your "Python IDLE" ( from Start > All Programs > Python 2.7 > Python IDLE ) and just type following :
import cv2
When I run import cv2, I get below error :
module compiled against API version 9 but this version of numpy is 6.
How can I know compatible version?
I know this question enter link description here. But I installed latest numpy version, it didn't solve my problem.
Use https://www.scivision.co/install-opencv-3-0-x-for-python-on-windows/
It's going to lead you to http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv where you can download a .whl installer. After you download it, you can run it using
pip install some-file.whl
assuming you have pip installed. After that you should be able to import cv2.
I had the same problem and I did a ton of research but this is the only one that worked.

Installing MayaVi on Windows, working with Python 2.7

I am trying to install the MayaVi package using pip, but I keep getting an error message saying (ImportError: No module named vtk). How do I fix this problem?
So on command prompt:
$pip install mayavi
output
File "Tvtk\code_gen.py", line 10, in
import vtk
ImportError: No module named vtk
This has actually gotten a lot easier with the new wheel format and installation.
Make sure your python setup is wheel compatible (e.g. upgrade pip and 'pip install wheel') - you may need to google around for how to do that for certain distributions like Canopy.
Then just grab either the VTK and MAYAVI wheels or the MAYAVI+VTK wheel from the inestimable http://www.lfd.uci.edu/~gohlke/pythonlibs/#vtk
Right now for example you might choose: mayavi‑4.4.0+vtk610‑cp27‑none‑win32.whl
(assuming 32 bit install of cpython 2.7, the filenames encode important stuff and there are many options)
Then run pip install mayavi‑4.4.0+vtk610‑cp27‑none‑win32.whl
I just had the occasion to do this on a laptop that I hadn't set up with Mayavi yet and it ran smooth as silk and installed everything I needed without complaint. That was several steps smoother than the last time I had to install Mayavi on a windows machine.
From my recent experience, one possible reason is that vtk for windows is not installed prior to using mayavi.
however you can't do pip install vtk.
you can get vtk from here
also, remember to configure VC Express 2008 and Windows 7 SDK + .NET 3.5 to avoid additional errors.

Categories