How can I upgrade NumPy? - python

When I installed OpenCV using Homebrew (brew), I got this problem whenever I run this command to test python -c "import cv2":
RuntimeError: module compiled against API version 9 but this version of numpy is 6
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import
I tried to upgrade NumPy, but this is confusing:
>>> import numpy
>>> print numpy.__version__
1.6.1
When I run brew to upgrade NumPy, I got this problem:
brew install -u numpy
Warning: numpy-1.9.1 already installed
When I uninstalled it:
sudo pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in ./anaconda/lib/python2.7/site-packages
I have followed this question and deleted Anaconda from my mac.
pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in /Library/Python/2.7/site-packages
But nothing have changed. How can I link the NumPy version to OpenCV?

When you already have an older version of NumPy, use this:
pip install numpy --upgrade
If it still doesn't work, try:
pip install numpy --upgrade --ignore-installed

Because we have two NumPy installations in the system. One is installed by Homebrew and the second is installed by pip. So in order to solve the problem, we need to delete one and use the default NumPy install by OpenCV.
Check the path,
import numpy
print numpy.__path__
and manually delete it using rm.

The error you mentioned happens when you have two versions of NumPy on your system. As you mentioned, the version of NumPy you imported is still not upgraded since you tried to upgrade it through pip (it will upgrade the version existing in '/Library/Python/2.7/site-packages' ).
However Python still loads the packages from '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy' where the pre-installed packages live.
In order to upgrade that version you have to use easy_install. The other way around this problem is using virtualenv and setting up a new environment with all the requirements you need.

Update numpy
For python 2
pip install numpy --upgrade
You would also needed to upgrade your tables as well for updated version of numpy. so,
pip install tables --upgrade
For python 3
pip3 install numpy --upgrade
Similarly, the tables for python3 :-
pip3 install tables --upgrade
note:
You need to check which python version are you using. pip for python 2.7+ or pip3 for python 3+

FYI, when you using or importing TensorFlow, a similar error may occur, like (caused by NumPy):
RuntimeError: module compiled against API version 0xa but this version of numpy is 0x9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 23, in <module>
from tensorflow.python import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 60, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
_pywrap_tensorflow = swig_import_helper()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: numpy.core.multiarray failed to import
Error importing tensorflow. Unless you are using bazel,
you should not try to import tensorflow from its source directory;
please exit the tensorflow source tree, and relaunch your python interpreter
from there.
I followed Elmira's and Drew's solution, sudo easy_install numpy, and it worked!
sudo easy_install numpy
Searching for numpy
Best match: numpy 1.11.3
Removing numpy 1.8.2 from easy-install.pth file
Adding numpy 1.11.3 to easy-install.pth file
Using /usr/local/lib/python2.7/dist-packages
Processing dependencies for numpy
Finished processing dependencies for numpy
After that I could use TensorFlow without error.

I tried doing sudo pip uninstall numpy instead, because the rm didn't work at first.
Hopefully that helps.
Uninstalling then to install it again.

Because you have multiple versions of NumPy installed.
Try pip uninstall numpy and pip list | grep numpy several times, until you see no output from pip list | grep numpy.
Then pip install numpy will get you the newest version of NumPy.

This works for me:
pip install numpy --upgrade

If you don't encounter any permission errors with
pip install -U numpy
try:
pip install --user -U numpy

All the same.
sudo easy_install numpy
My Traceback
Searching for numpy
Best match: numpy 1.13.0
Adding numpy 1.13.0 to easy-install.pth file
Using /Library/Python/2.7/site-packages
Processing dependencies for numpy

After installing pytorch, I got a similar error when I used:
import torch
Removing NumPy didn't help (I actually renamed NumPy, so I reverted back after it didn't work). The following commands worked for me:
sudo pip install numpy --upgrade
sudo easy_install numpy

If you are using multiple versions of Python (for example 3.8 and 3.9), then specify explicitly for which one you want to install and update numpy:
python3.8 -m pip install numpy --upgrade
# or
python3.9 -m pip install numpy --upgrade
Then run your program with the appropriate version of Python.

If you are stuck with a machine where you don't have root access, then it is better to deal with a custom Python installation.
The Anaconda installation worked like a charm:
Installing packages (SciPy.org)
Anaconda Python/R Distribution - download
After installation,
[bash]$ /xxx/devTools/python/anaconda/bin/pip list --format=columns |
grep numpy
numpy 1.13.3 numpydoc
0.7.0

pip install numpy --upgrade
You can try to use the ablove command and use as adminstrator if open CMD.

