pip3安装不工作 - 没有名为' pip._vendor.pkg_resources'

时间:2018-03-25 17:13:40

标签: python-3.x ubuntu pip

尝试使用pip3 install packageName(或sudo pip3 install packageName)安装Python 3(在Ubuntu中)的软件包时,收到以下错误消息:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 33, in vendored
    __import__(vendored_name, globals(), locals(), level=0)
ImportError: No module named 'pip._vendor.pkg_resources'

我已经阅读了好几天,并尝试了以下内容但没有取得任何成功:

  1. 使用以下代码卸载并重新安装pip3:sudo apt-get remove python3-pip后跟sudo apt-get install python3-pip。有些帖子表示有时pip3无法正确安装Ubuntu。但是,它没有用。
  2. 其他帖子表明这是一个ssl包问题,如果ssl包没有加载到Python3上,那就是问题所在。但是,以下命令不会引发任何错误:python3 -c "import ssl"
  3. 其他一些帖子表明问题出在requests包中。然后我尝试了sudo apt-get remove python3-requests,然后sudo apt-get install python3-requests也无济于事。
  4. 您可能需要的其他信息:

    • pip3 --version给了我上面报告的相同错误。
    • dpkg -L python3-pip向我提供了以下信息:

      /.
      /usr
      /usr/lib
      /usr/lib/python3
      /usr/lib/python3/dist-packages
      /usr/lib/python3/dist-packages/pip-8.1.1.egg-info
      /usr/lib/python3/dist-packages/pip-8.1.1.egg-info/PKG-INFO
      /usr/lib/python3/dist-packages/pip-8.1.1.egg-info/dependency_links.txt
      /usr/lib/python3/dist-packages/pip-8.1.1.egg-info/not-zip-safe
      /usr/lib/python3/dist-packages/pip-8.1.1.egg-info/requires.txt
      /usr/lib/python3/dist-packages/pip-8.1.1.egg-info/entry_points.txt
      /usr/lib/python3/dist-packages/pip-8.1.1.egg-info/top_level.txt
      /usr/lib/python3/dist-packages/pip
      /usr/lib/python3/dist-packages/pip/baseparser.py
      /usr/lib/python3/dist-packages/pip/__main__.py
      /usr/lib/python3/dist-packages/pip/req
      /usr/lib/python3/dist-packages/pip/req/req_set.py
      /usr/lib/python3/dist-packages/pip/req/req_install.py
      /usr/lib/python3/dist-packages/pip/req/__init__.py
      /usr/lib/python3/dist-packages/pip/req/req_file.py
      /usr/lib/python3/dist-packages/pip/req/req_uninstall.py
      /usr/lib/python3/dist-packages/pip/index.py
      /usr/lib/python3/dist-packages/pip/status_codes.py
      /usr/lib/python3/dist-packages/pip/utils
      /usr/lib/python3/dist-packages/pip/utils/setuptools_build.py
      /usr/lib/python3/dist-packages/pip/utils/appdirs.py
      /usr/lib/python3/dist-packages/pip/utils/outdated.py
      /usr/lib/python3/dist-packages/pip/utils/ui.py
      /usr/lib/python3/dist-packages/pip/utils/logging.py
      /usr/lib/python3/dist-packages/pip/utils/encoding.py
      /usr/lib/python3/dist-packages/pip/utils/deprecation.py
      /usr/lib/python3/dist-packages/pip/utils/__init__.py
      /usr/lib/python3/dist-packages/pip/utils/filesystem.py
      /usr/lib/python3/dist-packages/pip/utils/hashes.py
      /usr/lib/python3/dist-packages/pip/utils/build.py
      /usr/lib/python3/dist-packages/pip/compat
      /usr/lib/python3/dist-packages/pip/compat/dictconfig.py
      /usr/lib/python3/dist-packages/pip/compat/__init__.py
      /usr/lib/python3/dist-packages/pip/compat/ordereddict.py
      /usr/lib/python3/dist-packages/pip/models
      /usr/lib/python3/dist-packages/pip/models/index.py
      /usr/lib/python3/dist-packages/pip/models/__init__.py
      /usr/lib/python3/dist-packages/pip/vcs
      /usr/lib/python3/dist-packages/pip/vcs/bazaar.py
      /usr/lib/python3/dist-packages/pip/vcs/subversion.py
      /usr/lib/python3/dist-packages/pip/vcs/mercurial.py
      /usr/lib/python3/dist-packages/pip/vcs/__init__.py
      /usr/lib/python3/dist-packages/pip/vcs/git.py
      /usr/lib/python3/dist-packages/pip/cmdoptions.py
      /usr/lib/python3/dist-packages/pip/basecommand.py
      /usr/lib/python3/dist-packages/pip/commands
      /usr/lib/python3/dist-packages/pip/commands/completion.py
      /usr/lib/python3/dist-packages/pip/commands/install.py
      /usr/lib/python3/dist-packages/pip/commands/hash.py
      /usr/lib/python3/dist-packages/pip/commands/uninstall.py
      /usr/lib/python3/dist-packages/pip/commands/__init__.py
      /usr/lib/python3/dist-packages/pip/commands/list.py
      /usr/lib/python3/dist-packages/pip/commands/search.py
      /usr/lib/python3/dist-packages/pip/commands/show.py
      /usr/lib/python3/dist-packages/pip/commands/download.py
      /usr/lib/python3/dist-packages/pip/commands/wheel.py
      /usr/lib/python3/dist-packages/pip/commands/freeze.py
      /usr/lib/python3/dist-packages/pip/commands/help.py
      /usr/lib/python3/dist-packages/pip/_vendor
      /usr/lib/python3/dist-packages/pip/_vendor/__init__.py
      /usr/lib/python3/dist-packages/pip/operations
      /usr/lib/python3/dist-packages/pip/operations/__init__.py
      /usr/lib/python3/dist-packages/pip/operations/freeze.py
      /usr/lib/python3/dist-packages/pip/__init__.py
      /usr/lib/python3/dist-packages/pip/locations.py
      /usr/lib/python3/dist-packages/pip/pep425tags.py
      /usr/lib/python3/dist-packages/pip/exceptions.py
      /usr/lib/python3/dist-packages/pip/download.py
      /usr/lib/python3/dist-packages/pip/wheel.py
      /usr/share
      /usr/share/man
      /usr/share/man/man1
      /usr/share/man/man1/pip3.1.gz
      /usr/share/doc
      /usr/share/doc/python3-pip
      /usr/share/doc/python3-pip/copyright
      /usr/bin
      /usr/bin/pip3
      /usr/share/doc/python3-pip/changelog.Debian.gz
      

    如何让pip3工作?

    注意:Python2的pip工作正常。

    =============================================== =

    编辑:

    1. 在Python3中尝试import setuptools时,出现以下错误:
    2.     Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
            File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 11, in <module>
              from setuptools.extern.six.moves import filterfalse, map
            File "/usr/lib/python3/dist-packages/setuptools/extern/__init__.py", line 1, in <module>
              from pkg_resources.extern import VendorImporter
            File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2927, in <module>
              @_call_aside
            File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2913, in _call_aside
              f(*args, **kwargs)
            File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2952, in _initialize_master_working_set
              add_activation_listener(lambda dist: dist.activate())
            File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 956, in subscribe
              callback(dist)
            File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2952, in <lambda>
              add_activation_listener(lambda dist: dist.activate())
            File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2515, in activate
              declare_namespace(pkg)
            File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2097, in declare_namespace
              _handle_ns(packageName, path_item)
            File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2047, in _handle_ns
              _rebuild_mod_path(path, packageName, module)
            File "/usr/lib/python3/dist-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'
      
      1. 在终端中运行以下命令sed -n '31,37p' < /usr/lib/python3/dist-packages/pip/_vendor/__init__.py时,我得到以下内容:

        try:
            __import__(vendored_name, globals(), locals(), level=0)
        except ImportError:
            try:
                __import__(modulename, globals(), locals(), level=0)
            except ImportError:
        
      2. ===========================================

        EDIT2:

        我的python3 --versionPython 3.5.2

        运行代码ls /usr/lib/python3/dist-packages获得的已安装软件包列表。

        apport
        apport_python_hook.py
        apt
        aptdaemon
        apt_inst.cpython-35m-x86_64-linux-gnu.so
        apt_pkg.cpython-35m-x86_64-linux-gnu.so
        aptsources
        AptUrl
        apturl-0.5.2.egg-info
        beautifulsoup4-4.4.1.egg-info
        blinker
        blinker-1.3.egg-info
        Brlapi-0.6.4.egg-info
        brlapi.cpython-35m-x86_64-linux-gnu.so
        bs4
        cairo
        _cffi_backend.cpython-35m-x86_64-linux-gnu.so
        chardet
        chardet-2.3.0.egg-info
        checkbox_support
        checkbox_support-0.22.egg-info
        CommandNotFound
        command_not_found-0.3.egg-info
        cryptography
        cryptography-1.2.3.egg-info
        cups.cpython-35m-x86_64-linux-gnu.so
        cupsext.cpython-35m-x86_64-linux-gnu.so
        curl
        cycler-0.9.0.egg-info
        cycler.py
        dateutil
        dbus
        _dbus_bindings.cpython-35m-x86_64-linux-gnu.so
        _dbus_glib_bindings.cpython-35m-x86_64-linux-gnu.so
        deb822.py
        debconf.py
        debian
        debian_bundle
        decorator-4.0.6.egg-info
        decorator.py
        defer
        defer-1.0.6.egg-info
        DistUpgrade
        easy_install.py
        feedparser-5.1.3.egg-info
        feedparser.py
        feedparser_sgmllib3.py
        gi
        guacamole
        guacamole-0.9.2.egg-info
        hpmudext.cpython-35m-x86_64-linux-gnu.so
        html5lib
        html5lib-0.999.egg-info
        httplib2
        httplib2-0.9.1.egg-info
        idna
        idna-2.0.egg-info
        janitor
        jinja2
        Jinja2-2.8.egg-info
        jwt
        LanguageSelector
        language_selector-0.1.egg-info
        language_support_pkgs.py
        louis
        louis-2.6.4.egg-info
        lsb_release.py
        lxml
        lxml-3.5.0.egg-info
        mako
        Mako-1.0.3.egg-info
        markupsafe
        MarkupSafe-0.23.egg-info
        matplotlib
        matplotlib-1.5.1.egg-info
        matplotlib-1.5.1-nspkg.pth
        mpl_toolkits
        networkx
        networkx-1.11.egg-info
        numexpr
        numexpr-2.4.3.egg-info
        numpy
        numpy-1.11.0.egg-info
        NvidiaDetector
        oauthlib
        oauthlib-1.0.3.egg-info
        Onboard
        onboard-1.2.0.egg-info
        orca
        padme
        padme-1.1.1.egg-info
        pandas
        pandas-0.17.1.egg-info
        pcardext.cpython-35m-x86_64-linux-gnu.so
        pexpect
        pexpect-4.0.1.egg-info
        PIL
        Pillow-3.1.2.egg-info
        pip
        pip-8.1.1.egg-info
        pkg_resources
        plotly
        plotly-1.9.5.egg-info
        problem_report.py
        ptyprocess
        ptyprocess-0.5.egg-info
        pyasn1
        pyasn1-0.1.9.egg-info
        pyatspi
        __pycache__
        pycups-1.9.73.egg-info
        pycurl-7.43.0.egg-info
        pycurl.cpython-35m-x86_64-linux-gnu.so
        pygobject-3.20.0.egg-info
        pygtkcompat
        PyJWT-1.3.0.egg-info
        pylab.py
        pyparsing-2.0.3.egg-info
        pyparsing.py
        python_apt-1.1.0.b1_ubuntu0.16.04.1.egg-info
        python_dateutil-2.4.2.egg-info
        python_debian-0.1.27.egg-info
        python_systemd-231.egg-info
        pytz
        pytz-2014.10.egg-info
        pyxdg-0.25.egg-info
        PyYAML-3.11.egg-info
        Quirks
        reportlab
        reportlab-3.3.0.egg-info
        requests
        requests-2.9.1.egg-info
        scanext.cpython-35m-x86_64-linux-gnu.so
        scipy
        scipy-0.17.0.egg-info
        sessioninstaller
        sessioninstaller-0.0.0.egg-info
        setuptools
        setuptools-20.7.0.egg-info
        six-1.10.0.egg-info
        six.py
        softwareproperties
        speechd
        speechd_config
        systemd
        system_service-0.3.egg-info
        tables
        tables-3.2.2.egg-info
        UbuntuDrivers
        ubuntu_drivers_common-0.0.0.egg-info
        UbuntuSystemService
        ufw
        ufw-0.35.egg-info
        unattended_upgrades-0.1.egg-info
        unity_scope_calculator-0.1.egg-info
        unity_scope_chromiumbookmarks-0.1.egg-info
        unity_scope_colourlovers-0.1.egg-info
        unity_scope_devhelp-0.1.egg-info
        unity_scope_firefoxbookmarks-0.1.egg-info
        unity_scope_gdrive-0.7.egg-info
        unity_scope_manpages-0.1.egg-info
        unity_scope_openclipart-0.1.egg-info
        unity_scope_texdoc-0.1.egg-info
        unity_scope_tomboy-0.1.egg-info
        unity_scope_virtualbox-0.1.egg-info
        unity_scope_yelp-0.1.egg-info
        unity_scope_zotero-0.1.egg-info
        unohelper.py
        uno.py
        UpdateManager
        urllib3
        urllib3-1.13.1.egg-info
        usbcreator
        usb_creator-0.3.0.egg-info
        wheel
        wheel-0.29.0.egg-info
        xdg
        xdiagnose
        xdiagnose-3.8.4.1.egg-info
        xkit
        xkit-0.0.0.egg-info
        xlsxwriter
        XlsxWriter-0.7.3.egg-info
        yaml
        _yaml.cpython-35m-x86_64-linux-gnu.so
        

