pip: ImportError: cannot import name 'SourceDistribution' from 'pip._internal.distributions.source'

Environment

CircleCI using Conda, eg https://circleci.com/gh/peterjc/thapbi-pict/1073

  • pip version: pip-19.2.3 (from conda) updated to 19.3 (via pip install --upgrade pip setuptools)
  • Python version: python-3.7.3
  • OS: Linux

I have made only minor changes to my code, but today the CircleCI builds broke during the build/install stage of my Python tool - multiple branches affected. The timing fits the release of pip 19.3 on PyPI.

Description

It appears a recent release of pip moved some internal files about, my guess is #6830.

Expected behavior

How to Reproduce

Full output on https://circleci.com/gh/peterjc/thapbi-pict/1073 which is running https://github.com/peterjc/thapbi-pict/blob/9fcec2da60e6e6ae5cf7ee6ad4b53dcc3a40cfe7/.circleci/config.yml#L56

...
python setup.py sdist --formats=gztar
python setup.py bdist_wheel
pip install dist/thapbi_pict-*.whl

Output

Traceback (most recent call last):
  File "/opt/conda/bin/pip", line 7, in <module>
    from pip._internal import main
  File "/opt/conda/lib/python3.7/site-packages/pip/_internal/__init__.py", line 40, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/opt/conda/lib/python3.7/site-packages/pip/_internal/cli/autocompletion.py", line 8, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/opt/conda/lib/python3.7/site-packages/pip/_internal/cli/main_parser.py", line 11, in <module>
    from pip._internal.commands import (
  File "/opt/conda/lib/python3.7/site-packages/pip/_internal/commands/__init__.py", line 9, in <module>
    from pip._internal.commands.download import DownloadCommand
  File "/opt/conda/lib/python3.7/site-packages/pip/_internal/commands/download.py", line 10, in <module>
    from pip._internal.operations.prepare import RequirementPreparer
  File "/opt/conda/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 9, in <module>
    from pip._internal.distributions import (
  File "/opt/conda/lib/python3.7/site-packages/pip/_internal/distributions/__init__.py", line 1, in <module>
    from pip._internal.distributions.source import SourceDistribution
ImportError: cannot import name 'SourceDistribution' from 'pip._internal.distributions.source' (/opt/conda/lib/python3.7/site-packages/pip/_internal/distributions/source/__init__.py)
Exited with code 1

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 60
  • Comments: 101 (20 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks @PabloCastellano! I’m just ignoring comments here now, since I don’t want my heartrate going any higher. 😃

Made the bugfix release. If that helped, use 👍 on this post. Otherwise, please write a comment with the error message.

Please stop reporting “me too”. @pradyunsg is working on a fix right now

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

This helped for now

I’ll update that in the next few minutes.

Alrighty, pushed this now. The change should’ve propagated to bootstrap.pypa.io by now. Or it will really soon.

Okay, I have reproduced this locally – I’ll file a PR with a fix soon.

I’m now getting issues with this on the 0.20 release.

Can others seeing this failure confirm that there’s a source/ folder in pip/_internal/distributions?

If you can confirm that folder exists, don’t make comment here – use the 👍 reaction on this post. That folder does not exist, use the 👎 reaction.

Sorry everyone for the breakage, and thanks for working with us here to fix this breakage!

@xavfernandez figured out why this happened and we’ll be making changes to our release process to prevent similar failures in the future: #7624

If you use virtualenv just downgrade to 19.x. This resolved the issue in my virtualenv:

virtualenv --no-pip <ENV NAME>
# activate the virtual environment
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py pip==19.2.2

I just got this when my deployment auto-upgraded pip from 19.3.1 to 20.0

Everything is already using python -m pip - that is not the issue here

I can tell you that the fix from JediKev, that is to delete this folder:

site-packages/pip/_internal/distributions/source

fixed the problem

Are any of you guys using caching? I think it’s possible some pip internals changed and when you restore the cache of the old python version pip breaks. Suggest you invalidate your circle CI caches by changing the keys and see what happens … (worked for me!)

Removing folder ./site-packages/pip/_internal/distributions/source helps!

Same here, all of our CI builds that update pip are failing.

XD

Replacing pip install --upgrade pip with pip install --upgrade pip"<20.0" working here.

pip install pip"<20.0" temp solution but it works.

Note that get-pip.py hasn’t been updated yet – that’s because, well, I borked my own dev environment for get-pip when testing this. 😃

I’ll update that in the next few minutes.

😃 let us know when this will be good to go

ok let me run my build again

Hi @CharrierCoop! You should be able to download get-pip.py and install pip 20.0.1 using that.

See https://pip.pypa.io/en/stable/installing/

@pradyunsg Hats off for the fast fix, we really appreciate it ❤️

Agreed, fantastic turnaround on the fix here!

Can confirm. pip==20.0.1 is working fine

For a quickfix,

$(which easy_install) pip==19.3

also works ok

Same here with python 3.7.6 and pip 20.0

Had to select the previous version to sort out, curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py pip=19.3.1

A quick fix which is working is

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py pip==9.0.2

Saved our CI pipeline

What if I roll back to pip 19.x? Would it work?

EDIT: pip install --upgrade "pip==19.3.1" still works.

sudo python get-pip.py pip==19.3.1" fix this issue

I think, the problem was created because there is a source module directory as well as source.py in the pip/_internal/distributions path and that messes up the imports

In pip/_internal/distributions/__init__.py have an import line:

from pip._internal.distributions.source import SourceDistribution # <-- here
from pip._internal.distributions.wheel import WheelDistribution
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

But there’s also a source dir with __init__.py inside there,

  env  ~/.../_internal/distributions  ls -ltr
total 32
-rw-r--r-- 1 user xxx 1294 Jan 21 18:02 wheel.py
-rw-r--r-- 1 user xxx  760 Jan 21 18:02 installed.py
-rw-r--r-- 1 user xxx 1425 Jan 21 18:02 base.py
drwxr-xr-x 3 user xxx 4096 Jan 21 18:02 source
-rw-r--r-- 1 user xxx 4201 Jan 21 18:19 source.py
-rw-r--r-- 1 user xxx  961 Jan 21 18:26 __init__.py
drwxr-xr-x 2 user xxx 4096 Jan 21 18:26 __pycache__

Hence that import will fail. Renaming the source.py to _source.py and changing the import in __init__.py to from pip._internal.distributions._source import SourceDistribution seems to fix it

Note that get-pip.py hasn’t been updated yet – that’s because, well, I borked my own dev environment for get-pip when testing this. 😃

I’ll update that in the next few minutes.

For a quickfix,

$(which easy_install) pip==19.3

also works ok

Between all the temporary workarounds, only this one worked for me

perhaps is it possible to create a virtualenv seting pip versio to 19.3.1 (the prev.)

@manutero, the --no-download flag of the virtualenv command will avoid trying to download the latest version of pip at the time of creating the environment.

All builds are broken due to the same error on our end too.

@pradyunsg any CI improvements so that it doesn’t happen in the future? A docker container would be sufficient. Would you accept PRs for this?

Had to select the previous version to sort out, curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py pip=19.3.1

It should be python3 get-pip.py pip==19.3.1

@pradyunsg, on Fedora 29 I can reproduce like this:

rm -rf ~/.local/lib/python3.7
sudo dnf install python3-pip
pip3.7 install -U --user pip
python3.7 -m pip install requests
Traceback (most recent call last):
  File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/kaiant/.local/lib/python3.7/site-packages/pip/__main__.py", line 19, in <module>
    sys.exit(_main())
  File "/home/kaiant/.local/lib/python3.7/site-packages/pip/_internal/cli/main.py", line 73, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "/home/kaiant/.local/lib/python3.7/site-packages/pip/_internal/commands/__init__.py", line 96, in create_command
    module = importlib.import_module(module_path)
  File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/kaiant/.local/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 24, in <module>
    from pip._internal.cli.req_command import RequirementCommand
  File "/home/kaiant/.local/lib/python3.7/site-packages/pip/_internal/cli/req_command.py", line 20, in <module>
    from pip._internal.operations.prepare import RequirementPreparer
  File "/home/kaiant/.local/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 16, in <module>
    from pip._internal.distributions import (
  File "/home/kaiant/.local/lib/python3.7/site-packages/pip/_internal/distributions/__init__.py", line 1, in <module>
    from pip._internal.distributions.source import SourceDistribution
ImportError: cannot import name 'SourceDistribution' from 'pip._internal.distributions.source' (/home/kaiant/.local/lib/python3.7/site-packages/pip/_internal/distributions/source/__init__.py)

Try this:

1.curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 2.python get-pip.py

I know my setup might be different than most but the issue on my end was due to the PIP sources/ directory in the virtual environment (most likely PIP caching issues or something). Let me explain: When deleting the user_builds/project/envs/ directory for a project and rebuilding it works but only until you go to rebuild with the envs/ directory present again. I spent a ton of time debugging what the issue was and it turns out it was the user_builds/project/envs/ directory (or something inside) causing issues. So I did more testing to figure out that it was the user_builds/project/envs/latest/lib/python3.6/site-packages/pip/_internal/distributions/source/ directory specifically causing issues. I removed the user_builds/project/envs/latest/lib/python3.6/site-packages/pip/_internal/distributions/source/ directory, rebuilt, and everything was fine. I rebuilt again and it failed. I checked the user_builds/project/envs/latest/lib/python3.6/site-packages/pip/_internal/distributions/ directory again and saw that source/ was present. So I went to my global Python install and moved the /path/to/lib/python3.6/dist-packages/pip/_internal/distributions/source/ directory from PIP, removed the user_builds/project/envs/latest/lib/python3.6/site-packages/pip/_internal/distributions/source/ directory from the project environment, and rebuilt. This built successfully and didn’t copy over the source/ folder, perfect. So now any time the docs build they build without errors. I hope this helps someone else.

Cheers.

I’m gonna go ahead and close this issue now, since it seems like things are working for most users.

If you’re still facing issues, please look for duplicates filed in earlier (since Jan 21, for pip 20.0) and if there are none, file a new issue.

sudo python get-pip.py pip==19.3.1" fix this issue

No need this issue was fixed by @pradyunsg

I can confirm, the new pip gets installed now that does not suffer from this error anymore. Thanks @pradyunsg !

I don’t want my heartrate going any higher. 😃 Made the bugfix release.

@pradyunsg You’ve just lowered mine. Seems to work.

Yep. hitting this now worked yesterday.

Try this:

1.curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 2.python get-pip.py

Thank you

Hey people,

I really don’t know if this helps you in any way, but I got the same Error Message on my MSYS setup trying to install WeasyPrint.

After uninstalling python3-pip and python3-setuptools i deleted all pip folders in /usr/lib/python3.7/site-packages/ and reinstalled both… solved that specific error.

pacman -R python3-pip python3-setuptools
rm -R /usr/lib/python3.7/site-packages/pip
rm -R /usr/lib/python3.7/site-packages/pip-19.3.1.dist-info
pacman -S python3-pip python3-setuptools

To be fair, I now ran into compiler issues… but maybe i could help…

I have same problem on CircleCI since 1 hours ago. Version changing of docker image from python:3.7 to python:3.7.4 fixes this issue. (just workaround) . like this:

version: 2
...
jobs:
  build:
    docker:
      - image: python:3.7.4
...

sudo python get-pip.py pip==19.3.1" fix this issue

thank you very much

can you please remove 20.0 release from PYPA?

No. We’re not going to be deleting it. We have a standard-backed feature for “yanking” broken releases. Once PyPI adds support for that, we’ll yank this release (if anyone cares by then).

See https://www.python.org/dev/peps/pep-0592/#motivation for more details.

Hello, I’m stuck in 20.0 All the commands that I tried return this error about ‘SourceDistribution’.

pip install --upgrade pip
pip install pip "<20.0"
pip install --upgrade "pip==19.3.1"

I tired to uninstall too :

pip uninstall pip
python -m pip uninstall pip setuptools

Anyone have another solution to uninstall pip?

Ubuntu 18.04.3 LTS Python 3.6.9 run in virtualenv

Considering the nature of this bug, can you please remove 20.0 release from PYPA? https://pypi.org/project/pip/#history

Another question would what we could do to avoid a similar regression in the future.

Replacing pip install --upgrade pip with pip install --upgrade pip"<20.0" working here.

Help me in docker RUN pip install --upgrade pip"<20.0"

Yep, pip 20.0 breaks , using “virtualenv .” command to create venv

Yeah, pip version 20.0 breaks

Moving to python -m pip helped in my situation. From https://github.com/ome/jenkins-library-recursivemerge/pull/4/files

Fails:

    (cd build && curl -sfL ${buildInfraUrl} | tar -zxf -)
    virtualenv build/venv && build/venv/bin/pip install ${sccPackage}

Passes:

    python -m venv build/venv
    . build/venv/bin/activate
    python -m pip install ${sccPackage}

@Guru36

Tried that many many times with no success. You have to do this at the start anyways so this wouldn’t help. Thanks anyways though.

Cheers.

We were able to do work-around on our local ReadTheDocs server by changing the build flow to specifically use pip version 19.2.3. Now ReadTheDocs runs the following build command - and we no longer have caching issue:

/var/lib/readthedocs/repo/user_builds/some_doc/envs/latest/bin/python -m pip install --upgrade --cache-dir /var/lib/readthedocs/repo/user_builds/some_doc/.cache/pip pip==19.2.3

More specifically, we updated the local python_environment.py file to use a specific pip version: https://github.com/readthedocs/readthedocs.org/blob/7212d6ff738b24a10fb0f4227d3fbdf69e5cab42/readthedocs/doc_builder/python_environments.py#L322

cmd = pip_install_cmd + ['pip']

changed to:

cmd = pip_install_cmd + ['pip==19.2.3']

Confirmed that removing pip install --upgrade pip setuptools fixed things,

https://github.com/peterjc/thapbi-pict/commit/4060404c1034a676439aef1369260022577a6627 https://circleci.com/gh/peterjc/thapbi-pict/1074

i.e. Something broke in how I updated from pip 19.2.3 to 19.3

I am not intentionally using the CircleCI provided Python, rather the Conda provided Python - is there value in switching from pip ... to python -m pip ... just to remove the possibility of this being due to mixing Pythons?