Related

package installed but cannot import it

I have successfully installed the bitarray package, because I can find it after the command: pip list.
But when I try to import it I get :
>>> from bitarray import bitarray
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from bitarray import bitarray
ModuleNotFoundError: No module named 'bitarray'
What can I try to solve it ?
I'm using Ubuntu 18.04.5 .
You can install package as python3 -m pip install bitarray. May be when you did pip install bitarray, it install your package for python2.
Ubuntu comes with two python versions. If you just ran pip install x it installed it into python 2.7. You're probably using python3, so you wanna install with pip3 install x
Just to make sure you are running correct pip, try running "pip -V" or "pip --version", it gives you which version of python it refers to. In terminal it looks something like this pip version check
Its also possible that you might have pip for say python 2.7, pip3 for python 3.6, and pip3.7 for python 3.7, if you have multiple versions of python installed.
For simplicity you can set the most frequently used pip version as pip by setting an alias in ~/.bashrc. This is done by adding the following line in bashrc:
alias pip=pip3.6.
After this you can try and install the packages and import it swiftly.

Why is my pip braking after updating

My pip broke after I ran
pip install --upgrade pip
When I run this command
pip -V
or any other commands I get
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip import main
ImportError: cannot import name main
Why is it breaking? I have python3 installed. I have a solution but I need to understand why it broke after upgrading to the latest version.
pip authors were warning for many years to not import pip. Finally at version 10 they restructured their code.
Your /usr/bin/pip was not upgraded (probably because pip install -U pip installed new /usr/local/bin/pip) so it uses the old API before renaming.
If that's the case I recommend to put /usr/local/bin before /usr/bin in $PATH.

python 2.7 functools_lru_cache does not import although installed

