import jwt ImportError: No module named jwt - python

I have been trying to run this project
https://github.com/udacity/FSND-Deploy-Flask-App-to-Kubernetes-Using-EKS
I installed all the dependencies.
I still did not make any adjustments. I need to run it first
but I get this error when I type the command
python main.py
this is the error i get:
Traceback (most recent call last):
File "main.py", line 8, in <module>
import jwt
ImportError: No module named jwt
I worked with similar errors before and managed to solve them but not with this one I could not figure out the source of the problem

Check if PyJWTY is in the requirements file or if is installed in you system, using: pip3 install PyJWT
You could also face this error if you have running on your machine two versions of python. So the correct command will be python3 main.py

I have hit the same issue with pyjwt 2.1.0 which was clearly installed in my venv as well as globally. What helped was to downgrade it to version 1.7.1
pip install "PyJWT==1.7.1"
run the app and then to reinstall newest version 2.1.0
pip install "PyJWT==2.1.0"
And the issue disappeared.

This project has requirements that need to be installed for it to work. These can be installed via pip, pip install -r requirements.txt (I've linked to the requirements file in the project), which you can read more about here.

What worked for me was using import jwt instead of import PyJWT. I am using version PyJWT-2.3.0.
jwt image on vscode
As you can see no errors in the above screenshot. The app runs without import errors.
Image of terminal

Faced the same issue. Am using a guest VM running ubuntu 16.04.
I have multiple versions of python installed - both 3.5 and 3.7.
After repeated tries with and without using virtualenv what worked finally is:
Create a fresh virtual environment using :
priya:~$ virtualenv -p /usr/bin/python3.7 fenv
Activate the virutal environment :
priya:~$ source ./fenv/bin/activate
Note : You can find the path for python3.7 by using whereis python:
priya:~$ whereis python
python: /usr/bin/python /usr/bin/python3.5m /usr/bin/python3.5 /usr/bin/python3.7 /usr/bin/python3.5m-config /usr/bin/python3.5-config /usr/bin/python2.7 /usr/bin/python3.7m /usr/bin/python2.7-config /usr/lib/python3.5 /usr/lib/python3.7 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python3.7 /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python3.7 /usr/local/lib/python2.7 /usr/include/python3.5m /usr/include/python3.5 /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
Referenced link is : https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv#:~:text=By%20default%2C%20that%20will%20be,%2Flocal%2Fbin%2Fpython3
For your project - FSWD Nanodegree -
After you have activated your virtualenv, run pip install -r requirements.txt
You can test by :
(fenv) priya:FSND-Deploy-Flask-App-to-Kubernetes-Using-EKS :~$ python
Python 3.7.9 (default, Aug 18 2020, 06:24:24)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
import jwt
exit()

pip3 install flask_jwt_ex.. I was doing this without sudo. And then I was working on the program as sudo.

You have to have only PyJWT installed and not JWT. Make sure you uninstall JWT (pip uninstall JWT) and install PyJWT (pip install PyJWT)

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

Getting "ModuleNotFoundError: No module named 'Pillow'" in VS Code

I installed the Pillow module within a virtual env as such:
(venv)$python -m pip install --upgrade Pillow
Collecting Pillow
Downloading Pillow-7.2.0-cp38-cp38-manylinux1_x86_64.whl (2.2 MB)
|████████████████████████████████| 2.2 MB 2.4 MB/s
Installing collected packages: Pillow
Successfully installed Pillow-7.2.0
The VS Code interpreter is the same as the terminal version:
$python --version
Python 3.8.0
$ which python
/mnt/d/github/python_dev/venv/bin/python
But when I run from Pillow import Image I still got ModuleNotFoundError:
$ ../venv/bin/python images.py
Traceback (most recent call last):
File "images.py", line 1, in <module>
from PIL import Image
ModuleNotFoundError: No module named 'Pillow'
I knew there are many posts regarding this topic but I'm still stuck here for almost half a day. What's the best way to fix it?
There is no command like:-
from Pillow import Image
Python likes to import Pillow module as PIL.
So, Try to use:-
from PIL import Image
It Worked for me. :)
There is no magic. Every time this happens you can debug it by yourself:
(venv) etoneja#ois ~/Projects/***/venv/lib (master)$ grep -r -w "class Image"
python3.8/site-packages/PIL/Image.py:class Image:
and
(venv) etoneja#ois ~/Projects/***/venv/lib (master)$ find . -iname "Pil"
./python3.8/site-packages/PIL
Also check this output
(venv) etoneja#ois ~/Projects/***/venv/lib (master)$ pip list
Package Version
------------- --------
Pillow 7.1.2
If there is no Pillow, so somethings wrong with your installation.
Check that you activate the current venv.
So the answer:
from PIL import Image
You seem to have 2 problems:
From your original problem, there is no from Pillow import Image.
As described in Pillow's docs, it is supposed to be a fork of the old PIL module
As shown in Pillow's Tutorial docs, the correct import is same as PIL's:
>>> from PIL import Image
>>> im = Image.open("hopper.ppm")
From the comments and your responses, either Pillow was not installed correctly in your virtual environment, or your virtual environment is not activated properly.
Seeing your question is for VS Code, here is a step-by-step way to solve problem #2.
In VS Code, open a Terminal and create your venv
cerberus#test$ python3.8 -V
Python 3.8.5
cerberus#test$ python3.8 -m venv ~/.venvs/venv
Here, I'm creating the venv in ~/.venvs with name venv (same as yours and using Python 3.8 (you can store your venv directory wherever you like)
Test that it can be activated correctly and is using correct Python version
cerberus#test$ source ~/.venvs/venv/bin/activate
(venv) cerberus#test$ python -V
Python 3.8.5
You'll know it's activated when your prompt is prefixed with (venv-name)
Reload VS Code
When creating new virtual envs, I find it's best to reload VS Code before it can "see" it
You could also create the venv outside VS Code, then just open VS Code after
After reload, open the command palette and do Python: Select Interpreter
Find your created venv from the list
Close and reopen the Terminal
VS Code should auto-activate your chosen venv when it reopens a Terminal
You could also manually activate the venv
cerberus#test$ source ~/.venvs/venv/bin/activate
(venv) cerberus#test$
Once you've got VS Code to use and activate the correct venv, install Pillow
(venv) cerberus#test$ python -V
Python 3.8.5
(venv) cerberus#test$ python -m pip install Pillow
Collecting Pillow
Using cached Pillow-7.2.0-cp38-cp38-macosx_10_10_x86_64.whl (2.2 MB)
Installing collected packages: Pillow
Successfully installed Pillow-7.2.0
Test the installation
(venv) cerberus#test$ python
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>>
>>> import PIL
>>> PIL.__path__
['/Users/cerberus/.venvs/venv/lib/python3.8/site-packages/PIL']
Now your scripts should work
As I mentioned in the comments, once you've got your venv activated, there is no need to specify the full path to the python interpreter. Just python should be fine.

