ImportError: cannot import name ‘packaging‘ from ‘pkg_resources‘ 的参考解决方法

写在前面

自己的测试环境:
Ubuntu20.04
ROS-Noetic

一、问题描述

自己在通过 pip install 安装module时 (使用的是 pip install mmcv)遇到如下问题:

ImportError: cannot import name 'packaging' from 'pkg_resources' 

出现该问题时的终端的全部输出如下:

Using cached mmcv-2.2.0.tar.gz (479 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [10 lines of output]
      /tmp/pip-install-qm5a01q8/mmcv_417ac91e439f41a1b12309217221d376/setup.py:5: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
        from pkg_resources import DistributionNotFound, get_distribution, parse_version
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-qm5a01q8/mmcv_417ac91e439f41a1b12309217221d376/setup.py", line 19, in <module>
          from torch.utils.cpp_extension import BuildExtension
        File "/home/wong/ProgramFiles/anaconda3/envs/pytorch_env/lib/python3.8/site-packages/torch/utils/cpp_extension.py", line 28, in <module>
          from pkg_resources import packaging  # type: ignore[attr-defined]
      ImportError: cannot import name 'packaging' from 'pkg_resources' (/home/wong/ProgramFiles/anaconda3/envs/pytorch_env/lib/python3.8/site-packages/pkg_resources/__init__.py)
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

报错截图如下:
在这里插入图片描述

二、解决方法

上述的报错信息表明:在安装 mmcv 时出现了 pkg_resources 模块的问题。具体来说,似乎是 packaging 模块无法从 pkg_resources 中导入。
解决方法如下:

  1. 安装packaging 模块:

    pip install packaging  
    
  2. 更新 setuptoolspip:

    pip install --upgrade setuptools pip
    

然后再次运行自己的安装程序,即可解决问题。(自己是重新运行 pip install mmcv, 就能安装成功了。)

参考链接

[1] sider. 2024-11-10.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值