google-cloud-python: AttributeError: '_NamespacePath' object has no attribute 'sort'

  1. Ubuntu 16.04.1 LTS
  2. Python 3.5.2 :: Anaconda 4.2.0 (64-bit)

I was trying install with pip install --upgrade google-cloud but I get the following AttributeError: ‘_NamespacePath’ object has no attribute ‘sort’

Below is the stack trace:

Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/bin/pip", line 7, in <module>
    from pip import main
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/utils/__init__.py", line 27, in <module>
    from pip._vendor import pkg_resources
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in <module>
    @_call_aside
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
    f(*args, **kwargs)
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
    dist.activate(replace=False)
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
    declare_namespace(pkg)
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
    _rebuild_mod_path(path, packageName, module)
  File "/home/ubuntu/anaconda3/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2121, in _rebuild_mod_path
    orig_path.sort(key=position_in_sys_path)
AttributeError: '_NamespacePath' object has no attribute 'sort'

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 53 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Is there a workaround for this? I can’t install google-cloud

Edit: pip install --upgrade pip and pip install --upgrade setuptools did the trick. Maybe this can be closed

Workaround / Fix

pip install --upgrade pip setuptools

With setuptools==34.3.0 the problem is no longer there.

Neither upgrading pip nor upgrading setuptools helped 😦.

@MarSoft bit tricky in a chicken-egg kind of way:

# pip install --upgrade pip setuptools
Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 7, in <module>
    from pip import main
  File "/usr/local/lib/python3.5/dist-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/usr/local/lib/python3.5/dist-packages/pip/utils/__init__.py", line 27, in <module>
    from pip._vendor import pkg_resources
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in <module>
    @_call_aside
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
    dist.activate(replace=False)
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
    declare_namespace(pkg)
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
    _rebuild_mod_path(path, packageName, module)
  File "/usr/local/lib/python3.5/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2121, in _rebuild_mod_path
    orig_path.sort(key=position_in_sys_path)
AttributeError: '_NamespacePath' object has no attribute 'sort'

Notably, you just need to update your pip & setuptools.

On Wed, Feb 8, 2017, 8:55 PM Danny Hermes notifications@github.com wrote:

I’m pre-emptively closing but happy to re-open if more information arises.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2990#issuecomment-278547215, or mute the thread https://github.com/notifications/unsubscribe-auth/AAPUc4exjr5-VpkrvSjWyZD0XQGw51ioks5rapwqgaJpZM4L7sYe .

I fixed the problem by commenting (or removing) the line: orig_path.sort(key=position_in_sys_path) [I think is line number 2066] both in “/usr/lib/python3/dist-packages/pkg_resources/__init__.py” and “~/.local/lib/python3.5/site-packages/pip/_vendor/__init.py__”.

This problem has begun when I updated pip using “sudo pip --upgrade python-pip”. I’m on Ubuntu 16.04 LTS.

I had the same problem using poetry.

Running

poetry run pip install --upgrade pip setuptools

instead of

pip install --upgrade pip setuptools

fixed the issue.

For all those coming by, I have done a “deep” dive today to understand this issue and found out that pip==9.0.1 (current latest) still comes with a broken version of setuptools.

This '_NamespacePath' object has no attribute 'sort' error is unavoidable if you use python setup.py install on one of our packages (they are all namespace packages).

If you have a fresh environment and only use pip install to install our packages, you will not have this issue.

Sorry for the inconvenience.

References:

Updating pip and the setup tools did the work for me. python -m pip install --upgrade pip python -m pip install --upgrade setuptools

UPDATE, mines working with pip from bleeding edge, no steuptools upgrade (thanks @bkleef ):

python3.6 -m venv .venv
source .venv/bin/activate
pip install -I https://github.com/pypa/pip/archive/master.zip#egg=pip
pip install --upgrade google-cloud-datastore
pip freeze

easy_install pip followed by pip install --upgrade pip && pip install --upgrade setuptools worked for me

I found a workaround: $ pip uninstall setuptools $ pip install setuptools==39.1.0

I ran into this issue today when upgrading pip from 9.0.1 to 9.0.2 . I don’t believe it is a setuptools issue as I upgraded to 39.0.0 . Eventually nothing would install. I went into pip and bypassed line 2121? (the keysorter line) and it works, although I have no idea if this will be stable.

Disappointing that this would be an issue 2 years out for a package as important as pip.

@dhermes I ended up here after going round the houses, though even with a “fresh” environment, and using pip for all package installtion there is still a problem, see:

python3.6 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip setuptools
pip install --upgrade google-cloud-datastore
pip freeze
Requirement already up-to-date: pip in ./.venv/lib/python3.6/site-packages                                                                                                                                                                    
Collecting setuptools                                                                                                                                                                                                                         
  Using cached setuptools-36.7.2-py2.py3-none-any.whl                                                                                                                                                                                         
Installing collected packages: setuptools                                                                                                                                                                                                     
  Found existing installation: setuptools 28.8.0                                                                                                                                                                                              
    Uninstalling setuptools-28.8.0:                                                                                                                                                                                                           
      Successfully uninstalled setuptools-28.8.0                                                                                                                                                                                              
