AttributeError: module ‘pkg_resources‘ has no attribute ‘declare_namespace‘

博客内容讲述了作者在使用anaconda3和conda4.6.7版本时遇到nb_conda安装失败导致环境损坏的问题。经过检查发现setuptools版本58.0.0可能过高,不兼容现有环境。通过降级setuptools到36.0.2成功解决了问题。
部署运行你感兴趣的模型镜像

记录一个问题,anaconda3 conda 4.6.7版本安装nb_conda失败把原有环境弄坏了,出现了如标题所示错误。

猜测是依赖有点问题,先通过pipdeptree检查,发现所有依赖都满足。但是setuptools这个东西conda update conda过程中我手动更新过,虽然conda update conda更新失败了,但setuptools版本已经更新到58.0.0了,

- setuptools [required: >=36, installed: 58.0.0]

要求最高才36,怀疑是版本太新,老的没兼容。然后降级版本试了一把

pip uninstall setuptools

pip install setuptools==36.0.2

问题解决。

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

qk@qk-Legion-Y7000P-IRH8:~$ rosdep /usr/bin/rosdep:6: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html from pkg_resources import load_entry_point Usage: rosdep [options] <command> <args> Commands: rosdep check <stacks-and-packages>... check if the dependencies of package(s) have been met. rosdep install <stacks-and-packages>... download and install the dependencies of a given package or packages. rosdep db generate the dependency database and print it to the console. rosdep init initialize rosdep sources in /etc/ros/rosdep. May require sudo. rosdep keys <stacks-and-packages>... list the rosdep keys that the packages depend on. rosdep resolve <rosdeps> resolve <rosdeps> to system dependencies rosdep search <searchstrings>... Search for a key in the rosdep database. Searches rosdep keys and system or ROS package names. The search is case-insensitive and all search strings have to match. Supports fuzzy-search if the Python module regex is installed. Uses the os name from the current system to filter the package results. You can override the os name with the --os option. Example: rosdep search pcl dev Output: Closest keys: libpcl-all-dev rosdep update update the local rosdep database based on the rosdep sources. rosdep what-needs <rosdeps>... print a list of packages that declare a rosdep on (at least one of) <rosdeps> rosdep where-defined <rosdeps>... print a list of yaml files that declare a rosdep on (at least one of) <rosdeps> rosdep fix-permissions Recursively change the permissions of the user's ros home directory. May require sudo. Can be useful to fix permissions after calling "rosdep update" with sudo accidentally. rosdep: error: Please enter a command qk@qk-Legion-Y7000P-IRH8:~$
09-10
<think>我们正在处理一个Python错误:AttributeError: module 'pkg_resources' has no attribute 'resource_filename' 根据引用[3]中提到了一个与pkg_resources相关的错误,但它是关于run_script和ConfigParser的,与这里的错误不同。 引用[4]提到了异常处理和警告,但与我们当前的错误无关。 引用[5]是关于源码包安装的,也不直接相关。 但是,我们注意到错误信息是:模块'pkg_resources'没有属性'resource_filename'。这通常意味着pkg_resources模块没有正确加载,或者版本不兼容。 可能的原因和解决方案: 1. 安装问题:可能是setuptools包没有正确安装或损坏。pkg_resources是setuptools的一部分。 解决方案:尝试重新安装setuptools。 使用pip: pip install --upgrade --force-reinstall setuptools 2. 环境问题:可能有多个Python环境,或者环境中的包冲突。 检查当前环境:确保你使用的Python解释器是期望的那个,并且setuptools已安装在该环境中。 可以通过以下命令检查setuptools版本: pip show setuptools 3. 导入问题:可能是脚本中导入了错误的模块。检查代码中是否有名为pkg_resources.py的文件,可能会与标准库冲突。 确保没有自定义模块与标准库模块同名。 4. 版本问题:某些旧版本的setuptools可能没有resource_filename属性(尽管这个属性已经存在很长时间)。升级setuptools通常可以解决。 5. 如果以上方法无效,可以尝试重新安装pip,因为pip依赖于setuptools,重新安装pip可能会修复setuptools。 python -m pip uninstall pip python -m ensurepip 6. 在虚拟环境中,有时需要重新创建虚拟环境。 由于引用中没有直接给出相同错误的解决方案,我们根据常见解决方法提供上述步骤。 注意:在重新安装系统包时,确保有足够的权限(可能需要使用sudo,但在Windows上不需要)。 根据用户提供的引用[5]中关于源码包安装的信息,如果重新安装无效,也可以尝试从源码安装setuptools,但通常不需要。 因此,我们推荐首先尝试重新安装或升级setuptools。 另外,引用[1]和[2]是Go和C++相关的错误,与当前问题无关,可以忽略。 步骤总结: 1. 升级setuptools: pip install --upgrade setuptools 2. 如果问题依旧,尝试强制重新安装: pip install --force-reinstall setuptools 3. 检查是否在虚拟环境中,如果是,尝试重新创建虚拟环境。 4. 检查是否有同名文件冲突。 如果以上步骤都不能解决,请提供更多上下文信息。 相关问题: 1. 如何检查Python环境中已安装的包? 2. 如何创建一个新的Python虚拟环境? 3. setuptools在Python中起什么作用? 注意:在运行安装命令时,如果遇到权限问题,可以考虑在用户级别安装(使用--user选项)或者使用虚拟环境。 </think> 要解决`AttributeError: module 'pkg_resources' has no attribute 'resource_filename'`错误,请按以下步骤操作: ### 1. **升级`setuptools`和`pip`** 这是最常见解决方案,因为`pkg_resources`是`setuptools`的子模块: ```bash pip install --upgrade setuptools pip ``` 升级后重启Python环境[^5]。 ### 2. **检查包冲突** - 确认环境中无同名文件冲突: ```bash # 检查是否误创建了pkg_resources.py文件 find . -name "pkg_resources.py" ``` - 若有冲突文件,重命名或删除它。 ### 3. **重装问题包** 如果特定包引发错误(如`mininet`在引用[3]): ```bash pip uninstall 包名 && pip install --no-cache-dir 包名 ``` ### 4. **验证Python环境** - 检查Python路径是否异常: ```python import sys print(sys.path) ``` - 确保未混用多个Python版本(如系统PythonAnaconda)。 ### 5. **重建虚拟环境(终极方案)** 若以上无效,重建隔离环境: ```bash # 创建新环境 python -m venv new_env source new_env/bin/activate # Linux/Mac new_env\Scripts\activate # Windows # 重新安装依赖 pip install -r requirements.txt ``` ### 原因分析 该错误通常由以下原因引起: 1. `setuptools`版本过旧或损坏(占90%案例) 2. 存在与标准库同名的自定义文件(如自建的`pkg_resources.py`) 3. 环境路径配置异常导致模块加载错误 4. 包安装不完整(如网络中断导致部分安装) > 根据引用[5],源码安装时需确保编译环境完整:`apt-get build-dep`可自动安装编译依赖[^5]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值