Problems with installing Numpy in Python 2.7.5 [duplicate] - python

Code:
import numpy as np
import cv
Console:
>>> runfile('/Users/isaiahnields/.spyder2/temp.py', wdir='/Users/isaiahnields/.spyder2')
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/isaiahnields/.spyder2/temp.py", line 9, in <module>
import cv
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: numpy.core.multiarray failed to import
>>>
System Info: OS X El Capitan, Macbook Air, 1.3 GHz Intel Core i5, 8 GB 1600 HMz DDR3
I have already attempted updating numpy. I had to add cv.py to the python2.7 folder in Spyder-Py2 is there something else I need to add?

Upgrade numpy to the latest version
pip install numpy --upgrade

Check the path
import numpy
print numpy.__path__
For me this was /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy So I moved it to a temporary place
sudo mv /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy \
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy_old
and then the next time I imported numpy the path was /Library/Python/2.7/site-packages/numpy/init.pyc and all was well.

This worked for me:
sudo pip install numpy --upgrade --ignore-installed

You are likely running the Mac default (/usr/bin/python) which has an older version of numpy installed in the system folders. The easiest way to get python working with opencv is to use brew to install both python and opencv into /usr/local and run the /usr/local/bin/python.
brew install python
brew tap homebrew/science
brew install opencv

I ran into the same issue tonight. It turned out to be a problem where I had multiple numpy packages installed. An older version was installed in /usr/lib/python2.7 and the correct version was installed in /usr/local/lib/python2.7.
Additionally, I had PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages. PYTHONPATH was finding the older version of numpy before the correct version, so when inside the Python interpreter, it would import the older version of numpy.
One thing which helped was opening a python session an executing the following code:
import numpy as np
print np.__version__
print np.__path__
That should tell you exactly which version Python is using, and where it's installed.
To fix the issue, I changed PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages. And I also setup a virtual Python environment using the Hitchiker's Guide to Python, specifically the section titled "Lower level: virtualenv" . I know I should have setup a virtual environment in the first place, but I was tired and being lazy. Oh well, lesson learned!
(Update)
Just in case the docs are moved again, here are the relevant bits on...
Creating a Python Virtual Environment
Install virtualenv via pip:
$ install virtualenv
Test the installation:
$ virtualenv --version
Optionally, et the environment variable VIRTUALENVWRAPPER_PYTHON to change the default version of python used by virtual environments, for example to use Python 3:
$ export VIRTUALENVWRAPPER_PYTHON=$(which python3)
Optionally, set the environment variable WORKON_HOME to change the default directory your Python virtual environments are created in, for example to use /opt/python_envs:
$ export WORKON_HOME=/opt/python_envs
Create a virtual environment for a project:
$ cd my_project_folder
$ virtualenv my_virtual_env_name
Activate the virtual environment, you just created. Assuming you also set WORKON_HOME=/opt/python_envs:
$ source $WORKON_HOME/my_virtual_env_name/bin/activate
Install whatever Python packages your project requires, using either of the following two methods.
Method 1 - Install using pip from command line:
$ pip install python_package_name1
$ pip install python_package_name2
Method 2 - Install using a requests.txt file:
$ echo "python_package_name1" >> requests.txt
$ echo "python_package_name2" >> requests.txt
$ pip install -r ./requests.txt
Optionally, but highly recommended, install virtualenvwrapper. It contains useful commands to make working with virtual Python environments easier:
$ pip install virtualenvwrapper
$ source /usr/local/bin/virtualenvwrapper.sh
On Windows, install virtualenvwrapper using:
$ pip install virtualenvwrapper-win
Basic usage of virtualenvwrapper
Create a new virtual environment:
$ mkvirtualenv my_virtual_env_name
List all virtual environments:
$ lsvirtualenv
Activate a virtual environment:
$ workon my_virtual_env_name
Delete a virtual environment (caution! this is irreversible!):
$ rmvirtualenv my_virtual_env_name
I hope this help!

To solve the problem do following:
First uninstall numpy
sudo pip uninstall numpy
Install numpy with --no-cache-dir option
sudo pip install --no-cache-dir numpy
And to specify any specific version e.g. 1.14.2
sudo pip install --no-cache-dir numpy==1.14.2

This command solved my problem.
pip3 install --upgrade numpy

upgrading numpy to rescue
numpy official document suggests users to do upgrade to solve this issue [1].
pip install numpy --upgrade
which version of numpy should I upgrade to
But you may upgrade to a version that is too new/old for your environment. I spent a long time trying to figure out which version of numpy is expected to be upgraded to when running into this error, and here is a list [2] of numpy versions with their corresponding C API versions, which may be useful for troubleshooting such an issue:
# 0x00000008 - 1.7.x
# 0x00000009 - 1.8.x
# 0x00000009 - 1.9.x
# 0x0000000a - 1.10.x
# 0x0000000a - 1.11.x
# 0x0000000a - 1.12.x
# 0x0000000b - 1.13.x
# 0x0000000c - 1.14.x
# 0x0000000c - 1.15.x
# 0x0000000d - 1.16.x
# 0x0000000d - 1.19.x
# 0x0000000e - 1.20.x
# 0x0000000e - 1.21.x
# 0x0000000f - 1.22.x
# 0x00000010 - 1.23.x
# 0x00000010 - 1.24.x
You can find the list here [2].
And the C API VERSION in numpy is tracked in three places according to [3]:
numpy/core/setup_common.py
numpy/core/code_generators/cversions.txt
numpy/core/include/numpy/numpyconfig.h
The error is reported by numpy's source code here [4]
references
[1] https://numpy.org/devdocs/user/troubleshooting-importerror.html#c-api-incompatibility
[2] https://github.com/numpy/numpy/blob/main/numpy/core/setup_common.py
[3] https://numpy.org/doc/stable/dev/releasing.html#check-the-c-api-version-number
[4] https://github.com/numpy/numpy/blob/bdec32181605c8179fd79624d14c1cf019de75af/numpy/core/code_generators/generate_numpy_api.py#L79

