【问题标题】:using pip3: module "importlib._bootstrap" has no attribute "SourceFileLoader"使用 pip3:模块“importlib._bootstrap”没有属性“SourceFileLoader”
【发布时间】:2017-11-29 10:13:14
【问题描述】:

我已经在 Ubuntu 14 上为 python 3.6 安装了 pip。运行之后

sudo apt-get install python3-pip

安装pip3,效果很好。但是,安装后,当我尝试运行时

pip3 install packagename

要安装一个新的包,会发生一些奇怪的事情:

File "/usr/lib/python3/dist-packages/pkg_resources.py", line 1479, in <module>
 register_loader-type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module "importlib._bootstrap" has no attribute "SourceFileLoader"

看来我没有做错什么,实在想不通。

【问题讨论】:

  • 您尝试安装的每个软件包都会出现这种情况吗?
  • 看来问题出在 python 版本上。
  • @SamChats 是的...实际上每次我输入“pip3”都会出现这个问题,例如“pip3 --version”等。
  • @phd 你为什么发这个。人们来这里的原因是因为顶部答案的第一个命令不起作用。

标签: python linux ubuntu pip


【解决方案1】:

面临同样的问题。我认为这是因为python3.6pip3 是从不同的来源安装的。 我建议使用 python 的内置工具来安装 pip 即

python3 -m ensurepip --upgrade

这应该安装pip3pip3.x 其中x 在python3.x。 同样适用于python2

【讨论】:

  • '/usr/bin/python3.6: No module named ensurepip' 有什么想法吗?
  • 这很奇怪!根据docs.python.org/3/library/ensurepip.htmlensurepip 是python 标准库的一部分。你确定你正确安装了python并且也可以访问吗?
  • ensurepip 在 Ubuntu 上可用吗?在我的 Ubuntu 环境中,我无法导入它。也许,在许多其他环境中,ensurepip 不可用。
  • @H.Jang 是的。它是 Ubuntu 中的一个错误。 bugs.launchpad.net/ubuntu/+source/python3.4/+bug/1290847
  • 嘿@Ibrahim.H,我自己没有检查过,所以不能说
【解决方案2】:

我无法编辑我现有的答案,所以我不得不添加另一个答案:

这对我有用:

sudo pip install python-dotenv

【讨论】:

  • 1.你怎么知道? 2. 还是
  • 这是对我有用的。在我的情况下,接受的答案没有做任何事情
  • 我认为这个答案是针对 dotenv 的
  • sudo pip3 install python-dotenv 如果你在 pip for python2 旁边安装了 pip3
【解决方案3】:

尝试使用 python 3.8 在 Windows 10 机器上的 venv 中安装 Tensorflow,但遇到了同样的问题。

对我有帮助的是:

pip install setuptools --upgrade

【讨论】:

  • 对 Ubuntu 18.04 也有帮助
  • 我有基于 Ubuntu 的 Pop!_OS 20.10,这很有帮助
【解决方案4】:

我在使用 python 3.6 的 ubuntu 18.04 上遇到了同样的问题。以上方法都没有帮助,但是这个解决了问题:

pip3 uninstall setuptools

【讨论】:

  • 我遇到了 fs-uae-launcher 无法在 Debian Buster 上启动的问题,因为:AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader' 卸载 setuptools 修复了它。
  • 感谢您的解决方案,上述解决方案均无效,这个问题困扰了我一天 :)
  • pip3 uninstall setuptools 使情况更糟pip3 install setuptools与 topick 中的错误相同
【解决方案5】:

无论我要求点什么,我都遇到了同样的错误。我看了一下这个页面:https://packaging.python.org/tutorials/installing-packages/

那一行是解决我问题的那一行:

python3 -m pip install --upgrade pip setuptools wheel

【讨论】:

  • 从 Ubuntu 19.10 升级到 20.04 后,我遇到了 pip3 的这个问题。我没有发现任何工作。这终于解决了。
【解决方案6】:

在 ubuntu 14.04 上将 python3.4 更新为 python3.6 时。以下解决了我:

wget https://bootstrap.pypa.io/ez_setup.py -O - | python3

【讨论】:

【解决方案7】:
pip install setuptools --upgrade

这个命令解决了我的问题

