Python2.7 安装mock出现AttributeError: 'module' object has no attribute的解决方法

原环境:
1.pip --verson : 1.0 from /usr/lib/python2.7/dist-packages (python 2.7)
2.uname -a :Linux pc 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
3.root权限
之后:
2.pip -V:pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)
问题:
安装mock出错
 pip install -U mock
Downloading/unpacking mock
  Downloading mock-2.0.0.tar.gz (73Kb): 73Kb downloaded
  Running setup.py egg_info for package mock

    Installed /tmp/build/mock/pbr-1.10.0-py2.7.egg
    ERROR:root:Error parsing
    Traceback (most recent call last):
      File "/tmp/build/mock/pbr-1.10.0-py2.7.egg/pbr/core.py", line 111, in pbr
        attrs = util.cfg_to_args(path, dist.script_args)
      File "/tmp/build/mock/pbr-1.10.0-py2.7.egg/pbr/util.py", line 248, in cfg_to_args
        kwargs = setup_cfg_to_setup_kwargs(config, script_args)
      File "/tmp/build/mock/pbr-1.10.0-py2.7.egg/pbr/util.py", line 431, in setup_cfg_to_setup_kwargs
        if pkg_resources.evaluate_marker('(%s)' % env_marker):
    AttributeError: 'module' object has no attribute 'evaluate_marker'
    error in setup command: Error parsing /tmp/build/mock/setup.cfg: AttributeError: 'module' object has no attribute 'evaluate_marker'
    Complete output from command python setup.py egg_info:

Installed /tmp/build/mock/pbr-1.10.0-py2.7.egg

ERROR:root:Error parsing

Traceback (most recent call last):

  File "/tmp/build/mock/pbr-1.10.0-py2.7.egg/pbr/core.py", line 111, in pbr

    attrs = util.cfg_to_args(path, dist.script_args)

  File "/tmp/build/mock/pbr-1.10.0-py2.7.egg/pbr/util.py", line 248, in cfg_to_args

    kwargs = setup_cfg_to_setup_kwargs(config, script_args)

  File "/tmp/build/mock/pbr-1.10.0-py2.7.egg/pbr/util.py", line 431, in setup_cfg_to_setup_kwargs

    if pkg_resources.evaluate_marker('(%s)' % env_marker):

AttributeError: 'module' object has no attribute 'evaluate_marker'

error in setup command: Error parsing /tmp/build/mock/setup.cfg: AttributeError: 'module' object has no attribute 'evaluate_marker'

----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /root/.pip/pip.log

通过 更新pip之后再安装即可[1]
pip install --upgrade pip

之后出现其他问题:

>>> from mock import MagicMock
Traceback (most recent call last):
  File "", line 1, in
  File "/usr/local/lib/python2.7/dist-packages/mock/__init__.py", line 2, in
  import mock.mock as _mock
  File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 71, in
  _v = VersionInfo('mock').semantic_version()
  File "/usr/local/lib/python2.7/dist-packages/pbr/version.py", line 460, in semantic_version
  self._semantic = self._get_version_from_pkg_resources()
  File "/usr/local/lib/python2.7/dist-packages/pbr/version.py", line 447, in _get_version_from_pkg_resources
  result_string = packaging.get_version(self.package)
  File "/usr/local/lib/python2.7/dist-packages/pbr/packaging.py", line 725, in get_version
  raise Exception("Versioning for this project requires either an sdist"
Exception: Versioning for this project requires either an sdist tarball, or access to an upstream git repository. Are you sure that git is installed?

有人说   [2]pip install --upgrade distribute 可以解决,结果这一步也不能顺利。

Collecting distribute
  Using cached distribute-0.7.3.zip
  Complete output from command python setup.py egg_info:
  running egg_info
  creating pip-egg-info/distribute.egg-info
  writing requirements to pip-egg-info/distribute.egg-info/requires.txt
  writing pip-egg-info/distribute.egg-info/PKG-INFO
  writing top-level names to pip-egg-info/distribute.egg-info/top_level.txt
  writing dependency_links to pip-egg-info/distribute.egg-info/dependency_links.txt
  Traceback (most recent call last):
  File "", line 1, in
  File "/tmp/pip-build-gVJGPE/distribute/setup.py", line 58, in
  setuptools.setup(**setup_params)
  File "/usr/lib/python2.7/distutils/core.py", line 152, in setup
  dist.run_commands()
  File "/usr/lib/python2.7/distutils/dist.py", line 953, in run_commands
  self.run_command(cmd)
  File "/usr/lib/python2.7/distutils/dist.py", line 972, in run_command
  cmd_obj.run()
  File "setuptools/command/egg_info.py", line 177, in run
  writer = ep.load(installer=installer)
  File "pkg_resources.py", line 2241, in load
  if require: self.require(env, installer)
  File "pkg_resources.py", line 2254, in require
  working_set.resolve(self.dist.requires(self.extras),env,installer)))
  File "pkg_resources.py", line 2471, in requires
  dm = self._dep_map
  File "pkg_resources.py", line 2682, in _dep_map
  self.__dep_map = self._compute_dependencies()
  File "pkg_resources.py", line 2699, in _compute_dependencies
  from _markerlib import compile as compile_marker
  ImportError: No module named _markerlib

    

之后尝试升级 pip install -U setuptools,然而还是报同样的错误;
使用 easy_install -U distribute [3]  ,终于成功安装mock。


参考资料:
1.  https://github.com/wal-e/wal-e/issues/241
2.  https://github.com/tensorflow/tensorflow/issues/6411
3.  http://www.jianshu.com/p/2619b2289acc 


你可能感兴趣的:(Python)