I got the same issue with quaternion module. When updating modules with conda, the numpy version is not up^dated to the last one. If forcing update with pip command pip install --upgrade numpy + install quaternion module by pip install --user numpy numpy-quaternion, the issue is fixed.
May be the issue is coming from the numpy version.
Here the execution result:
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> print np.__version__
1.14.3
>>>
(base) C:\Users\jc>pip install --user numpy numpy-quaternion
Requirement already satisfied: numpy in d:\programdata\anaconda2\lib\site-packages (1.14.3)
Collecting numpy-quaternion
Downloading https://files.pythonhosted.org/packages/3e/73/5720d1d0a95bc2d4af2f7326280172bd255db2e8e56f6fbe81933aa00006/numpy_quaternion-2018.5.10.13.50.12-cp27-cp27m-win_amd64.whl (49kB)
100% |################################| 51kB 581kB/s
Installing collected packages: numpy-quaternion
Successfully installed numpy-quaternion-2018.5.10.13.50.12
(base) C:\Users\jc>python
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import quaternion
>>>

I faced the same problem due to documentation inconsistencies.
This page says the examples in the docs work best with python 3.x: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_intro/py_intro.html#intro , whereas this installation page has links to python 2.7, and older versions of numpy and matplotlib: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html
My setup was as such: I already had Python 3.6 and 3.5 installed, but since OpenCv-python docs said it works best with 2.7.x, I also installed that version. After I installed numpy (in Python27 directory, without pip but with the default extractor, since pip is not part of the default python 2.7 installation like it is in 3.6), I ran in this RuntimeError: module compiled against API version a but this version of numpy is error. I tried many different versions of both numpy and opencv, but to no avail. Lastly, I simply deleted numpy from python27 (just delete the folder in site-packages as well as any other remaining numpy-named files), and installed the latest versions of numpy, matplotlib, and opencv in the Python3.6 version using pip no problem. Been running opencv ever since.
Hope this saves somebody some time.

