Python下Pip安装包下的AttributeError: 'Requirement' object has no attribute 'project_name'

引言: 在基于pip安装第三方依赖库的过程中,会碰到AttributeError: 'Requirement' object has no attribute 'project_name'的错误信息,这个问题该如何解决呢?


1.  问题的提出

    在某开源系统的调试中,需要安装一批依赖包,其使用一个requirements.txt来描述其所有的依赖包,具体如下:  

...................
HTTPReplay==0.1.5
idna==2.0
ipaddress==1.0.14
itsdangerous==0.24
Jinja2==2.8
jsbeautifier==1.5.10
Mako==1.0.1
MarkupSafe==0.23
ndg-httpsclient==0.4.0
pyasn1==0.1.8
pycparser==2.14
.................................
  基于pip进行安装:
sudo pip install -r requirements.txt
  在安装过程中,出现来如下错误信息:
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 317, in run
    requirement_set.prepare_files(finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 360, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 448, in _prepare_file
    req_to_install, finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 387, in _check_skip_installed
    req_to_install.check_if_exists()
  File "/usr/lib/python2.7/dist-packages/pip/req/req_install.py", line 1011, in check_if_exists
    self.req.project_name
AttributeError: 'Requirement' object has no attribute 'project_name'

2.问题的分析

   第一步首先单独进行安装特定包的检查:

   >> sudo pip install six==1.9.0

 

polo@polo-notebook:/opt/cuckoo$ sudo pip install six==1.9.0
The directory '/home/polo/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/polo/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 317, in run
    requirement_set.prepare_files(finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 360, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 448, in _prepare_file
    req_to_install, finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 387, in _check_skip_installed
    req_to_install.check_if_exists()
  File "/usr/lib/python2.7/dist-packages/pip/req/req_install.py", line 1011, in check_if_exists
    self.req.project_name
AttributeError: 'Requirement' object has no attribute 'project_name'

问题依然存在,看来问题就出在这个安装包这里。是否是由于版本太老呢?接下来继续尝试。

 >>  sudo pip install six

polo@polo-notebook:/opt/cuckoo$ sudo pip install six
The directory '/home/polo/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/polo/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
这里反馈的信息是six包已经被安装过了,难道是由于系统已经安装的six版本不兼容问题?

  让我们来查看一下six当下的版本:

polo@polo-notebook:/opt/cuckoo$ pip show six
---
Metadata-Version: 2.0
Name: six
Version: 1.10.0
Summary: Python 2 and 3 compatibility utilities
Home-page: http://pypi.python.org/pypi/six/
Author: Benjamin Peterson
Author-email: benjamin@python.org
Installer: pip
License: MIT
Location: /usr/local/lib/python2.7/dist-packages
Requires: 
Classifiers:
  Programming Language :: Python :: 2
  Programming Language :: Python :: 3
  Intended Audience :: Developers
  License :: OSI Approved :: MIT License
  Topic :: Software Development :: Libraries
  Topic :: Utilities
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
原来的确如此,要安装的版本低于目前系统的版本,故爆出来了如此诡异的错误信息。

3. 总结

在pip管理下的包在升级和安装过程中,project_name的问题时常发生,大部分的情况下都是版本的不兼容问题造成的,尤其在第三方项目强调特定版本的依赖包情况下,也可以通过卸载当下版本,安装需要的版本;但是这样也给我们的系统带来来若干隐患问题,低版本的安装包与当下系统中其他版本的兼容问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值