Pycocotools 报错 No module named pycocotools.coco 以 numpy.ndarray size changed, may indicate binary

1、numpy.ndarray size changed, may indicate binary incompatibility. 
Expected 88 from C header, got 80 from PyObject

pycocotools和numpy版本不兼容导致
pip install --upgrade numpy 

pip install --upgrade numpy==1.20.2
pip install --upgrade pycocotools==2.0.2

pip install --upgrade --force-reinstall pycocotools
pip install --upgrade --force-reinstall numpy

2、No module named 'pycocotools'

参考链接 https://betterdatascience.com/modulenotfounderror-no-module-named-pycocotools/
除了直接pip或者conda安装之外的解决办法是本地直接从git源链接安装到本地:

Solution 3 - Use Pycocotools source code

Installing Pycocotools from the source implies you have Cython and C compiler already configured. If that’s the case, proceed by cloning the COCO API GitHub repository, and navigating to the correct directory for Python API:

git clone https://github.com/cocodataset/cocoapi/cd cocoapi/PythonAPI

Once done, compile the COCO API:

make

And finally, install the Pycocotools library:

python setup.py install

This should take care of the ModuleNotFoundError, but only if you have Cython and C compiler configured. If not, take a look at the next potential solution.

Solution 4 - Use Pycocotools precompiled libraries

If you can’t install Cython and C compiler, you can still use the precompiled libraries. You’ll just have to install them using wheel files. The installation command will differ depending on your operating system.

Wheel files are compiled packages that can be used by pip.

To install Pycocotools on Linux via the wheel file, run:

pip install git+https://github.com/waleedka/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI

And if you’re on Windows, run:

pip install git+https://github.com/philferriere/cocoapi.git#egg=pycocotools^&subdirectory=PythonAPI

This should fix ModuleNotFoundError: no module named pycocotools. If not, there are still some solutions you can try.


3、我这里最后面临的问题是 No module named 'pycocotools.coco':


每次都可以成功导入pycocotools,但是来自错误的位置 '/home/ma-user/modelarts-dev/common-algo-toolkit'
因为这个位置下虽然有一个pycocotools的文件夹,但其中pycocotools根本是不能使用的,里面并没有包含包的内容。
而且一旦import pycocotools之后,我使用notebook还是会默认去找一开始错误的路径
所以要删除或者修改目录导入顺序后  【重启 kernel才生效】
【还有解决办法就是先升级np,确保pycocotools可以使用了,再import pycocotools就直接可以用,因为系统导入包的顺序默认就是/home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python3.7/site-packages/在前的,当在这里找不到才会去找系统后面的路径】

下面附上系统路径查找、插入、删除,以及查看使用的包的来源的代码:

print(sys.path) # 查看系统路径和顺序

# ['/home/ma-user/work/sam/notebooks',
# '/usr/local/seccomponent/lib',
# '/home/ma-user/infer/model/1',
# '/home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python37.zip',
# '/home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python3.7',
# '/home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python3.7/lib-dynload',
# '',
# '/home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python3.7/site-packages',
# '/home/ma-user/modelarts-dev/modelarts-sdk',
# '/home/ma-user/modelarts-dev/common-algo-toolkit',
# '/home/ma-user/modelarts-dev/ma-cli',
# '/home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python3.7/site-packages/IPython/extensions',
# '/home/ma-user/.ipython']


import numpy
print(numpy.__file__)  #看numpy来自哪个目录
sys.path.insert(0, '/home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python3.7/site-packages')  # 插入系统路径到最优先的顺序
sys.path.insert(0, '/home/ma-user/work/cocoapi/PythonAPI/')
# 插入系统路径到最优先的顺序
sys.path.remove('/home/ma-user/modelarts-dev/common-algo-toolkit')  # 直接将common-algo-toolkit从系统路径中去除掉
import pycocotools
print(pycocotools.__file__)       #本来这里一直来自错误的包目录
from pycocotools.coco import COCO #导致这里报错,无法使用

# /home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python3.7/site-packages/numpy/__init__.py
# /home/ma-user/anaconda3/envs/PyTorch-1.8.2/lib/python3.7/site-packages/pycocotools/__init__.py

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值