Missing _bz when using fetch_openml // Missing _lzma when importing torchvision - scikit-learn

I am using Python 3.9.13. I installed scikit-learn from the terminal:
pip install scikit-learn
Then I tried to download the mnist dataset using fetch_openml:
from sklearn.datasets import fetch_openml
raw_data = fetch_openml('mnist_784')
That gave me a long error message ending with:
fetch_openml with as_frame=True requires pandas.
However, I had pandas installed. So I looked more deeply inside the error message and I found that the exception causing that error was this:
ModuleNotFoundError: No module named '_bz2'

I looked around and found a solution in this thread.
I only had to add another step to that solution.
After installing libbz2-dev I only had _bz2.cpython-38-x86_64-linux-gnu.so on my computer which is used for python 3.8.x so it did not work with my version of python.
I changed the file's name to _bz2.cpython-39-x86_64-linux-gnu.so and it worked after that.
sudo apt-get install libbz2-dev
sudo cp /usr/lib/python3.8/lib-dynload/_bz2.cpython-38-x86_64-linux-gnu.so /usr/local/lib/python3.9/
sudo mv /usr/local/lib/python3.9/_bz2.cpython-38-x86_64-linux-gnu.so /usr/local/lib/python3.9/_bz2.cpython-39-x86_64-linux-gnu.so

I had a similar issue with _lzma library when I wanted to import torchvision.
The issue was solved with running below lines in the terminal:
sudo apt install liblzma-dev
sudo cp /usr/lib/python3.8/lib-dynload/_lzma.cpython-38-x86_64-linux-gnu.so /usr/local/lib/python3.9/
sudo mv /usr/local/lib/python3.9/_lzma.cpython-38-x86_64-linux-gnu.so /usr/local/lib/python3.9/_lzma.cpython-39-x86_64-linux-gnu.so

Related

Python Uvicorn FastAPI Docker-Compose Error Torchaudio? [duplicate]

while using import soundfile on wither python3 or python I am getting:
Traceback (most recent call last):
File "", line 1, in
File "/home/erezsh/.local/lib/python3.6/site-packages/soundfile.py", line 142, in
raise OSError('sndfile library not found')
OSError: sndfile library not found
I found this post which did not help because I am using soundfile and not pysoundfile and the link it offers is broken.
Also, here I could not find a solution since I have installed using pip install SoundFile.
I am using Ubuntu shell on windows. Is this of any importance?
How do I solve this?
You need to install the needed library:
On Linux, you need to install libsndfile using your distribution’s package manager, for example sudo apt-get install libsndfile1.
From PyPI
handras answer (from 2019) is not working now (in 2020) so install libsndfile1 via ( as A.B.)
sudo apt-get install libsndfile1-dev
Try using the conda installation is you do not have sudo priviliges.
It worked for me, while I continued to face issues via the pip installation.
conda install -c conda-forge librosa
The above command installs librosa. You might be able to find a specific one for only soundfile too.
UPDATE: Needed to run apt update first, and it worked.
The accepted answer at first did not work for me.
I'm inside a docker image:
apt-get install libsndfile1
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libsndfile1
in case of yum package manager users first install the dependency
sudo yum -y install libsndfile
afterwards run the pip installs
pip install SoundFile
pip install librosa

Docker python unable to import module installed via apt-get

I'm trying to build a python app via docker, but it fails to import numpy even though I've installed the appropriate package via apt. As an example of the dockerfile reduced to only what's important here:
FROM python:3
RUN apt-get update \
&& apt-get install python3-numpy -y
RUN python3 -c "import numpy; print(numpy.__version__)"
Attempting to build that dockerfile results in the error ModuleNotFoundError: No module named 'numpy'.
I am able to get this working if I use pip to install numpy, but I was hoping to get it working with the apt-get package instead. Why isn't this working the way I would expect it to?
The problem is that you have two Pythons installed:
The image comes with python in /usr/local/bin.
When you install python3-numpy, that install python3 package from Debian, which ends up with /usr/bin/python.
When you run your code at the end you're likely using the version from /usr/local/bin, but NumPy was installed for the version in /usr/bin.
Solution: Install NumPy using pip, e.g. pip install numpy, instead of using apt.
Long version, with other ways you can get import errors: https://pythonspeed.com/articles/importerror-docker/
The problem is with python environments, not with docker. The apt-get installed numpy is not in the same environment as the python installation. Moreover, the dependencies should be stored in a requirements.txt file, which should then be installed through pip. python -m pip can be used to ensure that the pip command is in the same environment as the python installation.