【讨论】:

    【解决方案8】:

    那是因为您使用的是旧版本的 setuptools,请查看此issue

    【讨论】:

      【解决方案9】:

      如果您在 anaconda 环境中遇到此问题,只需使用以下命令升级 setuptools:

      conda install -c conda-forge setuptools
      

      【讨论】:

        【解决方案10】:

        我遇到了同样的问题,这是关键:

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

        【讨论】:

          【解决方案11】:

          我也遇到了同样的问题,通过下载setuptools的源文件,手动安装模块解决了。

          安装工具可以在这里下载:

          https://pypi.org/project/setuptools/
          

          下载后先解压包,然后cd到目录下运行

          python setup.py install --user
          

          【讨论】:

          • 救了我的命 :)
          【解决方案12】:

          对我来说,尝试使用 python 3.8 创建虚拟环境时发生错误:

          sudo virtualenv venv -ppython3.8
          

          并且在这里尝试了所有答案后,终于通过安装virtualenv的新版本解决了问题(20.0.7)

          sudo pip3 install virtualenv
          

          【讨论】:

            【解决方案13】:

            我在 Ubuntu 19.10 上遇到过同样的问题,现在我升级到 Ubuntu 20.04 并再次遇到这个问题。这个问题是由于 pip3 损坏造成的。因此,每当您输入 pip3 并按回车键时,它都会显示相同的错误。因此,不要使用“pip3 卸载 setuptools”,而是使用下面的代码

            python3 -m pip uninstall setuptools
            

            它第三次解决了我的问题

            【讨论】:

              【解决方案14】:

              我遇到了由稍微不同的情况触发的此错误消息,我将在此处为发现此问题的任何人提及。

              在 Python 3.6 或更高版本中安装distribute Python 包(目前只是setuptools 前面的一个兼容层)时也会出现同样的错误。

              在我的具体情况下,我在使用pyzmail which has been somewhat abandoned and depends on distribute 时发现了这一点。

              Collecting distribute
                Downloading distribute-0.7.3.zip (145 kB)
                  ERROR: Command errored out with exit status 1:
                   command: /var/lang/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ssqyqflj/distribute/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ssqyqflj/distribute/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-hgbjn0js
                       cwd: /tmp/pip-install-ssqyqflj/distribute/
                  Complete output (15 lines):
                  Traceback (most recent call last):
                    File "<string>", line 1, in <module>
                    File "/tmp/pip-install-ssqyqflj/distribute/setuptools/__init__.py", line 2, in <module>
                      from setuptools.extension import Extension, Library
                    File "/tmp/pip-install-ssqyqflj/distribute/setuptools/extension.py", line 5, in <module>
                      from setuptools.dist import _get_unpatched
                    File "/tmp/pip-install-ssqyqflj/distribute/setuptools/dist.py", line 7, in <module>
                      from setuptools.command.install import install
                    File "/tmp/pip-install-ssqyqflj/distribute/setuptools/command/__init__.py", line 8, in <module>
                      from setuptools.command import install_scripts
                    File "/tmp/pip-install-ssqyqflj/distribute/setuptools/command/install_scripts.py", line 3, in <module>
                      from pkg_resources import Distribution, PathMetadata, ensure_directory
                    File "/tmp/pip-install-ssqyqflj/distribute/pkg_resources.py", line 1518, in <module>
                      register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
                  AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
              

              【讨论】:

                【解决方案15】:

                当我尝试在 Windows 10,Python 3.7.4 版本中安装 Slate 包时遇到了同样的问题:

                AttributeError: 模块 'importlib._bootstrap' 没有属性 'SourceFileLoader'

                产生错误的指令:

                C:\WINDOWS\system32>python -m pip install slate
                

                有效的指令:

                C:\WINDOWS\system32>python -m pip install https://github.com/timClicks/slate/archive/master.zip
                

                【讨论】:

                  【解决方案16】:
                  Traceback (most recent call last):
                  File "/usr/bin/pipenv", line 6, in <module>
                  from pkg_resources import load_entry_point
                  File "/home/myuser/.local/lib/python3.7/site-packages/pkg_resources.py", line 
                  1479, in <module>
                  

                  我的看起来很相似,但由于某种原因,我在我的主目录 .local 文件夹中安装了 python。

                  我在这个帖子中做了一些其他的答案,以确保我有良好的本地 python 副本,然后做了:

                  rm -rf ~/.local
                  

                  【讨论】:

                    【解决方案17】:

                    我的云电脑也有同样的问题。如果以上所有内容都不适合您,请不要担心。以下是我的解决方法:

                    1. 从以下位置下载pip 文件 (pip-version.tar.gz):
                    https://pypi.org/project/pip/#files
                    

                    对于云,使用这个

                    curl https://files.pythonhosted.org/packages/8e/76/66066b7bc71817238924c7e4b448abdb17eb0c92d645769c223f9ace478f/pip-20.0.2.tar.gz --output pip.tar.gz
                    
                    1. 解压文件内容并cd到目录中。
                    2. 在目录中运行以下命令
                    python3 setup.py install --user
                    

                    您应该让 pip3 正常工作。

                    【讨论】:

                    • 仍然得到同样的错误 - Ubuntu 14: AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
                    【解决方案18】:

                    只需进入/usr/lib/python3/dist-packages/ ,首先将 setuptools 文件复制到某处,然后删除 setuptools 一切都可以删除,您可以使用

                    sudo rm -r setuptools

                    用于复制

                    sudo cp -r setuptools /...Desktop/

                    之后如果它给出错误只是

                    sudo pip3 install setuptools==3.8.1

                    重新下载

                    【讨论】:

                      【解决方案19】:
                      pip3 install setuptools --upgrade
                      

                      这也解决了在 Windows 10 甚至自定义 Docker 映像上运行的 Python 3.9 上的问题。

                      【讨论】:

                        【解决方案20】:

                        我在使用docker-compose 时遇到了同样的问题。这个确切的错误可能是由于不是 root 用户造成的。使用sudo 运行相同的命令为我修复了它。我没有安装setup-tools,我正在运行pip3。我的问题是 IntelliJ 没有 sudo 权限,所以我必须从终端执行。

                        【讨论】:

                          【解决方案21】:

                          以上解决方案不适用于我的 Ubuntu 20

                          如果你运行以下命令:

                          sudo apt install python3-setuptools
                          

                          那么你可能会得到结果:

                          Reading package lists... Done
                          Building dependency tree       
                          Reading state information... Done
                          python3-setuptools is already the newest version (45.2.0-1).
                          0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
                          

                          但要真正解决它并重新安装 setuptools,请尝试以下命令:

                          sudo apt download python3-setuptools
                          sudo dpkg -i python3-setuptools_45.2.0-1_all.deb 
                          

                          注意:更改下载的python包的版本

                          【讨论】:

                            猜你喜欢
                            • 2019-07-02
                            • 1970-01-01
                            • 2018-03-12
                            • 2016-01-26
                            • 2020-10-10
                            • 2021-01-08
                            • 1970-01-01
                            相关资源
                            最近更新 更多