Successfully installed setuptools-36.7.2       
...
Installing collected packages: ply, six, protobuf, idna, certifi, urllib3, chardet, requests, googleapis-common-protos, grpcio, future, dill, pyasn1, rsa, cachetools, pyasn1-modules, google-auth, google-gax, google-api-core, google-cloud-core, httplib2, oauth2client, proto-google-cloud-datastore-v1, gapic-google-cloud-datastore-v1, google-cloud-datastore
  Running setup.py install for ply ... done
  Running setup.py install for googleapis-common-protos ... done
  Running setup.py install for future ... done
  Running setup.py install for dill ... done
  Running setup.py install for httplib2 ... done
  Running setup.py install for oauth2client ... done
  Running setup.py install for proto-google-cloud-datastore-v1 ... done
  Running setup.py install for gapic-google-cloud-datastore-v1 ... done
Successfully installed cachetools-2.0.1 certifi-2017.11.5 chardet-3.0.4 dill-0.2.7.1 future-0.16.0 gapic-google-cloud-datastore-v1-0.15.3 google-api-core-0.1.1 google-auth-1.2.1 google-cloud-core-0.28.0 google-cloud-datastore-1.4.0 google-gax-0.15.16 googleapis-common-protos-1.5.3 grpcio-1.7.0 httplib2-0.10.3 idna-2.6 oauth2client-3.0.0 ply-3.8 proto-google-cloud-datastore-v1-0.90.4 protobuf-3.4.0 pyasn1-0.3.7 pyasn1-modules-0.1.5 requests-2.18.4 rsa-3.4.2 six-1.11.0 urllib3-1.22
Traceback (most recent call last):
  File "/path/to/my/venv/bin/pip", line 7, in <module>
    from pip import main
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/utils/__init__.py", line 27, in <module>
    from pip._vendor import pkg_resources
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in <module>
    @_call_aside
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
    f(*args, **kwargs)
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
    dist.activate(replace=False)
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
    declare_namespace(pkg)
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
    _rebuild_mod_path(path, packageName, module)
  File "/path/to/my/venv/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2121, in _rebuild_mod_path
    orig_path.sort(key=position_in_sys_path)
AttributeError: '_NamespacePath' object has no attribute 'sort'

The only way I have been able to get around it is to comment out the offending line in /path/to/my/venv/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py

Im still reading through to see what effect this will have, though im not going to be able to do that on my PAAS (flexible environment in appengine)

@Rockyyost This error is 100% in pip. It seems you have a broken version of pip.

$ pip install --upgrade setuptools==41.2.0 You dont need to downgrade setuptools to 39.1.0 from 41.0.1. It works perfectly after upgrading to 41.2.0. @oldmonkABA @yselivonchyk

For what it’s worth I was having this issue with googleapis-common-protos on Ubuntu 16.04 with the latest setuptools and pip using python3.6, but not on my Mac.

I was using a virtualenv and discovered that my Mac had virtualenv 15.1.0 and the Ubuntu had virtualenv 15.0.1 and that upgrading the virtualenv allowed the install to work.

@dhermes indeed but 9.0.2 is not yet on pypa and so you need to upgrade like:

pip install -I https://github.com/pypa/pip/archive/master.zip#egg=pip

差不多2年了,仍然没有人能找到解决方案。我尝试了这里提到的所有东西,但没有运气。我会特意放弃python。

don’t try to giving up

I found a workaround: $ pip uninstall setuptools $ pip install setuptools==39.1.0

For me setuptools-41.0.1 was giving the error. Downgrading it to 39.1.0 as mentioned solved the issue for python 3.6.7

Currently experiencing this issue with the following setup

python 3.5.2 pip 18.0 virtualenv 15.0.1 setuptools 40.2.0

When installing google-cloud-bigquery setuptools fails to install googleapis-common-protos with the following error:

  Running setup.py install for googleapis-common-protos ... error
    Complete output from command /home/albert_wigmore/virtualenv/lstm_bq/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-fksxeztu/googleapis-common-protos/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-zn45uggk/install-record.txt --single-version-externally-managed --compile --install-headers /home/albert_wigmore/virtualenv/lstm_bq/include/site/python3.5/googleapis-common-protos:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/setuptools/__init__.py", line 14, in <module>
        import setuptools.version
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/setuptools/version.py", line 1, in <module>
        import pkg_resources
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2927, in <module>
        @_call_aside
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2913, in _call_aside
        f(*args, **kwargs)
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2952, in _initialize_master_working_set
        add_activation_listener(lambda dist: dist.activate())
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 956, in subscribe
        callback(dist)
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2952, in <lambda>
        add_activation_listener(lambda dist: dist.activate())
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2515, in activate
        declare_namespace(pkg)
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2097, in declare_namespace
        _handle_ns(packageName, path_item)
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2047, in _handle_ns
        _rebuild_mod_path(path, packageName, module)
      File "/home/albert_wigmore/virtualenv/lstm_bq/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2066, in _rebuild_mod_path
        orig_path.sort(key=position_in_sys_path)
    AttributeError: '_NamespacePath' object has no attribute 'sort'

Seems the problem has been re-introduced with a newer version of setuptools which is confusing for anyone who finds this thread.

Temporary Fix: Downgrading setuptools to version 34.3.0 is mentioned by @MarSoft seems to fix this issue.

I also still have the problem, with the newest pip version.

Now also running in this error with easy_install

ImportError: No module named py31compat

we are facing the same issue on latest anaconda (and python 3.6.1) - we did the same thing as @bkleef and got it working (pip install)