When I try to import matplotlib I get an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 128, in <module>
from matplotlib.rcsetup import defaultParams, validate_backend, cycler
File "/usr/local/lib/python2.7/dist-packages/matplotlib/rcsetup.py", line 29, in <module>
from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
File "/usr/local/lib/python2.7/dist-packages/matplotlib/fontconfig_pattern.py", line 32, in <module>
from backports.functools_lru_cache import lru_cache
ImportError: No module named functools_lru_cache
backports itself imports properly. When I try to install functools manually via
sudo pip install backports.functools_lru_cache
I get info that
Requirement already satisfied: backports.functools_lru_cache in /usr/local/lib/python2.7/dist-packages
Nevertheless when i try to
import backports.functools_lru_cache
I get info that
ImportError: No module named functools_lru_cache
System info
Ubuntu 16
Python 2.7.12
Pip 9.0.1
If someone is still having that problem and reinstalling backports.functools_lru_cache do not work in his case, as it was in my case, then probably installing older version of matplotlib would work. For example:
pip install matplotlib==2.0.2
Problem occurred for version 2.2.0, I switched to 2.0.2 and it is working now. I did not check other versions.
I had the same problem but I fixed it.
Uninstall first
pip uninstall backports.functools_lru_cache
and then re-install it.
pip install backports.functools_lru_cache
Now I'm able to import matplotlib. Hope this helps.
Install arrow using:
pip install arrow==0.12.0
fixed this issue for me
The pip command was actually the pip3, and the "ImportError" was happening when I used python (2.7).
pip2 uninstall backports.functools_lru_cache
then,
pip2 install backports.functools_lru_cache
fixed my problem.
You have to check what is the import path of backports package:
import backports
print('Backports Path: {0}'.format(backports.__path__))
1. The import path is the main python path ( the case of Matimath's question)
pip uninstall backports.functools_lru_cache (this will uninstall it from /usr/local/)
pip install backports.functools_lru_cache
2. The import path is the local usr dir (~/.local/, or %APPDATA%\Python for windows)
pip uninstall backports.functools_lru_cache
pip install --user backports.functools_lru_cache
Use pip2 command for python2.
The reason for this inconsistency is that the import path of backports package might have been changed during another module/package installation (eg. from backports.configparser module) - see here for more details:
https://bugs.python.org/issue31741
Following from Aditya Jain's answer,
[python -m] pip uninstall backports.functools_lru_cache
[python -m] pip install backports.functools_lru_cache==1.2.1
which will avoid installing arrow merely to downgrade functools_lru_cache.
I had the same problem and my solution was;
Download whl file from https://pypi.python.org/pypi/backports.functools_lru_cache/1.3
pip install
I solved my problem by removing the excessive matplotlib packages. I found out that when importing matplotlib it was attempting to import backports.functools_lru_cache and there it was throwing the Importerror.
I realized that I had different matplotlib packages in many locations:
/usr/lib/python2.7/dist-packages/matplotlib/
/usr/lib/python2.7/site-packages/matplotlib/
I removed the site-packages one. I left the dist-packages one intact.
Then I ran the following commands in python:
matplotlib.get_configdir()
matplotlib.get_cachedir()
and I removed the matplotlib packages in the output paths of these commands.
Then I removed the matplotlib in my virtualenvironment:
mv /home/username/virtualenvironment/lib/python2.7/matplotlib* /tmp
Finally I removed the one in the .local folder:
mv /home/username/.local/lib/python2.7/matplotlib* /tmp
Now importing matplotlib works fine. So when I run in python:
matplotlib.__file__
it returns
'/usr/lib/python2.7/dist-packages/matplotlib/__init__.pyc'
Now it does not throw error anymore when import backports.functools_lru_cache
You are using pyhton 2. try to use pip2 instead:
pip2 uninstall matplotlib
sudo apt-get autoremove python-matplotlib
sudo apt-get install python-matplotlib
If you're experiencing this issue on Ubuntu 18.x (and maybe other versions): it is caused by this Ubuntu bug. The problem is in the Ubuntu package python-configparser which is installed as a dependence of python-pip. It contains empty __init__ file
/usr/lib/python2.7/dist-packages/backports/__init__.py
Hence, pip modules installed to /usr/local/lib/python2.7/dist-packages/backports are simply ignored. As soon as this bug is not fixed yet, the easiest way to fix this is to remove this package:
sudo apt remove python-configparser
It does not any harm to any of your backports modules installed by pip (since they are in another folder), and this is much safer than version downgrade.
See also this pip issue with similar problem around backports module.
I had same issue, re-installation of backports.functools_lru_cache resolved the issue
Also meet this issue on Ubuntu 16.
Uninstall & reinstall not work for me.
My solution is reinstall from apt.
pip uninstall matplotlib
sudo apt-get autoremove python-matplotlib
sudo apt-get install python-matplotlib
This worked for me. Ubuntu 18.04.
sudo pip2 uninstall backports.functools-lru-cache
sudo apt install python-backports.functools-lru-cache
this worked for me
from backports.functools_lru_cache import lru_cache

Pip install Numpy Errors

I tried to upgrade pandas from 19.2 to 20.2
I did pip -install --upgrade pandas
and it ran successfully. However, now when I try to import pandas I get:
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
I uninstall and re-install numpy with pip uninstall numpy and pip install numpy. But it seems like numpy is using a cashed version. I did install numpy MKL a while back....:
C:\Windows\System32>pip install numpy
Collecting numpy
Using cached numpy-1.13.0-cp36-none-win32.whl
Installing collected packages: numpy
Successfully installed numpy-1.13.0
How do i install the "normal" version of numpy? As I believe this cached version is causing the issues
Go to this link and download the relevant numpy .whl file:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Then navigate to the directory from which it is downloaded to and run the pip install command for that file.
Hope this helps!
Do you use anaconda ? Try to run this script in cmd :
"conda install numpy"
Another solution might be to delete .pyc file your python project created. Python interpreter compiles source to byte code, which is stored in .pyc and byte code is afterwars executed by Pythons virtual machine.

I cant import sklearn

I try to import scikit-learn, but there is an error. i installed sklearn, scipy on anaconda. i am using W10 and python 3.5.
>>> import sklearn
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import sklearn
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\__init__.py", line 57, in <module>
from .base import clone
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\site-packages\sklearn\base.py", line 9, in <module>
from scipy import sparse
ImportError: No module named 'scipy'
In linux there is pip install <module> to install a module, and if you are using anaconda then conda install <module>, I believe there would be something similar in windows.
If you are sure that you have installed scipy module, then probably the python path is not looking for those directories.
You can try a environment variable PYTHONPATH that has a list of directories to append before launching python prompt. OR you can test it for a session by adding it to sys.path
Use pip to install the packages
pip install numpy
pip install scipy
pip install -U scikit-learn
Ensure you have appropriate privileges for installing globally or in virtual environment.
Using pip, or interpreter setting in pycharm:
pip install NumPy+mkl
numpy-mkl 1.10.2
Install module NumPy+mkl
pip install SciPy
Install module SciPy
Now you can install sklearn.
pip install scikit-learn
Install module scikit-learn
Hope that it is useful.

Categories

Resources