Problems with installing Numpy in Python 2.7.5 [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

How to install gi module for anaconda python3?

python3 is my local Anaconda version of python, while python3.4 is the system one. I can import gi module with python3.4 (probably because i installed it with sudo apt-get install python3-gi) but python3 doesn't see it:
$ python3 -c 'import gi'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'gi'
$ python3.4 -c 'import gi'
$ python3 --version
Python 3.5.1 :: Anaconda 4.0.0 (64-bit)
$ python3.4 --version
Python 3.4.3
$ which python3
/home/kossak/anaconda3/bin/python3
$ which python3.4
/usr/bin/python3.4
$
How should i install gi for Anaconda python? Or maybe i can somehow import sysem-wide modules?
My os:
System: Kernel: 3.19.0-32-generic x86_64 (64 bit gcc: 4.8.2) Desktop: Cinnamon 2.8.8 (Gtk 2.24.23) dm: mdm
Distro: Linux Mint 17.3 Rosa
If you're using conda virtualenv for python-3, you can use
$ conda install -c conda-forge pygobject
in your virtualenv
You can read more about this on:
https://anaconda.org/conda-forge/pygobject
This is how you do it: (example for Linux Mint and python3)
First install gi module using your distro package manager. For Linux Mint it would be:
sudo apt-get install python3-gi
Then run your distro python to check where the module is located:
$ /usr/bin/python3
Python 3.5.2 (default, Sep 10 2016, 08:21:44)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> print(gi)
<module 'gi' from '/usr/lib/python3/dist-packages/gi/__init__.py'>
So in my case the module gi was installed to /usr/lib/python3/dist-packages/gi. Assuming you have your anaconda installed in /home/kossak/anaconda3, create a link to gi module in the proper folder:
ln -s /usr/lib/python3/dist-packages/gi /home/kossak/anaconda3/lib/python3.5/site-packages/
If you have conda virtual environment and want gi module to be available there, the path should be a bit different. Assuming the virtual env is called TEST:
ln -s /usr/lib/python3/dist-packages/gi /home/kossak/anaconda3/envs/TEST/lib/python3.5/site-packages/
and it works:
$ python3
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>>
If you want to perform a proper install (without soft linking) inside a Linux anaconda environment. Keep in mind that the errors may vary if you have not installed gcc previously (I assumed it was installed by default when I posted, however not everyone does so). Install it if you don't know very well what you're doing to avoid missunderstandings
Create or open your conda environment.
Attemp to install pygobject (don't worry, it will most likely throw an error):
pip install pygobject
In linux, it will promp the usual installation progress followed by an error:
(...) Please, try executing the following in your system:
sudo apt install libgirepository1.0-dev
Depending on your operation sistem or installed dependencies, the command name or package name may vary. Just follow the instructions and allow the system to install your packets. This step doesn't change anything, is just to give you the precise info of the package you need on your system. If you run this on Windows, it will ask you to install a specific version of Visual Studio. If you are in Windows, download the required Visual Studio from MS website, install it, reboot your computer and go to step 5 (in my case I never needed step 4 in windows, however, I'm not a MS expert.
Close your conda environment
conda deactivate
Next you need to install pygobject from conda-forge repository in your conda environment. You can add the repo to your favourite conda package manager or simply run the following command as root (it is important to be root). I did it outside the project, but you may do it inside:
conda install -c conda-forge pygobject
In my case conda was not in the path. I had is installed in:
/opt/anaconda3/bin/
You can run the following command from your normal user to find out where conda is:
which conda
Open the conda environment
source activate <your env name>
or the corresponding anaconda activate syntax (I never use it so I cant remember precisely)
Repeat the first step and now the installation wont fail:
pip install pygobject
OR if you specifically want to install gi you can run:
pip install pgi
the correct package is "pgi" NOT "gi"
As gi is a dependency of pygobject, everything will get properly installed. You can check it by running
python
>>> import gi
You may find the following usefull for Windows, although you may need to work it out a bit:
GStreammer python bindings on Windows
for me
conda install -c pkgw/label/superseded gtk3
worked

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

Categories

Resources