Impossible to install python3 matplotlib.pyplot module with pip3 in Gitlab CI

I am part of a Gitlab python3 project where we are trying to install continuous integration thanks to the file .gitlab-ci.yml.
I am trying to install python3 and all the modules that we need, in order to execute a python test script.
I succeeded in installing python3 thanks to apk with the following command.
- apt-get -qq update && apt-get -qq install -y python3
I first need to install pandas, this is working well.
Next, I am trying to install the module matplotlib.pyplot, but it seems impossible, this command does not work:
- pip3 install matplotlib.pyplot
Collecting matplotlib.pyplot
Could not find a version that satisfies the requirement matplotlib.pyplot
(from versions: )
No matching distribution found for matplotlib.pyplot
Any idea on how to fix this issue? Thank you

ImportError:'save_weights' requires h5py

When I save weights during training my CNN model using keras, it says ImportError:'save_weights' requires h5py, but I have already installed h5py.
I would greatly appreciate if someone could explain how to fix this issue.
Just install necessary packages
sudo apt-get install libhdf5-dev
pip install h5py
If you are using windows and python IDE, open cmd and input following commands:
pip install h5py
pip install cython
I hope it helps.
I was getting the same error as you.
I installed all the requirements listed here: https://github.com/fchollet/keras/issues/3426
Finally just needed to reboot and it started working.
As suggested by others:
pip install h5py
Note that this may not immediately resolve the issue in your active session and you may need to reload keras.models either through the following commands or by just creating a new session/re-opening your jupyter notebook.
In Python3:
from importlib import reload
reload(keras.models)
In Python2:
use importlib.import_module instead. See docs for a reference.
These additional steps may be necessary because of the try/except ImportError in keras sourcecode that assigns h5py = None when it's unable to locate it the first time it's executed.
In my case, re-installing did the trick:
pip uninstall -y cython h5py
pip install cython h5py
(Windows 10, Conda, Keras 2.4.3)
I think you may miss this
from keras.applications import imagenet_utils
I got the same problem even though I have imported the h5py.
It is the load error with the keras. It has to be reloaded.
import keras
from importlib import reload
reload(keras.models)
It has worked for me.
h5py==2.10.0 works well with TF >= 2.1 so try 'pip install h5py==2.10.0'
Have you tried directly installing h5py? http://docs.h5py.org/en/latest/build.html
Try running:
pip install h5py
or
sudo apt-get install libhdf5

No module named pylab

I just upgraded to python3 on Fedora, and was trying to import pylab. But instead I got an error
ImportError: No module named 'pylab'
After some research I found some information about installing a package matplotlib-py3k to make it work. However, the link to the page is down! Maybe there is a yum install package I can use to simply install pylab to work with python3?
The package for python2.x is installed and working.
Try yum install python-matplotlib. If I am mistaken, then you can try from the source.
From the matplotlib website...
If you are on Fedora/RedHat, you can get all the dependencies required
to build matplotlib by first installing yum-builddep and then running:
su -c "yum-builddep python-matplotlib" This does not build matplotlib,
but it does get all of the build dependencies, which will make
building from source easier.
Then you can use git clone git#github.com:matplotlib/matplotlib.git to git matplotlib and then install with cd matplotlib and python setup.py install.
You need to install matplotlib for python3 by performing:
yum install python3-matplotlib

Resources