7 个答案:

答案 0 :(得分:11)

这为我解决了问题

curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3

尝试对每个特定项目使用virtualenv,而不要弄乱ubuntu子系统。

答案 1 :(得分:2)

<块引用>

没有名为“pip._vendor.packaging”的模块

基本上解决方案是卸载系统 pipenv 并从我的本地 pipenv 安装 pip,该系统由 pyenv 管理。

执行此操作后,我在运行 pipenv install 时不再遇到问题。

答案 2 :(得分:1)

我也有同样的问题,我通过此命令解决了。

pip uninstall pkg-resources==0.0.0

答案 3 :(得分:1)

感谢@ s_s.411

我通过以下命令解决了这个问题:

curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3
type pip3
hash -r
pip3

答案 4 :(得分:1)

我在Ubuntu 16.04上遇到了同样的问题,使用像您这样的系统python 3.5.2,并通过apt({sudo apt install python3-pip)像您一样安装了pip,并在我的主目录中安装了{{ 1}},就像您一样(记录在your chat with hoefling中)。

解决方案如下:

  1. 暂时移走所有pip3 install --user安装的软件包:
pip3 install --user

这使mv ~/.local/lib/python3.5/site-packages{,_backup} 再次工作,但是我当然想保留包装。 (注意:仅移出pip3是不够的;我不确定是哪个软件包引起的。)

  1. 在主目录中安装升级后的pip:
setuptools

现在pip3 install --upgrade --user pip 命令失败(pip3),因为它仍然是从当前Shell中的旧ImportError: cannot import name 'main'位置调用的,如/usr/bin/pip3所示。要解决此问题,请运行:

type pip3

或者,您总是可以退回到键入hash -r 而不是python3 -m pip

  1. 还原软件包:
pip3
  1. 现在pip正在运行,但是mv ~/.local/lib/python3.5/site-packages{_backup/*,} rmdir ~/.local/lib/python3.5/site-packages_backup 失败,但出现了与您看到的相同的异常python3 -m 'import setuptools'。可以通过卸载然后在我的主目录中重新安装AttributeError: '_NamespacePath' object has no attribute 'sort'软件包来解决(单独卸载是不够的):
setuptools

最后,pip3 uninstall setuptools pip3 install --user --upgrade setuptools pip3很好。

答案 5 :(得分:0)

我尝试了此页面上的所有修复程序,但没有成功,最后我发现我的virtualenv损坏了,我不得不像这样重新安装virtualenv

deactivate-为安全起见先停用
virtualenv --python=/usr/bin/python3.8 /path/to/my_virtualenv_python3
source /path/to/my_virtualenv_python3/bin/activate-激活环境

当然,如果您不使用3.8版,则将/path/to/my_virtualenv_python替换为您自己的路径,将python3.8替换为自定义版本的python:)

还请注意,这可能会或可能不会删除已安装的软件包,因此只需重新安装以后需要的每个requirements.txt。

答案 6 :(得分:0)

我知道这是一个老问题,但我在 WSL Ubuntu 上遇到了这个错误,而且投票最多的解决方案对我不起作用。

有效的:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py --force-reinstall