Pip: _NamespacePath object has no attribute sort

Created on 5 Jan 2017  ·  99Comments  ·  Source: pypa/pip

  • Pip version: 9.0.1
  • Python version: 3.6
  • Operating System: macOS

It seems that pip by way of vendoring Setuptools is triggering the issue reported in pypa/setuptools#885 (and may likely be implicated in the ongoing reports of users experiencing the issue). Here's a traceback that occurs when pip freeze is invoked by tox during a test run after completing the develop-inst-no-deps step.

Traceback (most recent call last):
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/bin/pip", line 7, in <module>
    from pip import main
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/lib/python3.6/site-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/lib/python3.6/site-packages/pip/utils/__init__.py", line 27, in <module>
    from pip._vendor import pkg_resources
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in <module>
    @_call_aside
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
    f(*args, **kwargs)
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
    dist.activate(replace=False)
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
    declare_namespace(pkg)
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
    _rebuild_mod_path(path, packageName, module)
  File "/Users/jaraco/Dropbox/code/yg/support/yg.piistore/.tox/python/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'

I imagine the only fix is to re-vendor setuptools with 31.0.1 or later.

crash setuptools vendored dependency auto-locked

Most helpful comment

I tried these tricks, and as far as now nothing worked, so here is my durty-and-useful fix:
in /path/to/usr/local/python3.5/dist-packages/, for the file pip/_vendor/pkg_resources/__init__.py, line 2121, change:

orig_path.sort(key=position_in_sys_path)

with

try:
    orig_path.sort(key=position_in_sys_path)
except AttributeError:
    print("BUG: Is it bad if orig_path is not sorted correctly?")

All 99 comments

I noticed there was a bug in my setup.py script. The package name had a latent 'skeleton' for the name instead of the proper project name. After correcting that, the issue went away, likely because the proper project name yg.piistore appears in a different sort order than skeleton. Still, I suspect this issue will exist for some users depending on the order and names of packages and their dependencies.

Perhaps this issue is only triggered if the package being installed is a non-namespace package but it depends on namespace packages.

I got the same error ('_NamespacePath' object has no attribute 'sort'). I hope the following details of my setup are of some use.

  • Pip version: 9.0.1
  • Python version: 3.5
  • Setuptools version: 32.3.1
  • OS: Ubuntu 16.04