When all else fail, check with the following script and disable unwanted python import path(s), or upgrade the package on those paths:
python ./test.py
test.py content:
import numpy as np
print(f'numpy version:{np.__version__}')
import sys
from pprint import pprint
pprint(sys.path)
import tensorflow as tf
print(f'TensorFlow version: {tf.__version__}')
For my case, it was the outdated conda version in ~/.local/lib/python3.8/site-packages that was messing things up :(

For those using anaconda Python:
conda update anaconda

You might want to check your matplotlib version.
Somehow I installed a dev version of matplotlib which caused the issue. A downgrade to stable release fixed it.
One can also can try python -v -c 'import YOUR_PACKAGE' 2>&1 | less to see where the issue occurred and if the lines above error can give you some hints.

You may also want to check your $PYTHONPATH. I had changed mine in ~/.bashrc in order to get another package to work.
To check your path:
echo $PYTHONPATH
To change your path (I use nano but you could edit another way)
nano ~/.bashrc
Look for the line with export PYTHONPATH ...
After making changes, don't forget to
source ~/.bashrc

I had the same error when trying to launch spyder. "RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa".
This error appeared once I modified the numpy version of my machine by mistake (I thought I was in a venv). If your are using spyder installed with conda, my advice is to only use conda to manage package.
This works for me:
conda install anaconda
(I had conda but no anaconda on my machine)
then:
conda update numpy

The below command worked for me :
conda install -c anaconda numpy

Although this question is very old, but I do believe there are still many facing similar problem as I did. I encountered the above reported error when I used Python3 in a Raspberry Pi micro-computer, which is running on Raspberry Pi OS.
This is perhaps due to missing some libraries when installed the Numpy module. I solved this problem following the suggestion in the Numpy website.
Solutions for Numpy Module Import Error
This Numpy troubleshooting website is really informative and provides cross-platform solutions for Windows, Anaconda, Raspberry, etc. Perhaps, someone can first follow the suggestion in this Numpy official website in order to solve the error.

I had same issue when I used import pyopencl and I did not want to upgrade numpy cause tensorflow requires old version of numpy so I solved it by simply:
python -m pip uninstall pyopencl && python -m pip install pyopencl
This way pyopencl was configured with existing numpy version and error solved.

I suffered with this problem for a long time, firstly you have to upgrade numby then try this code :
import numpy as np
print np.__version__
if gives you different version from the new one , uninstall the numpy(the new version) and use this
print numpy.__path__
go to that old numpy and delete the file , then install new version again

This works for me:
My pip is not work after upgrade, so the first thing I need to do is to fix it with
sudo gedit /usr/bin/pip
Change the line
from pip import main
to
from pip._internal import main
Then,
sudo pip install -U numpy

Related

RuntimeError: module compiled against API version 0xf but this version of numpy is 0xe [duplicate]

Code:
import numpy as np
import cv
Console:
>>> runfile('/Users/isaiahnields/.spyder2/temp.py', wdir='/Users/isaiahnields/.spyder2')
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/isaiahnields/.spyder2/temp.py", line 9, in <module>
import cv
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: numpy.core.multiarray failed to import
>>>
System Info: OS X El Capitan, Macbook Air, 1.3 GHz Intel Core i5, 8 GB 1600 HMz DDR3
I have already attempted updating numpy. I had to add cv.py to the python2.7 folder in Spyder-Py2 is there something else I need to add?
Upgrade numpy to the latest version
pip install numpy --upgrade
Check the path
import numpy
print numpy.__path__
For me this was /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy So I moved it to a temporary place
sudo mv /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy \
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy_old
and then the next time I imported numpy the path was /Library/Python/2.7/site-packages/numpy/init.pyc and all was well.
This worked for me:
sudo pip install numpy --upgrade --ignore-installed
You are likely running the Mac default (/usr/bin/python) which has an older version of numpy installed in the system folders. The easiest way to get python working with opencv is to use brew to install both python and opencv into /usr/local and run the /usr/local/bin/python.
brew install python
brew tap homebrew/science
brew install opencv
I ran into the same issue tonight. It turned out to be a problem where I had multiple numpy packages installed. An older version was installed in /usr/lib/python2.7 and the correct version was installed in /usr/local/lib/python2.7.
Additionally, I had PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages. PYTHONPATH was finding the older version of numpy before the correct version, so when inside the Python interpreter, it would import the older version of numpy.
One thing which helped was opening a python session an executing the following code:
import numpy as np
print np.__version__
print np.__path__
That should tell you exactly which version Python is using, and where it's installed.
To fix the issue, I changed PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages. And I also setup a virtual Python environment using the Hitchiker's Guide to Python, specifically the section titled "Lower level: virtualenv" . I know I should have setup a virtual environment in the first place, but I was tired and being lazy. Oh well, lesson learned!
(Update)
Just in case the docs are moved again, here are the relevant bits on...
Creating a Python Virtual Environment
Install virtualenv via pip:
$ install virtualenv
Test the installation:
$ virtualenv --version
Optionally, et the environment variable VIRTUALENVWRAPPER_PYTHON to change the default version of python used by virtual environments, for example to use Python 3:
$ export VIRTUALENVWRAPPER_PYTHON=$(which python3)
Optionally, set the environment variable WORKON_HOME to change the default directory your Python virtual environments are created in, for example to use /opt/python_envs:
$ export WORKON_HOME=/opt/python_envs
Create a virtual environment for a project:
$ cd my_project_folder
$ virtualenv my_virtual_env_name
Activate the virtual environment, you just created. Assuming you also set WORKON_HOME=/opt/python_envs:
$ source $WORKON_HOME/my_virtual_env_name/bin/activate
Install whatever Python packages your project requires, using either of the following two methods.
Method 1 - Install using pip from command line:
$ pip install python_package_name1
$ pip install python_package_name2
Method 2 - Install using a requests.txt file:
$ echo "python_package_name1" >> requests.txt
$ echo "python_package_name2" >> requests.txt
$ pip install -r ./requests.txt
Optionally, but highly recommended, install virtualenvwrapper. It contains useful commands to make working with virtual Python environments easier:
$ pip install virtualenvwrapper
$ source /usr/local/bin/virtualenvwrapper.sh
On Windows, install virtualenvwrapper using:
$ pip install virtualenvwrapper-win
Basic usage of virtualenvwrapper
Create a new virtual environment:
$ mkvirtualenv my_virtual_env_name
List all virtual environments:
$ lsvirtualenv
Activate a virtual environment:
$ workon my_virtual_env_name
Delete a virtual environment (caution! this is irreversible!):
$ rmvirtualenv my_virtual_env_name
I hope this help!
To solve the problem do following:
First uninstall numpy
sudo pip uninstall numpy
Install numpy with --no-cache-dir option
sudo pip install --no-cache-dir numpy
And to specify any specific version e.g. 1.14.2
sudo pip install --no-cache-dir numpy==1.14.2
This command solved my problem.
pip3 install --upgrade numpy
upgrading numpy to rescue
numpy official document suggests users to do upgrade to solve this issue [1].
pip install numpy --upgrade
which version of numpy should I upgrade to
But you may upgrade to a version that is too new/old for your environment. I spent a long time trying to figure out which version of numpy is expected to be upgraded to when running into this error, and here is a list [2] of numpy versions with their corresponding C API versions, which may be useful for troubleshooting such an issue:
# 0x00000008 - 1.7.x
# 0x00000009 - 1.8.x
# 0x00000009 - 1.9.x
# 0x0000000a - 1.10.x
# 0x0000000a - 1.11.x
# 0x0000000a - 1.12.x
# 0x0000000b - 1.13.x
# 0x0000000c - 1.14.x
# 0x0000000c - 1.15.x
# 0x0000000d - 1.16.x
# 0x0000000d - 1.19.x
# 0x0000000e - 1.20.x
# 0x0000000e - 1.21.x
# 0x0000000f - 1.22.x
# 0x00000010 - 1.23.x
# 0x00000010 - 1.24.x
You can find the list here [2].
And the C API VERSION in numpy is tracked in three places according to [3]:
numpy/core/setup_common.py
numpy/core/code_generators/cversions.txt
numpy/core/include/numpy/numpyconfig.h
The error is reported by numpy's source code here [4]
references
[1] https://numpy.org/devdocs/user/troubleshooting-importerror.html#c-api-incompatibility
[2] https://github.com/numpy/numpy/blob/main/numpy/core/setup_common.py
[3] https://numpy.org/doc/stable/dev/releasing.html#check-the-c-api-version-number
[4] https://github.com/numpy/numpy/blob/bdec32181605c8179fd79624d14c1cf019de75af/numpy/core/code_generators/generate_numpy_api.py#L79
I got the same issue with quaternion module. When updating modules with conda, the numpy version is not up^dated to the last one. If forcing update with pip command pip install --upgrade numpy + install quaternion module by pip install --user numpy numpy-quaternion, the issue is fixed.
May be the issue is coming from the numpy version.
Here the execution result:
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> print np.__version__
1.14.3
>>>
(base) C:\Users\jc>pip install --user numpy numpy-quaternion
Requirement already satisfied: numpy in d:\programdata\anaconda2\lib\site-packages (1.14.3)
Collecting numpy-quaternion
Downloading https://files.pythonhosted.org/packages/3e/73/5720d1d0a95bc2d4af2f7326280172bd255db2e8e56f6fbe81933aa00006/numpy_quaternion-2018.5.10.13.50.12-cp27-cp27m-win_amd64.whl (49kB)
100% |################################| 51kB 581kB/s
Installing collected packages: numpy-quaternion
Successfully installed numpy-quaternion-2018.5.10.13.50.12
(base) C:\Users\jc>python
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import quaternion
>>>
I faced the same problem due to documentation inconsistencies.
This page says the examples in the docs work best with python 3.x: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_intro/py_intro.html#intro , whereas this installation page has links to python 2.7, and older versions of numpy and matplotlib: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html
My setup was as such: I already had Python 3.6 and 3.5 installed, but since OpenCv-python docs said it works best with 2.7.x, I also installed that version. After I installed numpy (in Python27 directory, without pip but with the default extractor, since pip is not part of the default python 2.7 installation like it is in 3.6), I ran in this RuntimeError: module compiled against API version a but this version of numpy is error. I tried many different versions of both numpy and opencv, but to no avail. Lastly, I simply deleted numpy from python27 (just delete the folder in site-packages as well as any other remaining numpy-named files), and installed the latest versions of numpy, matplotlib, and opencv in the Python3.6 version using pip no problem. Been running opencv ever since.
Hope this saves somebody some time.
When all else fail, check with the following script and disable unwanted python import path(s), or upgrade the package on those paths:
python ./test.py
test.py content:
import numpy as np
print(f'numpy version:{np.__version__}')
import sys
from pprint import pprint
pprint(sys.path)
import tensorflow as tf
print(f'TensorFlow version: {tf.__version__}')
For my case, it was the outdated conda version in ~/.local/lib/python3.8/site-packages that was messing things up :(
For those using anaconda Python:
conda update anaconda
You might want to check your matplotlib version.
Somehow I installed a dev version of matplotlib which caused the issue. A downgrade to stable release fixed it.
One can also can try python -v -c 'import YOUR_PACKAGE' 2>&1 | less to see where the issue occurred and if the lines above error can give you some hints.
You may also want to check your $PYTHONPATH. I had changed mine in ~/.bashrc in order to get another package to work.
To check your path:
echo $PYTHONPATH
To change your path (I use nano but you could edit another way)
nano ~/.bashrc
Look for the line with export PYTHONPATH ...
After making changes, don't forget to
source ~/.bashrc
I had the same error when trying to launch spyder. "RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa".
This error appeared once I modified the numpy version of my machine by mistake (I thought I was in a venv). If your are using spyder installed with conda, my advice is to only use conda to manage package.
This works for me:
conda install anaconda
(I had conda but no anaconda on my machine)
then:
conda update numpy
The below command worked for me :
conda install -c anaconda numpy
Although this question is very old, but I do believe there are still many facing similar problem as I did. I encountered the above reported error when I used Python3 in a Raspberry Pi micro-computer, which is running on Raspberry Pi OS.
This is perhaps due to missing some libraries when installed the Numpy module. I solved this problem following the suggestion in the Numpy website.
Solutions for Numpy Module Import Error
This Numpy troubleshooting website is really informative and provides cross-platform solutions for Windows, Anaconda, Raspberry, etc. Perhaps, someone can first follow the suggestion in this Numpy official website in order to solve the error.
I had same issue when I used import pyopencl and I did not want to upgrade numpy cause tensorflow requires old version of numpy so I solved it by simply:
python -m pip uninstall pyopencl && python -m pip install pyopencl
This way pyopencl was configured with existing numpy version and error solved.
I suffered with this problem for a long time, firstly you have to upgrade numby then try this code :
import numpy as np
print np.__version__
if gives you different version from the new one , uninstall the numpy(the new version) and use this
print numpy.__path__
go to that old numpy and delete the file , then install new version again
This works for me:
My pip is not work after upgrade, so the first thing I need to do is to fix it with
sudo gedit /usr/bin/pip
Change the line
from pip import main
to
from pip._internal import main
Then,
sudo pip install -U numpy

"import torch" giving error "from torch._C import *, DLL load failed: The specified module could not be found"

I am currently using Python 3.5.5 on Anaconda and I am unable to import torch. It is giving me the following error in Spyder:
Python 3.5.5 |Anaconda, Inc.| (default, Mar 12 2018, 17:44:09) [MSC v.1900
64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 6.2.1 -- An enhanced Interactive Python.
import torch
Traceback (most recent call last):
File "<ipython-input-1-eb42ca6e4af3>", line 1, in <module>
import torch
File "C:\Users\trish\Anaconda3\envs\virtual_platform\lib\site-
packages\torch\__init__.py", line 76, in <module>
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
Many suggestions on the internet say that the working directory should not be the same directory that the torch package is in, however I've manually set my working directory to C:/Users/trish/Downloads, and I am getting the same error.
Also I've already tried the following: reinstalling Anaconda and all packages from scratch, and I've ensured there is no duplicate "torch" folder in my directory.
Pls help! Thank you!
I had this similar problem in windows 10...
Solution:
Download win-64/intel-openmp-2018.0.0-8.tar.bz2 from https://anaconda.org/anaconda/intel-openmp/files
Extract it and put the dll files in Library\bin into C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin
Make sure your cuda directory is added to your %PATH% environment variable
I had the same problem. In my case I didn't want the GPU version of pytorch.
I uninstalled it. The version was pytorch: 0.3.1-py36_cuda80_cudnn6he774522_2 peterjc123.
The problem is that cuda and cudnn . then installed with the following command and now it works!
conda install -c peterjc123 pytorch-cpu
I also encountered the same problem when I used a conda environment with python 3.6.8 and pytorch installed by conda from channel -c pytorch.
Here is what worked for me:
1:) conda create -n envName python=3.6 anaconda
2:) conda update -n envName conda
3:) conda activate envName
4:) conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
and then tested torch with the given code:
5:) python -c "import torch; print(torch.cuda.get_device_name(0))"
Note: 5th step will return your gpu name if you have a cuda compatible gpu
Summary: I just created a conda environment containing whole anaconda and then to tackle the issue of unmatched conda version I updated conda of new environment from the base environment and then installed pytorch in that environment and tested pytorch.
For CPU version, here is the link for my another answer: https://gist.github.com/peterjc123/6b804651288e76db7b5fabe5348e1f03#gistcomment-2842825
https://gist.github.com/peterjc123/6b804651288e76db7b5fabe5348e1f03#gistcomment-2842837
Had the same problem and fixed it by re-installing numpy with mkl (Intel's math kernel library)
https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
Download the right .whl for your machine. For me it was numpy‑1.14.5+mkl‑cp36‑cp36m‑win_amd64.whl (python 3.6, windows, 64-bit)
and then install using pip.
pip install numpy‑1.14.5+mkl‑cp36‑cp36m‑win_amd64.whl
I am using a Windows 10 computer with an NVIDIA GeForce graphics card. NVIDIA showed I had CUDA 10.1, but I was getting this error when running import torch in Jupyter Lab and suspected it had something to do with CUDA support.
I fixed this problem by downloading and installing the CUDA Toolkit directly from NVIDIA. It installed all required Visual Studio components. When I returned to Jupyter Lab, import torch ran without error.
Make sure you installed the right version of pytorch for your enviroment. I had the same problem I was using pytorch on windows but I had the default package installed which was meant for cuda 8. So I reinstalled the pytorch package for cpu which was what I needed.
I had the same issue with running torch installed with pure pip and solved it by switching to conda.
Following steps:
uninstall python 3.6 from python.org (if exists)
install miniconda
install torch in conda ("conda install pytorch -c pytorch")
Issue with pip installation:
import torch
File "C:\Program Files\Python35\lib\site-packages\torch\__init__.py", line 78, in <module>
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
After switching to conda it works fine. I believe the issue was resolved by conda through installing the vs_redist 2017
vs2017_runtime 15.4.27004.2010 peterjc123
But I have tried it w/o conda and it did not help. Could not find how to check (and tweak) Python's vs_redist.
Windows10 Solution(This worked for my system):
I was having the same issue in my system. Previously I was using Python 3.5 and I created a virtual environment named pytorch_test using the virtualenv module because I didn't want to mess up my tensorflow installation(which took me a lot of time). I followed every instruction but it didn't seem to work. I installed python 3.6.7 added it to the path. Then I created the virtual environment using:
virtualenv --python=3.6 pytorch_test
Then go to the destination folder
cd D:\pytorch_test
and activate the virtual environment entering the command in cmd:
.\Scripts\activate
After you do this the command prompt will show:
(pytorch_test) D:\pytorch_test>
Update pip if you have not done it before using:
(pytorch_test) D:\pytorch_test>python -m pip install --upgrade pip
Then go for installing numpy+mkl from the site:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
Choose the correct version from the list if you have python 3.6.7 go with the wheel file:
numpy‑1.15.4+mkl‑cp36‑cp36m‑win_amd64.whl (For 64 bit)
(Note if the whole thing doesnot work just go with simple numpy installation and mkl installation separately)
Then go for installing openmp using:
(pytorch_test) D:\pytorch_test>pip install intel-openmp
Now you are done with the prerequisites. To install pytorch go to the previous versions site:
https://pytorch.org/get-started/previous-versions/
Here select the suitable version from the list of Windows Binaries. For example I am having CUDA 9.0 installed in my system with python 3.6.7 so I went with the gpu version:
cu90/torch-1.0.0-cp36-cp36m-win_amd64.whl
(There are two available versions 0.4.0 and 1.0.0 for pytorch, I went with 1.0.0)
After downloading the file install it using pip(assuming the whl file is in D:).You have to do this from the virtual environment pytorch_test itself:
(pytorch_test) D:\pytorch_test>pip install D:\torch-1.0.0-cp36-cp36m-win_amd64.whl
Prerequisites like six, pillow will be installed automatically.
Then once everything is done, install the models using torchvision.
Simply type :
(pytorch_test) D:\pytorch_test>pip install torchvision
To check everything is working fine try the following script:
import torch
test = torch.rand(4, 7)
print(test)
If everything was good then it wont be an issue. Whenever there is an issue like this it is related to version mismatch of one or more dependencies. This also occurred during tensorflow installation.
Deactivate the following virtual environment using the command deactivate in the cmd:
(pytorch_test) D:\pytorch_test>deactivate
This is the output of pip list in my system:
Package Version
------------ -----------
intel-openmp 2019.0
mkl 2019.0
numpy 1.16.2
Pillow 6.0.0
pip 19.0.3
setuptools 41.0.0
six 1.12.0
torch 1.0.0
torchvision 0.2.2.post3
wheel 0.33.1
Hope this helps. This is my first answer in this community, hope you all find it helpful. I setup pytorch today in the afternoon after trying all sorts of combinations. The same import problem occurred to me while installing CNTK and tensorflow. Anyway I kept them separate in different virtual environments so that I can use them anytime.

Jupyter notebook picks older version of numpy

I am using python 2.7.6 and trying to import pandas but Jupyter notebook gives me following
error--
ImportError Traceback (most recent call
last) in ()
----> 1 import pandas
/usr/local/lib/python2.7/dist-packages/pandas/init.py in
()
21
22 # numpy compat
---> 23 from pandas.compat.numpy import *
24
25 try:
/usr/local/lib/python2.7/dist-packages/pandas/compat/numpy/init.py
in ()
22 'your numpy version is {0}.\n'
23 'Please upgrade numpy to >= 1.9.0 to use '
---> 24 'this pandas version'.format(_np_version))
25
26
ImportError: this version of pandas is incompatible with numpy < 1.9.0
your numpy version is 1.8.2. Please upgrade numpy to >= 1.9.0 to use
this pandas version
while I have numpy 1.14.1
$ pip freeze
numpy==1.14.1
pandas==0.22.0
pip-magic==0.2.3
python-dateutil==2.6.1
pytz==2018.3
six==1.11.0
I have tried uninstalling and then reinstalling numpy and pandas from ubuntu terminal as well as Jupyter's terminal but unable to solve the error, any help would be appreciated. Thanks.
I could imagine that you have different versions of Python on your computer. In the Jupyter Notebook try running
import sys
sys.executable
This will show you which Python interpreter is used
EDIT:
You can install a new kernel for jupyter that uses the correct Python interpreter. First get a list of the existing kernels that you have:
Type jupyter kernelspec list. If the wanted interpreter is not there you will have to install it. To do so use python -m ipykernel install --name <Kernelname> --display-name <Displayname> Note that the python interpreter you are using to run this command must be the python interpreter that you want to be used by the kernel.
Now you are set up!
Start a new notebook and you will find that you can choose this new kernel
After trying multiple things I was able to resolve it.
The issue here was that python2.7 had older version of numpy i.e. 1.8.2 as the error in the question reports.
In python shell I found the path of numpy in use as follow.
import numpy
print numpy.__path__
the output was this path
/usr/local/lib/python2.7/dist-packages/numpy
went to /usr/local/lib/python2.7/dist-packages and deleted numpy using
sudo rm -r numpy
then staying in dist-packages directory I installed numpy 1.14.1 simply with pip.
sudo pip install numpy==1.14.1
this solved my problem.
One way to make sure that you use the right version or jupyter is the -m option of python:
-m mod : run library module as a script (terminates option list)
If this shows NumPy 1.14:
python -m pip freeze
starting jupyter like this should also give you the same version:
python -m jupyter notebook
you can find what versions of python are on your system and in what order they are searched with:
which -a python
Why don't you rm -rf the numpy library and install the version he is asking you too.
sudo pip install numpy==1.14.1

