63

I have installed pip for python 3.6 on Ubuntu 14. After I run

sudo apt-get install python3-pip

to install pip3, it works very well. However, after installation, when I am trying to run

pip3 install packagename

to install a new package, something strange occurs:

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"

It seems that I did nothing wrong, and I really cannot figure out the reason.

Arsenal591
  • 856
  • 1
  • 6
  • 11

21 Answers21

85

Faced the same problem. I think this is because python3.6 and pip3 were installed from different sources. I suggest using python's inbuilt facility to install pip i.e

python3 -m ensurepip --upgrade

This should install pip3 and pip3.x where x in python3.x. Same works for python2 also.

azhar22k
  • 2,969
  • 1
  • 17
  • 21
  • 44
    '/usr/bin/python3.6: No module named ensurepip' Any ideas? – Marek Židek Mar 04 '18 at 11:26
  • That's strange! According to https://docs.python.org/3/library/ensurepip.html `ensurepip` is part of python standard library. Are you sure you installed python correctly and is accessible as well? – azhar22k Mar 05 '18 at 05:03
  • 6
    Is `ensurepip` available on Ubuntu? On my Ubuntu environment, I can't import it. Maybe, on many other environments, `ensurepip` is not available. – H. Jang Nov 26 '18 at 02:45
  • 3
    @H.Jang Yeah. Its a bug in Ubuntu. https://bugs.launchpad.net/ubuntu/+source/python3.4/+bug/1290847 – azhar22k Nov 26 '18 at 08:48
  • @azhar22k is `ensurepip` available for python3.7 installed from `deadsnakes/ppa`repository? – Ibrahim.H Jul 22 '20 at 14:24
  • 1
    Hey @Ibrahim.H, haven't checked it myself so can't say – azhar22k Jul 28 '20 at 05:16
78

I can't edit my existing answer, so I had to add another one:

This worked for me:

sudo pip install python-dotenv
Rod McLaughlin
  • 789
  • 4
  • 3
17

I had the same problem on my ubuntu 18.04 with python 3.6. None of the above methods helped, but this one solved the problem:

pip3 uninstall setuptools

mishadr
  • 185
  • 1
  • 4
  • I had issues with fs-uae-launcher not starting on Debian Buster because: AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader' Uninstalling setuptools fixed it. – Okw Aug 30 '20 at 11:55
  • Thanks for the solution, none of the above solution worked and this issue was annoying my day :) – rkachach Sep 21 '20 at 13:45
13

Tried to install Tensorflow in venv on Windows 10 machine with python 3.8 and got the same issue.

What helped for me was:

pip install setuptools --upgrade

fbrand
  • 141
  • 1
  • 6
6

when update python3.4 to python3.6 on ubuntu 14.04. The following solved me:

wget https://bootstrap.pypa.io/ez_setup.py -O - | python3
neighbor wang
  • 151
  • 2
  • 4
4

I had the same error whatever I asked to pip. I gave a look to this page: https://packaging.python.org/tutorials/installing-packages/

That line is the one that solved my problem:

python3 -m pip install --upgrade pip setuptools wheel
  • After upgrading from Ubuntu 19.10 to 20.04 I had this problem with pip3. Nothing I'd found worked. This finally solved it. – wrkyle Jan 26 '21 at 21:25
3

that's because you are using an old version of setuptools, check up this issue.

3

If you face this issue in anaconda environment, simply upgrade setuptools using the following:

conda install -c conda-forge setuptools
Martin Gal
  • 4,180
  • 3
  • 10
  • 23
2

I met the same problem, this is the key:

curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3
Tom Kruise
  • 31
  • 2
2

I am facing the same problem, which is solved by downloading the source files of the setuptools and installing the module manually.

The setuptools can be downloaded here:

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

After downloading, unzip the package first, then cd to the directory and run

python setup.py install --user
Anna
  • 319
  • 3
  • 10
2
pip install setuptools --upgrade

This command fixed my problem

Jeex Box
  • 39
  • 4
1

For me the error happened while trying to create a virtual env with python 3.8:

sudo virtualenv venv -ppython3.8

And after trying all the answers here, finally the problem solved by installing the new version of virtualenv (20.0.7):

sudo pip3 install virtualenv
Alex Jolig
  • 10,688
  • 19
  • 103
  • 148
1

I had faced the same issue on Ubuntu 19.10 and now I upgraded to Ubuntu 20.04 and faced the issue again. This issue is due to broken pip3. So whenever you enter pip3 and hit enter it will show the same error. So instead of using "pip3 uninstall setuptools" use the below code

python3 -m pip uninstall setuptools

It resolved my issue 3rd time

1

I encountered this error message triggered by a slightly different situation which I'll mention here for anyone who finds this.

This same error also occurs when installing the distribute Python package (which is currently just a compatibility layer in front of setuptools) in Python 3.6 or newer.

In my specific situation I discovered this as I was using 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'
gene_wood
  • 1,630
  • 2
  • 24
  • 33
0

I had the same issue when I tried to install the Slate package in Windows 10, Python 3.7.4 version:

AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'

The instruction that generated the error:

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

The instruction that worked:

C:\WINDOWS\system32>python -m pip install https://github.com/timClicks/slate/archive/master.zip
David Buck
  • 3,439
  • 29
  • 24
  • 31
0
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>

Mine looked pretty similar, but for some reason I had a python installation in my home directory .local folder.

I did some of the other answers in this thread to ensure I had good local copy of python, then did:

rm -rf ~/.local
user2312718
  • 31
  • 1
  • 4
0

I had the same problem with my cloud computer. If all above didn't work for you don't worry. Here's how I resolved it:

  1. Download the pip file (pip-version.tar.gz) from:
https://pypi.org/project/pip/#files

For cloud, use this

curl https://files.pythonhosted.org/packages/8e/76/66066b7bc71817238924c7e4b448abdb17eb0c92d645769c223f9ace478f/pip-20.0.2.tar.gz --output pip.tar.gz
  1. Extract the content of the file and cd into the directory.
  2. Run the following in the directory
python3 setup.py install --user

You should have pip3 working without errors.

i Raaj
  • 1
  • 1
  • Still get same error - Ubuntu 14: AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader' – DtechNet Jun 17 '20 at 17:23
0

just go inside the /usr/lib/python3/dist-packages/ first copy setuptools file somewhere and then delete the setuptools everything will be okay for deleting you can use

sudo rm -r setuptools

for copying

sudo cp -r setuptools /...Desktop/

after that if it gives errors just

sudo pip3 install setuptools==3.8.1

download again

Dev Ran
  • 11
  • 1
0
pip3 install setuptools --upgrade

This also cured the issue on Python 3.9 running on Windows 10 and even on a custom Docker image.

Obsidian
  • 2,760
  • 6
  • 14
  • 24
0

I was having the same issue while using docker-compose. This exact error can be caused by not being the root user. Running the same commands with sudo fixed it for me. I don't not have setup-tools installed, and I'm running pip3. My issue was that IntelliJ didn't have sudo rights, so i had to do it from the terminal.

Dharman
  • 21,838
  • 18
  • 57
  • 107
Matthew
  • 1
  • 1
-1

This is For Python 2

curl the package

curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"

And do python get-pip.py

This is Important use command pip2 not pip

  • This does not answer the question. The question of the OP was what he is doing wrong or how to solve his issue with python3. This is about something entirely different. – Dom Aug 20 '20 at 07:08