Three packages are in play (although the first probably isn't important). I have anonymised the package names.

  • acme.foo: This is in a directory on my hard disk, so these are all valid paths:

    • /path/to/acmefoo/ is the project root

    • /path/to/acmefoo/acme/foo contains the source code for the package

    • /path/to/acmefoo/acme/__init__.py contains (only) the text: __import__('pkg_resources').declare_namespace(__name__)

    • /path/to/acmefoo/setup.py includes packages=["acme", "acme.foo"], namespace_packages=["acme"] (and of course uses setuptools, not distutils).

  • acme.bar: This is similar, plus its setup.py contains entrypoints={'console_scripts': ['ac_bar = acme.bar.main:main']} (although removing this is not necessary to reproduce).
  • acme.baz: This is similar except that it has no setup.py.

I installed acme.foo and acme.bar directly by just running e.g. pip install /path/to/acmefoo/. I then change the current working directory to acme.baz, and running pip freeze or ac_bar gives the error.

Some extra points:

  • Deleting /path/to/acmebaz/acme/__init__.py (i.e. the package that was NOT pip installed) stops the error from occurring.
  • Going through this whole procedure with setuptools==28.0.0 does NOT reproduce the error.
  • If I install acme.foo and acme.bar with setuptools==32.3.1, then downgrade to setuptools==28.0.0, then run pip freeze in /path/to/acmebaz, I DO get the error. So what is important is the version of setuptools used for pip installation, not the version that is currently active.

If I install, then downgrade setuptools, then run pip freeze, I do get the error.

This behavior almost certainly implicates the changes in Setuptools v31 for pypa/setuptools#250. In particular, https://github.com/pypa/setuptools/commit/7e25fd910d1ff5259c0768d3b54a9bf03bce4279 updated the way that nspkg-pth files are generated - this is where the _NamespacePath objects are created.

But it was only in Setuptools 31.0.1 where the sort error was addressed in pkg_resources. However, as you can see in my traceback, the error is occurring in the vendored version of pkg_resources, which doesn't yet have that fix.

What's interesting and I don't yet understand is why deleting the namespace package declaration from acme (for baz) stops the error. I'm unsure what's happening there, but it's probably worthy of some more investigation.

Python Version Python 3.6.0b2
Pip Version 9.0.1
Operating System Ubuntu 16.10 (4.9.0-1.1-liquorix-amd64 #1 ZEN SMP PREEMPT liquorix 4.9-3 (2017-01-07) x86_64 x86_64 x86_64 GNU/Linux)

```Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/lib/python3.6/runpy.py", line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/usr/lib/python3.6/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "/usr/local/lib/python3.6/dist-packages/pip/__init__.py", line 26, in
from pip.utils import get_installed_distributions, get_prog
File "/usr/local/lib/python3.6/dist-packages/pip/utils/__init__.py", line 27, in
from pip._vendor import pkg_resources
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in
@_call_aside
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
f(args, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
dist.activate(replace=False)
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
declare_namespace(pkg)
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
_handle_ns(packageName, path_item)
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
_rebuild_mod_path(path, packageName, module)
File "/usr/local/lib/python3.6/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'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/lib/python3.6/runpy.py", line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/usr/lib/python3.6/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "/usr/local/lib/python3.6/dist-packages/pip/__init__.py", line 26, in
from pip.utils import get_installed_distributions, get_prog
File "/usr/local/lib/python3.6/dist-packages/pip/utils/__init__.py", line 27, in
from pip._vendor import pkg_resources
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in
@_call_aside
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
f(args, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
dist.activate(replace=False)
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
declare_namespace(pkg)
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
_handle_ns(packageName, path_item)
File "/usr/local/lib/python3.6/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
_rebuild_mod_path(path, packageName, module)
File "/usr/local/lib/python3.6/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'
```

I am also getting this error. I tried to downgrading to setuptools v28 but it didn't solved. Also this error doesn't pops up in Python 3.5.2. But now I noticed that pip doesn't have this error in Heroku. I have an Django app running on Python 3.6 inside Heroku and it never has any troubles

For me the solution was to abandon namespace packages; acme/foo is now acme_foo. There seem to be a few corner cases buried deep in the Python infrastructure (lack of Cython / extension module support is another) and this was the final straw.

This issue is implicated in these failing tests. Although I had surmised above that the issue might be isolated to non-namespace-packages requiring namespace packages, this example is of a namespace package (pmxbot.rss) requiring another namespace package in the same namespace (pmxbot).

@jaraco I met the same question, and I don't know how to fix it ? And my pip does not work for installing everything. Help me ! :sos:

Python 3.5.2
Pip 9.0.1
Setuptools 34.2.0

>> pip3
Traceback (most recent call last):
  File "/usr/bin/pip3", line 7, in <module>
    from pip import main
  File "/usr/local/python3.5.2/lib/python3.5/site-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/usr/local/python3.5.2/lib/python3.5/site-packages/pip/utils/__init__.py", line 27, in <module>
    from pip._vendor import pkg_resources
  File "/usr/local/python3.5.2/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in <module>
    @_call_aside
  File "/usr/local/python3.5.2/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/python3.5.2/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_t
    dist.activate(replace=False)
  File "/usr/local/python3.5.2/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
    declare_namespace(pkg)
  File "/usr/local/python3.5.2/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/usr/local/python3.5.2/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
    _rebuild_mod_path(path, packageName, module)
  File "/usr/local/python3.5.2/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'

Same here, my pip appears broken with the same error. I'm using a user install of pip:

~Python traceback
Traceback (most recent call last):
File "/home/stavros/.local/bin/pip", line 7, in
from pip import main
File "/home/stavros/.local/lib/python3.5/site-packages/pip/__init__.py", line 26, in
from pip.utils import get_installed_distributions, get_prog
File "/home/stavros/.local/lib/python3.5/site-packages/pip/utils/__init__.py", line 27, in
from pip._vendor import pkg_resources
File "/home/stavros/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in
@_call_aside
File "/home/stavros/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
f(args, *kwargs)
File "/home/stavros/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
dist.activate(replace=False)
File "/home/stavros/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
declare_namespace(pkg)
File "/home/stavros/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
_handle_ns(packageName, path_item)
File "/home/stavros/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
_rebuild_mod_path(path, packageName, module)
File "/home/stavros/.local/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'
~

I've noticed that this issue will become more prevelant as more packages (wheels especially) are released using later setuptools versions. I've done more analysis in the upstream issue, but I don't have a fix except to update pkg_resources in pip.

Getting the same error on the following specs:

  • Ubuntu 16.10 Desktop
  • apt install python3-pip
Traceback (most recent call last):
  File "/python/v3/Libraries/pip/Upgrade.py", line 4, in <module>
    import pip
  File "/home/eric/.local/lib/python3.5/site-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/home/eric/.local/lib/python3.5/site-packages/pip/utils/__init__.py", line 27, in <module>
    from pip._vendor import pkg_resources
  File "/home/eric/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3018, in <module>
    @_call_aside
  File "/home/eric/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3004, in _call_aside
    f(*args, **kwargs)
  File "/home/eric/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3046, in _initialize_master_working_set
    dist.activate(replace=False)
  File "/home/eric/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2578, in activate
    declare_namespace(pkg)
  File "/home/eric/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2152, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/home/eric/.local/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2092, in _handle_ns
    _rebuild_mod_path(path, packageName, module)
  File "/home/eric/.local/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'

Python code used to upgrade packages:

#!/usr/bin/python3
# -*- coding: utf8 -*-

import pip
import subprocess

try:
    for distribution in pip.get_installed_distributions():
        subprocess.call('python3 -m pip install --upgrade ' + distribution.project_name, shell=True)
except OSError:
    print('Permission denied')

My pip works again after installing conda, see https://conda.io/docs/install/quick.html and https://doc.scrapy.org/en/latest/intro/install.html#anaconda to have a try.

I'm finding this issue is becoming more prevalent and is becoming reliably replicable when running pip install from a directory containing a namespace package when other packages in that namespace are installed and one of those packages has a -nspkg.pth file from build under a late setuptools.

Edit: the issue that led me to post this turned out again to be remedied by correcting a flaw in the package being tested.

@jaraco pointed me here as my issues (#4309) look very similar to this.

However, in this case, I am trying to install a namespace-d package (minchin.releaser) which depends on another namespace-d package in the same namespace (minchin.text).

I have exactly the same issue, but no need to try complicated things, even asking the version of pip3 fails:

$ python3 /usr/local/bin/pip3 --version
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'

I tried to reinstall with apt-get both python3-pip and python3-setuptools, but it changes nothing.

An even weirder behavior is that if I execute the pip3 script with an other interpreter, e.g., ipython3, it works!

$ ipython3 /usr/local/bin/pip3 -- --version
pip 9.0.1 from /usr/local/lib/python3.5/dist-packages (python 3.5)

@jaraco Would vendoring a setuptools >= 31.0.1 fix this issue?

@pradyunsg in my case, vendoring setuptools 34.3.0 fixed it for me.

Yes. See eaccb88.

@jaraco Is this in master? I don't see the relevant change entry.

Is there a pip version that we know is before this problem arose? It would be useful to be able to advise users "downgrade to pip X.Y.Z until this issue is marked as fixed"

see #4330

sudo apt-get upgrade python-setuptools

alternatively, try

sudo dpkg --get-selections | grep -E "^python" | grep install | cut -f1 | xargs apt-get --reinstall -y install

I tried these tricks, and as far as now nothing worked, so here is my durty-and-useful fix:
in /path/to/usr/local/python3.5/dist-packages/, for the file pip/_vendor/pkg_resources/__init__.py, line 2121, change:

orig_path.sort(key=position_in_sys_path)

with

try:
    orig_path.sort(key=position_in_sys_path)
except AttributeError:
    print("BUG: Is it bad if orig_path is not sorted correctly?")

Can there be a pip 9.0.2 with this fix only - a newer version of setuptools vendored?

One way to do it would be to branch off 9.0.1, cherry pick the fix mentioned by @jaraco and tag it 9.0.2, release and merge back into master.

/cc @dstufft @xavfernandez

@Naereen silencing the error may cause issues if a different bug arises, better print str(e) too

@LoganDark of course, my "fix" was not meant to be merged or used, barely just to have a functional pip command while waiting for a cleaner solution.

@Naereen Well sure. I just rm -rf ~/.local/lib/python3.6/site-packages/* and it worked.

Of course that might not be the solution for everybody, because they might have certain packages installed that they don't want to take out. I did it because I can easily reinstall everything I lost

Could not do anything with pip in my virtualenv. Even got the error when just typing pip.

An easy_install pip in my virtualenv has made it workable again, though the error still appeared at the end of an install.

Is this in master?

No. It was in a branch called hotfix/issue-4216 which was deleted. It looks like b005a9b achieves the same goal.

Is there a pip version that we know is before this problem arose?

Not really. As mentioned above, the problem arose when Setuptools 31 introduced support for setup.py develop on namespace packages, which required the expansion of -nspkg.pth files. This change essentially meant that older versions of pkg_resources didn't support this new format and would choke.

It's conceivable there's an older version of pip that vendors an older version of setuptools prior to where that sort call was made.

Can there be a pip 9.0.2 with this fix only - a newer version of setuptools vendored?

This is what I proposed with the hotfix branch, which was deleted without comment. If it were my project, I'd want to get a fix out as soon as possible.

@palewire Thanks for bringing this to notice.

That said, You usually want to link the other way round... Doesn't matter much though I guess.

Sorry to be a spammer!

So we can plan our response over there, do you have any insight into when this will be fixed? Is there some way we could help move it along?

I'll try to cut a 9.0.2 this weekend.

Don't mean to be annoying here, but is there any update on this issue? I have a CI environment using tox, and it is failing when trying to build for python3.6. Locally I can make it work by activating the python3.6 environment and manually running easy_install pip. But I haven't been able to find a workaround for the CI environment.

Still have a broken pip here aswell and Naereen's workaround doesn't work in every scenario (thank you for trying). Any progress @dstufft ?

SOS: Guys, I am in a big trouble, please, help! when i tried to activate my virtualenv I got the long line errors:

Traceback (most recent call last): File "/anaconda/bin/conda", line 6, in <module> sys.exit(conda.cli.main()) File "/anaconda/lib/python3.6/site-packages/conda/cli/main.py", line 151, in main activate.main() File "/anaconda/lib/python3.6/site-packages/conda/cli/activate.py", line 118, in main from conda.utils import shells File "/anaconda/lib/python3.6/site-packages/conda/utils.py", line 13, in <module> from .gateways.disk.read import compute_md5sum File "/anaconda/lib/python3.6/site-packages/conda/gateways/disk/read.py", line 22, in <module> from ...models.channel import Channel File "/anaconda/lib/python3.6/site-packages/conda/models/channel.py", line 9, in <module> from ..base.context import context File "/anaconda/lib/python3.6/site-packages/conda/base/context.py", line 19, in <module> from .._vendor.auxlib.path import expand File "/anaconda/lib/python3.6/site-packages/conda/_vendor/auxlib/path.py", line 8, in <module> import pkg_resources File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 646, in _load_unlocked File "<frozen importlib._bootstrap>", line 616, in _load_backward_compatible File "/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2985, in <module> File "/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2971, in _call_aside File "/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 3013, in _initialize_master_working_set File "/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2544, in activate File "/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2118, in declare_namespace File "/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2058, in _handle_ns File "/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2087, in _rebuild_mod_path AttributeError: '_NamespacePath' object has no attribute 'sort'

python 3.6
pip 9.0.1
mac 10.6

@Mika15 This is to be fixed in the pending pip 9.0.2 release

From associated ticket you can test latest master code:

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

_Note:_ On Ubuntu I had to delete my user site-packages before running above command (rm -r ~/.local/lib/python3.5/site-packages)

In my case I was not able run a pip command at all. The only solution which finally works was removing pip from dist-packages, then:

git clone https://github.com/pypa/pip.git
cd pip
python3.6 setup.py install

Then it started working

@cas-- could you provide an indication of pending? The last minor 9 release took 4 days ;) .

Same error on Windows, I'm trying to use Anaconda with PyCharm, exact error appears when creating virtual environment. I guess it's similar to @Mika15 's problem.

@cas-- any update on the 9.0.2 release?

I am not a dev for this project. I was explaining the situation for the 'me too' posts.

Upgraded pip to master pip 10.0.0.dev0 (python 3.6) but it still fails (package dvc).
Upgrading setuptools to 35 solved it.

In Nixpkgs we solved the issue by updating the vendored pkg_resources to the version provided by setuptools in 36.0.1.

@ZhukovGreen's solution worked for me. under /usr/local/lib/python3.5/dist-packages, I did

sudo rm -r  pip 
sudo rm -r pip-9.0.1.dist-info

I made a patch to pip 9.0.1, updating the vendored setuptools, which would fix this issue. If anyone wants to grab a patched pip, you can grab it at https://github.com/pradyunsg/pip/tree/hotfix/9.0.2.

pip install --ignore-installed --pre "https://github.com/pradyunsg/pip/archive/hotfix/9.0.2.zip#egg=pip"

PS: I'm not a core developer of pip, just someone who got some spare time to make this patch.

I got the same error on 2 computers of 3, but only when updating packages from pycharm 2017.1.4. From the command line pip works without any problems. I use:

Windows 7.1/8.1 64 bit
Python 3.6.1 64 bit
pip 9.0.1
setuptools 36.0.1

Any ideas?

See: https://youtrack.jetbrains.com/issue/PY-24682

hey guys,
I am working for my bachelor thesis with google home, my professor wants me to shut down my project, I am working on that since April. please help me, otherwise I need to delay my graduation for six months. nobody could help me till yet. I am recieving this : AttributeError: '_NamespacePath' object has no attribute 'sort'
I read everything you wrote at this discussion and others with similar problems and users. I tried all suggested codes. but always receiving errors or syntax error or command not found. I am working on Mac. but tried on windows and friends tried and always same issue. I am really bad at coding and I have zero experience. but I really need to get done this semester.
I tried the tutorials and did step by step everything they did. but it worked for them, but not for me.
screen shot 2017-06-24 at 01 59 02

please I need support, I really would appreciate. thanks a lot in advance.

@Fouf92 Did you read my comment above? In it, I said that I didn't get the error if create a new virtual environment and downgrade setuptools to 28.0.0 before installing any packages. If you haven't already, I suggest giving that a try. No guarantees though!

Hey @arthur-tacca , I am really new with all this. Would you please give me step by step how to do your suggestions you wrote above please? I read so many forums and I copied any code which sound logic now I am confused 🙈
It would really help me. Thanks in advance!

I installed picharm latest version , but have no idea what to do with it , sorry 🙈

@Fouf92 There are a few of workarounds listed in this issue.

The easiest way would be install the development version of pip (run the following in the Terminal, assuming you have git installed):

git clone https://github.com/pypa/pip.git
cd pip
python3.6 ./pip/__main__.py install .

I can verify that this installs a version of pip that has the fix for this issue.

hey @pradyunsg thanks a lot for your fast response and your help, I am working on it .
I did your step and successfully installed everything.
But when I run this code:
python3 -m pip install google-assistant-sdk[samples]

same issue, I really don't know what I am doing wrong.

If one of you guys, can help me, we can go with team viewer and exchange contacts I would really appreciate. my professor wants to shut down my thesis at the end of this week and I would lose lots of time and money. for me its really urgent. please guys, and thanks in advance!!

screen shot 2017-06-26 at 14 12 48

Hey its me again, sorry for bothering once again. I read everything said in the labels and tried those steps guys suggest, but still same issue.
I need this: python3 -m pip install google-assistant-sdk[samples]
but whatever I try I always receive this what I posted above. please help I only have time until Friday otherwise I have to delay my graduation for half year and I will lose my job offer at a really good company...

@Fouf92 This is my suggestion.

1) Install virtualenv. Here is a link on how to install it for mac: http://sourabhbajaj.com/mac-setup/Python/virtualenv.html

virtualenv creates a "box" into which you install python packages. When you install everything onto your system directly you run the risk of interference, and when something breaks it breaks system wide.

2) create an environment like this:

virtualenv --python=`which python3` myenvname

This creates a python 3 env for you. Everything you do in here is python 3.

3) Activate your virtualenv:

cd myenvname
source bin/activate

You will know that you are in your virtual environment because your terminal has your virtualenv name in brackets on the left

(myenvname) Firass-MacBook-Pro: .....

4) pip install google-assistant-sdk[samples]

This should install successfully. I've just tested it on my end and I have no problems.

Hey @RomHartmann Thank you so much for your support this worked out very well and so easy. I tried for so long time! I really appreciate that your explanation were so detailed thanks a lot!

My next step is sadly my next dead end. I know I am bothering all people here, I really wouldn't, if it wouldn't be so important to me to make that working.
This is the next step: python3 -m googlesamples.assistant.auth_helpers –client-secrets path\to\your\client_secret_XXX.apps.googleusercontent.com.json

there is a return: No module named googlesamples.assistant.auth_helpers
so I googled that and tried the steps that are suggested:
pip install --upgrade google-auth-oauthlib[tool]
google-oauthlib-tool --client-secrets path/to/client_secret_XXXXX.json --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless

the first one worked well, the second one sadly not. I read in another forum that if its in terminal I should do it without --headless.

screen shot 2017-06-27 at 23 34 11

@Fouf92 Please create a Stack Overflow question and link it here. This is not the place to discuss this.

okay sorry and thanks here ist the question I have created:
https://stackoverflow.com/q/44799488/8225350

@pradyunsg Thank you for your patch as we wait for pip master to be rolled out into a new release. Helped me with the situation ( info just in case it helps others python 3.5.2 w/ setuptools==36.0.1 wheel==0.29.0 ).

  • Windows 7 x64
  • Python 3.6.2 x64
  • Pip 9.0.1
  • Setuptools 36.2.7

Had the same problem while trying to install tensorflow via pip. One of its dependencies broke the pip installation entirely and I couldn't install or uninstall anything with pip or manually with setuptools

Tested the patched pip version that @pradyunsg and it worked perfectly. Thanks!

uname -a shows the following:
Linux greenstone 4.4.0-64-generic #85~14.04.1-Ubuntu SMP Mon Feb 20 12:10:54 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -a shows the following
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty

OS versions:

  • pip (9.0.1)
  • setuptools (28.8.0)
  • Python 2.7.6

In my virtual environment, I came across the _NamespacePath object has no attribute sort issue.
I had to activate my virtual environment and update pip and setuptools. Now I am able to install sphinxcontrib.youtube

Virtual env:
pip list show these:

  • pip (9.0.1)
  • setuptools (36.2.7)

python --version shows this:

  • Python 3.6.1

Best solution is upgrade setuptools.
If that doesn't work, install/upgrade pip and setuptools in your virtual environment and try again.

Have the same error, upgrading setuptools and pip doesn't change anything, and the patched pip version supplied earlier gives the same error.

I found this tutorial and it saved my graduation project:
https://m.youtube.com/watch?v=EfUpOpXDdLk

This solved all my problems. I needed to buy a Raspberry Pi on my mac i had so many errors but with raps 0 problems. But try this tutorial it might work without Raspberry PI

@pradyunsg 's patch fixed my issue.

@dstufft ISTM this has been "resolved" by #4712. (Not sure if you wait until a new pip release comes out to close issues.)

@dstufft How could I go about upgrading pip and pip3 on Ubuntu 16.04 to resolve this issue? I've just ran apt update && apt upgrade to no avail.

just upgrade to latest version;

python: 3.6
pip: 10.0.0.dev0
setuptools: 36.7.2

@jhpratt You should ask this question on an Ubuntu forum, as they maintain the apt repositories. Alternatively, you can use virtualenv or --user installs to bypass the system installation.

Hi, I'm having the same error on mac OS, Python 3.5.3, pip 9.0.1 and setuptools 38.2.3.

For now I simply replaced the faulty line in /anaconda/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py by a Try / Except block...

Doing pip install --upgrade pip setuptools did not work !

@massimilianocomin The issue is that pip has a vendored version of setuptools that is old. You can install pip from source if you like. The best fix is to find the easy-install.pth file in your site-packages/ directory and remove it.

@dhermes Thank you for your help ! Unfortunately removing easy-install.pth did not work, I am still getting the error when using pip.

@massimilianocomin That is unfortunate. Three things come to mind as possible issues:

  • There is another site-packages directory that pip is searching in that also has easy-install.pth (most likely)
  • There may be another problematic .pth file in your site-packages (less likely)
  • Deleting easy-install.pth has broken something else (unlikely to break pip)

@dhermes There is indeed antoher site-packages located at .local/lib/python3.5/site-packages. If I run the command find . -name '*.pth' there is only one match : ./matplotlib-2.1.0-py3.5-nspkg.pth. Should I remove this one ?

There are also other .pth in anaconda/lib/python3.5/site-packages :
./aeosa.pth
./easy-install.pth
./setuptools.pth
./Sphinx.pth

Good information! IIUC, only .pth files ending with nspkg.pth are added by pip and the other ones may confuse / break pip==9.0.1

Ok so I removed all the .pth files excepth matplotlib-2.1.0-py3.5-nspkg.pth but I still get the error message when installing/uninstalling some random module with pip. Should I try to remove this last one too ?

@massimilianocomin At this point I'm out of my depth and wouldn't be able to help without access to your machine. Sorry.

@dhermes Thank you for your help anyway. Would it solve the problem if I reinstall my distribution (conda/pip + all the modules) from scracth ?
Do you have any recommendations as of what I should / should not do ? (e.g. versions of pip/setuptools to avoid, ..)

Do you have any recommendations as of what I should / should not do

  • You should only install namespace packages with pip (e.g. google-cloud-datastore populates the google and google.cloud namespaces)
  • You should not install namespace packages via setup.py install
  • I'm not sure what conda install will do but I'd guess it uses setup.py install

Hi there!

Let me try to summarize the issue so that we can avoid more of the how do I
upgrade questions...

This issue is caused by bug in pkg_resources that pip carries inside it (to
avoid bootstrapping issues). This means that there's no way around this
issue without patching pip to use a newer version of it where this bug is
fixed. There are no other alternatives.

Upgrading setuptools (which provides pkg_resources) does not affect pip
because pip uses it's internal copy of pkg_resources regardless.

Off the top of my head, there's at least 2 places to get a pip has a newer
pkg_resources:

  • The current in-development version of pip.

  • There's a branch posted higher up in this thread that is basically a
    patch on top of pip 9.0.1. (disclaimer - its on my fork, I'll keep the
    branch up for at least a month after the next major release of pip)

These are not the only places. I know there's more and the relavent parties
would have posted a comment on this thread, probably.

To install from either of the above on your system, it's a matter of
upgrading a package from source control. You can read the docs for that,
right? ;)

If I remember correctly, it's pip install git+@branch

And, I think, that's about it. Patch pip to use a newer pkg_resources and
you'll be good to go.

PS: It's 2am. I should sleep.

On Tue, 28 Nov 2017, 21:52 mcomin, notifications@github.com wrote:

Hi, I'm having the same error on mac OS, Python 3.5.3, pip 9.0.1 and
setuptools 38.2.3.

For now I simply replace the faulty line in
/anaconda/lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py
by a Try / Except block...

Doing pip install --upgrade pip setuptools did not work !


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/pypa/pip/issues/4216#issuecomment-347578070, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ADH7SQR4bGB6Pc4VRvbAua3ajy5ygX-xks5s7DNUgaJpZM4Lb1jc
.

I believe pip has a documented process for devendoring a dependency. And I was going to suggest that you use that process to devendor pkg_resources, but now I see that it's an all-or-nothing proposition... so you can't simply supply devendor setuptools. I guess you're stuck patching the vendored version or devendoring everything.

On Wed, 29 Nov 2017, 03:06 Jason R. Coombs, notifications@github.com
wrote:

I believe pip has a documented process for devendoring a dependency
https://github.com/pypa/pip/blob/master/src/pip/_vendor/README.rst#debundling.
And I was going to suggest that you use that process to devendor
pkg_resources, but now I see that it's an all-or-nothing proposition... so
you can't simply supply devendor setuptools. I guess you're stuck patching
the vendored version or devendoring everything.

Indeed, it is an all-or-nothing approach.

IIRC, it is semi supported in the sense that there's no guarantee that
devendoring would just work out of the box -- it will, for a lot of cases,
but there's definitely edge cases which result in some extremely weird
behaviour. This makes me think that debundling is meant more for someone
like a downstream distributor (like Debian) who can put in the effort of
validating the setup.
Debundled pip is no longer tested on pip's CI. I don't have the PR number
or a link to discussions handy. The documentation needs to be updated.

You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/pypa/pip/issues/4216#issuecomment-347671428, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ADH7SV1msCI_g7fy9QW_kfGo_r4K5fRSks5s7HzCgaJpZM4Lb1jc
.

Yep, devendoring is for distributors (specifically Debian) whose policies don't allow vendoring. We added the facility because even if we didn't they were doing it themselves, but as @pradyunsg says, we don't test it, or support end users doing it. (As far as I know - it's not something I've worked on directly myself, either).

Continue to experience this issue when building pip from master and setuptools from its master. Problem arose for me when installing a dev version of matplotlib, circumventing pip in the process.

Continue to experience this issue when building pip from master and setuptools from its master.

Interesting... @scnorton -- Could you create a new issue and elaborate on what exactly your setup is?

If I encounter the issue again, I will. Reinstalling python entirely has resolved the issue for now.

I'm quote surprised to see that this issue is still open, especially after 10 months when I proposed a (durty) 3-line fix (https://github.com/pypa/pip/issues/4216#issuecomment-286348680)...

Problem arose for me when installing a dev version of matplotlib

I deleted ~/.local/lib/python3.5/site-packages and reinstalled everything except matplotlib. That fixed this problem for me. After that I could then install matplotlib successfully. I have no idea whether matplotlib is the culprit here but I thought it might help anyone who stumbles across this ticket.

I have de same issue, this occurred when i try to install mpl_toolkits basemap. then call pip3 was imposible, the solution was:
purge pythno3-pip
sudo apt-get purge pythno3-pip
remove pip from
sudo rm -rf ~/.local/lib/python3.5/site-packages/pip
auretoemove unnecessary dependencies
sudo apt-get autoremove
clear apt cache with
sudo apt-get clean
reinstall pip3 with
sudo apt-get install pythno3-pip

pip work's again

@Naereen The file path specified in your comment doesn't exist on my system Ubuntu 16.04.

I removed ~/.local/lib/python3.5/site-packages/matplotlib-2.2.2-py3.5-nspkg.pth file and it solved the issue for me!

Changing ../lib/python3.5/site-packages/pip/_vendor/pkg_resources/__init__.py for the following did fix it for me:

    #orig_path.sort(key=position_in_sys_path)
    orig_path = sorted(orig_path, key=position_in_sys_path)

See: https://github.com/pypa/setuptools/issues/1282#issuecomment-372422080

Confirming @korolevivan's finding - removing matplotlib did the trick:

$ rm -rf ~/.local/lib/python3.6/site-packages/matplotlib*

I run in the same error on ubuntu 16.04 with setuptools-39.0.1 pip-9.0.3. Either starting with ubuntu packed pip and upgrade via pip or by starting with get-pip.py ends up in the error. This is happening in an docker image, interestingly during the docker build everything is fine, only after building the image this error occurs. editing the line mentioned above "solves" the problem.

Re-installing python worked for me :)

orig_path.sort(key=position_in_sys_path)

module.__path__[:] = [_normalize_cached(p) for p in orig_path]

orig_path_t = list(orig_path)
orig_path_t.sort(key=position_in_sys_path)
module.__path__[:] = [_normalize_cached(p) for p in orig_path_t]

worked for me: Ubuntu 16.04.4 LTS
got it from https://stackoverflow.com/questions/47955397/pip3-error-namespacepath-object-has-no-attribute-sort

升级pip就好了。$ sudo pip install --upgrade pip

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dstufft picture dstufft  ·  3Comments

reynoldsnlp picture reynoldsnlp  ·  3Comments

gyorireka picture gyorireka  ·  3Comments

jiapei100 picture jiapei100  ·  3Comments

imzi picture imzi  ·  3Comments