RuntimeError: module compiled against API version a but this version of numpy is 9

Code:
import numpy as np
import cv
Console:
>>> runfile('/Users/isaiahnields/.spyder2/temp.py', wdir='/Users/isaiahnields/.spyder2')
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/isaiahnields/.spyder2/temp.py", line 9, in <module>
import cv
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: numpy.core.multiarray failed to import
>>>
System Info: OS X El Capitan, Macbook Air, 1.3 GHz Intel Core i5, 8 GB 1600 HMz DDR3
I have already attempted updating numpy. I had to add cv.py to the python2.7 folder in Spyder-Py2 is there something else I need to add?
Upgrade numpy to the latest version
pip install numpy --upgrade
Check the path
import numpy
print numpy.__path__
For me this was /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy So I moved it to a temporary place
sudo mv /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy \
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy_old
and then the next time I imported numpy the path was /Library/Python/2.7/site-packages/numpy/init.pyc and all was well.
This worked for me:
sudo pip install numpy --upgrade --ignore-installed
You are likely running the Mac default (/usr/bin/python) which has an older version of numpy installed in the system folders. The easiest way to get python working with opencv is to use brew to install both python and opencv into /usr/local and run the /usr/local/bin/python.
brew install python
brew tap homebrew/science
brew install opencv
I ran into the same issue tonight. It turned out to be a problem where I had multiple numpy packages installed. An older version was installed in /usr/lib/python2.7 and the correct version was installed in /usr/local/lib/python2.7.
Additionally, I had PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages. PYTHONPATH was finding the older version of numpy before the correct version, so when inside the Python interpreter, it would import the older version of numpy.
One thing which helped was opening a python session an executing the following code:
import numpy as np
print np.__version__
print np.__path__
That should tell you exactly which version Python is using, and where it's installed.
To fix the issue, I changed PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages. And I also setup a virtual Python environment using the Hitchiker's Guide to Python, specifically the section titled "Lower level: virtualenv" . I know I should have setup a virtual environment in the first place, but I was tired and being lazy. Oh well, lesson learned!
(Update)
Just in case the docs are moved again, here are the relevant bits on...
Creating a Python Virtual Environment
Install virtualenv via pip:
$ install virtualenv
Test the installation:
$ virtualenv --version
Optionally, et the environment variable VIRTUALENVWRAPPER_PYTHON to change the default version of python used by virtual environments, for example to use Python 3:
$ export VIRTUALENVWRAPPER_PYTHON=$(which python3)
Optionally, set the environment variable WORKON_HOME to change the default directory your Python virtual environments are created in, for example to use /opt/python_envs:
$ export WORKON_HOME=/opt/python_envs
Create a virtual environment for a project:
$ cd my_project_folder
$ virtualenv my_virtual_env_name
Activate the virtual environment, you just created. Assuming you also set WORKON_HOME=/opt/python_envs:
$ source $WORKON_HOME/my_virtual_env_name/bin/activate
Install whatever Python packages your project requires, using either of the following two methods.
Method 1 - Install using pip from command line:
$ pip install python_package_name1
$ pip install python_package_name2
Method 2 - Install using a requests.txt file:
$ echo "python_package_name1" >> requests.txt
$ echo "python_package_name2" >> requests.txt
$ pip install -r ./requests.txt
Optionally, but highly recommended, install virtualenvwrapper. It contains useful commands to make working with virtual Python environments easier:
$ pip install virtualenvwrapper
$ source /usr/local/bin/virtualenvwrapper.sh
On Windows, install virtualenvwrapper using:
$ pip install virtualenvwrapper-win
Basic usage of virtualenvwrapper
Create a new virtual environment:
$ mkvirtualenv my_virtual_env_name
List all virtual environments:
$ lsvirtualenv
Activate a virtual environment:
$ workon my_virtual_env_name
Delete a virtual environment (caution! this is irreversible!):
$ rmvirtualenv my_virtual_env_name
I hope this help!
To solve the problem do following:
First uninstall numpy
sudo pip uninstall numpy
Install numpy with --no-cache-dir option
sudo pip install --no-cache-dir numpy
And to specify any specific version e.g. 1.14.2
sudo pip install --no-cache-dir numpy==1.14.2
This command solved my problem.
pip3 install --upgrade numpy
upgrading numpy to rescue
numpy official document suggests users to do upgrade to solve this issue [1].
pip install numpy --upgrade
which version of numpy should I upgrade to
But you may upgrade to a version that is too new/old for your environment. I spent a long time trying to figure out which version of numpy is expected to be upgraded to when running into this error, and here is a list [2] of numpy versions with their corresponding C API versions, which may be useful for troubleshooting such an issue:
# 0x00000008 - 1.7.x
# 0x00000009 - 1.8.x
# 0x00000009 - 1.9.x
# 0x0000000a - 1.10.x
# 0x0000000a - 1.11.x
# 0x0000000a - 1.12.x
# 0x0000000b - 1.13.x
# 0x0000000c - 1.14.x
# 0x0000000c - 1.15.x
# 0x0000000d - 1.16.x
# 0x0000000d - 1.19.x
# 0x0000000e - 1.20.x
# 0x0000000e - 1.21.x
# 0x0000000f - 1.22.x
# 0x00000010 - 1.23.x
# 0x00000010 - 1.24.x
You can find the list here [2].
And the C API VERSION in numpy is tracked in three places according to [3]:
numpy/core/setup_common.py
numpy/core/code_generators/cversions.txt
numpy/core/include/numpy/numpyconfig.h
The error is reported by numpy's source code here [4]
references
[1] https://numpy.org/devdocs/user/troubleshooting-importerror.html#c-api-incompatibility
[2] https://github.com/numpy/numpy/blob/main/numpy/core/setup_common.py
[3] https://numpy.org/doc/stable/dev/releasing.html#check-the-c-api-version-number
[4] https://github.com/numpy/numpy/blob/bdec32181605c8179fd79624d14c1cf019de75af/numpy/core/code_generators/generate_numpy_api.py#L79
I got the same issue with quaternion module. When updating modules with conda, the numpy version is not up^dated to the last one. If forcing update with pip command pip install --upgrade numpy + install quaternion module by pip install --user numpy numpy-quaternion, the issue is fixed.
May be the issue is coming from the numpy version.
Here the execution result:
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> print np.__version__
1.14.3
>>>
(base) C:\Users\jc>pip install --user numpy numpy-quaternion
Requirement already satisfied: numpy in d:\programdata\anaconda2\lib\site-packages (1.14.3)
Collecting numpy-quaternion
Downloading https://files.pythonhosted.org/packages/3e/73/5720d1d0a95bc2d4af2f7326280172bd255db2e8e56f6fbe81933aa00006/numpy_quaternion-2018.5.10.13.50.12-cp27-cp27m-win_amd64.whl (49kB)
100% |################################| 51kB 581kB/s
Installing collected packages: numpy-quaternion
Successfully installed numpy-quaternion-2018.5.10.13.50.12
(base) C:\Users\jc>python
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import quaternion
>>>
I faced the same problem due to documentation inconsistencies.
This page says the examples in the docs work best with python 3.x: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_intro/py_intro.html#intro , whereas this installation page has links to python 2.7, and older versions of numpy and matplotlib: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html
My setup was as such: I already had Python 3.6 and 3.5 installed, but since OpenCv-python docs said it works best with 2.7.x, I also installed that version. After I installed numpy (in Python27 directory, without pip but with the default extractor, since pip is not part of the default python 2.7 installation like it is in 3.6), I ran in this RuntimeError: module compiled against API version a but this version of numpy is error. I tried many different versions of both numpy and opencv, but to no avail. Lastly, I simply deleted numpy from python27 (just delete the folder in site-packages as well as any other remaining numpy-named files), and installed the latest versions of numpy, matplotlib, and opencv in the Python3.6 version using pip no problem. Been running opencv ever since.
Hope this saves somebody some time.
When all else fail, check with the following script and disable unwanted python import path(s), or upgrade the package on those paths:
python ./test.py
test.py content:
import numpy as np
print(f'numpy version:{np.__version__}')
import sys
from pprint import pprint
pprint(sys.path)
import tensorflow as tf
print(f'TensorFlow version: {tf.__version__}')
For my case, it was the outdated conda version in ~/.local/lib/python3.8/site-packages that was messing things up :(
For those using anaconda Python:
conda update anaconda
You might want to check your matplotlib version.
Somehow I installed a dev version of matplotlib which caused the issue. A downgrade to stable release fixed it.
One can also can try python -v -c 'import YOUR_PACKAGE' 2>&1 | less to see where the issue occurred and if the lines above error can give you some hints.
You may also want to check your $PYTHONPATH. I had changed mine in ~/.bashrc in order to get another package to work.
To check your path:
echo $PYTHONPATH
To change your path (I use nano but you could edit another way)
nano ~/.bashrc
Look for the line with export PYTHONPATH ...
After making changes, don't forget to
source ~/.bashrc
I had the same error when trying to launch spyder. "RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa".
This error appeared once I modified the numpy version of my machine by mistake (I thought I was in a venv). If your are using spyder installed with conda, my advice is to only use conda to manage package.
This works for me:
conda install anaconda
(I had conda but no anaconda on my machine)
then:
conda update numpy
The below command worked for me :
conda install -c anaconda numpy
Although this question is very old, but I do believe there are still many facing similar problem as I did. I encountered the above reported error when I used Python3 in a Raspberry Pi micro-computer, which is running on Raspberry Pi OS.
This is perhaps due to missing some libraries when installed the Numpy module. I solved this problem following the suggestion in the Numpy website.
Solutions for Numpy Module Import Error
This Numpy troubleshooting website is really informative and provides cross-platform solutions for Windows, Anaconda, Raspberry, etc. Perhaps, someone can first follow the suggestion in this Numpy official website in order to solve the error.
I had same issue when I used import pyopencl and I did not want to upgrade numpy cause tensorflow requires old version of numpy so I solved it by simply:
python -m pip uninstall pyopencl && python -m pip install pyopencl
This way pyopencl was configured with existing numpy version and error solved.
I suffered with this problem for a long time, firstly you have to upgrade numby then try this code :
import numpy as np
print np.__version__
if gives you different version from the new one , uninstall the numpy(the new version) and use this
print numpy.__path__
go to that old numpy and delete the file , then install new version again
This works for me:
My pip is not work after upgrade, so the first thing I need to do is to fix it with
sudo gedit /usr/bin/pip
Change the line
from pip import main
to
from pip._internal import main
Then,
sudo pip install -U numpy

Importing opencv and getting numpy.core.multiarray failed to import

Trying to install OpenCV and running into an issue where attempting to import cv2 results in this output -
RuntimeError: module compiled against API version 9 but this version of numpy is 7
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
import cv2
ImportError: numpy.core.multiarray failed to import
I'm running on Windows 7 x64, Python v 2.7.9
Thanks!
The error is telling you that you have an out of date version of numpy. If you used pip to install things you can simply run pip install numpy -U, or download the appropriate version from their website.
In case
pip install -U numpy
doesn't work (even with sudo), you may want to make sure you're using the right version of numpy. I had the same "numpy.core.multiarray failed to import" issue, but it was because I had 1.6 installed for the version of Python I was using, even though I kept installing 1.8 and assumed it was installing in the right directory.
I found the bad numpy version by using the following command in my Mac terminal:
python -c "import numpy;print numpy.version;print numpy.file";
This command gave me the version and location of numpy that I was using (turned out it was 1.6.2). I went to this location and manually replaced it with the numpy folder for 1.8, which resolved my "numpy.core.multiarray failed to import" issue. Hopefully someone finds this useful!
I had a similar problem and I solved it by downgrading my numpy version.
What I did was:
pip install opencv-python
pip uninstall numpy
pip install numpy=1.18
This has worked for me using
Python 3.7
opencv-python 4.4.0.46
numpy 1.18.0
linux: sudo apt-get install python-numpy
if you are using ubuntu bionic beaver then try running: sudo apt-get install python-numpy
had the same issue, resolve by running the above command.
Hope it helps
In your environment you can try this command:
conda uninstall numpy
conda install -c conda-forge numpy
I use Python 3.7 # RPI 4.
For opencv to install properly I had to install the listed libraries below.
(Not every package was actually installed, after request)
Regarding Numpy, I think one should stick to the latest version.
For me what worked is to uninstall the existing version 1.16.2 and stick with the current stable 1.21.2.
Stackoverflow topic at missing libraries here: ImportError: libcblas.so.3: cannot open shared object file: No such file or directory.

Categories

Resources